001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.webdav.Resource;
020    import com.liferay.portal.kernel.webdav.WebDAVException;
021    import com.liferay.portal.kernel.webdav.WebDAVRequest;
022    import com.liferay.portal.kernel.webdav.WebDAVStorage;
023    import com.liferay.util.servlet.ServletResponseUtil;
024    
025    import java.io.InputStream;
026    
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     */
033    public class GetMethodImpl implements Method {
034    
035            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
036                    InputStream is = null;
037    
038                    try {
039                            WebDAVStorage storage = webDavRequest.getWebDAVStorage();
040                            HttpServletResponse response =
041                                    webDavRequest.getHttpServletResponse();
042    
043                            Resource resource = storage.getResource(webDavRequest);
044    
045                            if (resource != null) {
046                                    try {
047                                            is = resource.getContentAsStream();
048                                    }
049                                    catch (Exception e) {
050                                            if (_log.isErrorEnabled()) {
051                                                    _log.error(e.getMessage());
052                                            }
053                                    }
054                            }
055    
056                            int status = HttpServletResponse.SC_NOT_FOUND;
057    
058                            if (is != null) {
059                                    try {
060                                            response.setContentType(resource.getContentType());
061    
062                                            ServletResponseUtil.write(response, is);
063                                    }
064                                    catch (Exception e) {
065                                            if (_log.isWarnEnabled()) {
066                                                    _log.warn(e);
067                                            }
068                                    }
069    
070                                    status = HttpServletResponse.SC_OK;
071                            }
072    
073                            return status;
074                    }
075                    catch (Exception e) {
076                            throw new WebDAVException(e);
077                    }
078            }
079    
080            private static Log _log = LogFactoryUtil.getLog(GetMethodImpl.class);
081    
082    }