001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }