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.servlet.filters.uploadservletrequest;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.HttpHeaders;
019    import com.liferay.portal.kernel.upload.UploadServletRequest;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portal.servlet.filters.BasePortalFilter;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.InvokerPortlet;
029    import com.liferay.portlet.PortletInstanceFactoryUtil;
030    
031    import javax.servlet.FilterChain;
032    import javax.servlet.ServletContext;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Preston Crary
038     */
039    public class UploadServletRequestFilter extends BasePortalFilter {
040    
041            public static final String COPY_MULTIPART_STREAM_TO_FILE =
042                    UploadServletRequestFilter.class.getName() +
043                            "#COPY_MULTIPART_STREAM_TO_FILE";
044    
045            @Override
046            public void processFilter(
047                            HttpServletRequest request, HttpServletResponse response,
048                            FilterChain filterChain)
049                    throws Exception {
050    
051                    UploadServletRequest uploadServletRequest = null;
052    
053                    String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);
054    
055                    if ((contentType != null) &&
056                            contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA)) {
057    
058                            String portletId = ParamUtil.getString(request, "p_p_id");
059    
060                            if (Validator.isNotNull(portletId)) {
061                                    long companyId = PortalUtil.getCompanyId(request);
062    
063                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
064                                            companyId, portletId);
065    
066                                    if (portlet != null) {
067                                            ServletContext servletContext =
068                                                    (ServletContext)request.getAttribute(WebKeys.CTX);
069    
070                                            InvokerPortlet invokerPortlet =
071                                                    PortletInstanceFactoryUtil.create(
072                                                            portlet, servletContext);
073    
074                                            LiferayPortletConfig liferayPortletConfig =
075                                                    (LiferayPortletConfig)invokerPortlet.getPortletConfig();
076    
077                                            if (invokerPortlet.isStrutsPortlet() ||
078                                                    liferayPortletConfig.isCopyRequestParameters() ||
079                                                    !liferayPortletConfig.isWARFile()) {
080    
081                                                    request.setAttribute(
082                                                            UploadServletRequestFilter.
083                                                                    COPY_MULTIPART_STREAM_TO_FILE,
084                                                            Boolean.FALSE);
085                                            }
086                                    }
087                            }
088    
089                            uploadServletRequest = PortalUtil.getUploadServletRequest(request);
090                    }
091    
092                    if (uploadServletRequest == null) {
093                            processFilter(
094                                    UploadServletRequestFilter.class.getName(), request, response,
095                                    filterChain);
096                    }
097                    else {
098                            try {
099                                    processFilter(
100                                            UploadServletRequestFilter.class.getName(),
101                                            uploadServletRequest, response, filterChain);
102                            }
103                            finally {
104                                    uploadServletRequest.cleanUp();
105                            }
106                    }
107            }
108    
109    }