001    /**
002     * Copyright (c) 2000-present 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                                    _log.error(e.getMessage());
060                            }
061    
062                            if (is != null) {
063                                    String fileName = resource.getDisplayName();
064    
065                                    FlashMagicBytesUtil.Result flashMagicBytesUtilResult =
066                                            FlashMagicBytesUtil.check(is);
067    
068                                    if (flashMagicBytesUtilResult.isFlash()) {
069                                            fileName = FileUtil.stripExtension(fileName) + ".swf";
070                                    }
071    
072                                    is = flashMagicBytesUtilResult.getInputStream();
073    
074                                    try {
075                                            ServletResponseUtil.sendFileWithRangeHeader(
076                                                    request, response, fileName, is, resource.getSize(),
077                                                    resource.getContentType());
078                                    }
079                                    catch (Exception e) {
080                                            if (_log.isWarnEnabled()) {
081                                                    _log.warn(e);
082                                            }
083                                    }
084    
085                                    return HttpServletResponse.SC_OK;
086                            }
087    
088                            return HttpServletResponse.SC_NOT_FOUND;
089                    }
090                    catch (Exception e) {
091                            throw new WebDAVException(e);
092                    }
093            }
094    
095            private static final Log _log = LogFactoryUtil.getLog(GetMethodImpl.class);
096    
097    }