001    /**
002     * Copyright (c) 2000-2013 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.flash.FlashMagicBytesUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.webdav.Resource;
023    import com.liferay.portal.kernel.webdav.WebDAVException;
024    import com.liferay.portal.kernel.webdav.WebDAVRequest;
025    import com.liferay.portal.kernel.webdav.WebDAVStorage;
026    import com.liferay.portal.kernel.webdav.methods.Method;
027    
028    import java.io.InputStream;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Alexander Chow
036     */
037    public class GetMethodImpl implements Method {
038    
039            @Override
040            public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
041                    InputStream is = null;
042    
043                    try {
044                            WebDAVStorage storage = webDAVRequest.getWebDAVStorage();
045                            HttpServletRequest request = webDAVRequest.getHttpServletRequest();
046                            HttpServletResponse response =
047                                    webDAVRequest.getHttpServletResponse();
048    
049                            Resource resource = storage.getResource(webDAVRequest);
050    
051                            if (resource == null) {
052                                    return HttpServletResponse.SC_NOT_FOUND;
053                            }
054    
055                            try {
056                                    is = resource.getContentAsStream();
057                            }
058                            catch (Exception e) {
059                                    if (_log.isErrorEnabled()) {
060                                            _log.error(e.getMessage());
061                                    }
062                            }
063    
064                            if (is != null) {
065                                    String fileName = resource.getDisplayName();
066    
067                                    FlashMagicBytesUtil.Result flashMagicBytesUtilResult =
068                                            FlashMagicBytesUtil.check(is);
069    
070                                    if (flashMagicBytesUtilResult.isFlash()) {
071                                            fileName = FileUtil.stripExtension(fileName) + ".swf";
072                                    }
073    
074                                    is = flashMagicBytesUtilResult.getInputStream();
075    
076                                    try {
077                                            ServletResponseUtil.sendFileWithRangeHeader(
078                                                    request, response, fileName, is, resource.getSize(),
079                                                    resource.getContentType());
080                                    }
081                                    catch (Exception e) {
082                                            if (_log.isWarnEnabled()) {
083                                                    _log.warn(e);
084                                            }
085                                    }
086    
087                                    return HttpServletResponse.SC_OK;
088                            }
089    
090                            return HttpServletResponse.SC_NOT_FOUND;
091                    }
092                    catch (Exception e) {
093                            throw new WebDAVException(e);
094                    }
095            }
096    
097            private static Log _log = LogFactoryUtil.getLog(GetMethodImpl.class);
098    
099    }