001    /**
002     * Copyright (c) 2000-2012 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.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.webdav.Resource;
021    import com.liferay.portal.kernel.webdav.WebDAVException;
022    import com.liferay.portal.kernel.webdav.WebDAVRequest;
023    import com.liferay.portal.kernel.webdav.WebDAVStorage;
024    
025    import java.io.InputStream;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Alexander Chow
033     */
034    public class GetMethodImpl implements Method {
035    
036            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
037                    InputStream is = null;
038    
039                    try {
040                            WebDAVStorage storage = webDavRequest.getWebDAVStorage();
041                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
042                            HttpServletResponse response =
043                                    webDavRequest.getHttpServletResponse();
044    
045                            Resource resource = storage.getResource(webDavRequest);
046    
047                            if (resource == null) {
048                                    return HttpServletResponse.SC_NOT_FOUND;
049                            }
050    
051                            try {
052                                    is = resource.getContentAsStream();
053                            }
054                            catch (Exception e) {
055                                    if (_log.isErrorEnabled()) {
056                                            _log.error(e.getMessage());
057                                    }
058                            }
059    
060                            if (is != null) {
061                                    try {
062                                            ServletResponseUtil.sendFile(
063                                                    request, response, resource.getDisplayName(), is,
064                                                    resource.getSize(), resource.getContentType());
065                                    }
066                                    catch (Exception e) {
067                                            if (_log.isWarnEnabled()) {
068                                                    _log.warn(e);
069                                            }
070                                    }
071    
072                                    return HttpServletResponse.SC_OK;
073                            }
074    
075                            return HttpServletResponse.SC_NOT_FOUND;
076                    }
077                    catch (Exception e) {
078                            throw new WebDAVException(e);
079                    }
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(GetMethodImpl.class);
083    
084    }