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.servlet.filters.absoluteredirects;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.TryFilter;
020    import com.liferay.portal.kernel.servlet.WrapHttpServletResponseFilter;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.service.ServiceContextThreadLocal;
025    import com.liferay.portal.servlet.filters.BasePortalFilter;
026    import com.liferay.portal.util.PortalInstances;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    import javax.servlet.http.HttpSession;
033    
034    /**
035     * <p>
036     * This filter is used to ensure that all redirects are absolute. It should not
037     * be disabled because it also sets the company ID in the request so that
038     * subsequent calls in the thread have the company ID properly set. This filter
039     * should also always be the first filter in the list of filters.
040     * </p>
041     *
042     * @author Minhchau Dang
043     * @author Brian Wing Shun Chan
044     */
045    public class AbsoluteRedirectsFilter
046            extends BasePortalFilter
047            implements TryFilter, WrapHttpServletResponseFilter {
048    
049            @Override
050            public Object doFilterTry(
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    request.setCharacterEncoding(StringPool.UTF8);
055                    //response.setContentType(ContentTypes.TEXT_HTML_UTF8);
056    
057                    // Company id needs to always be called here so that it's properly set
058                    // in subsequent calls
059    
060                    long companyId = PortalInstances.getCompanyId(request);
061    
062                    if (_log.isDebugEnabled()) {
063                            _log.debug("Company id " + companyId);
064                    }
065    
066                    PortalUtil.getCurrentCompleteURL(request);
067                    PortalUtil.getCurrentURL(request);
068    
069                    HttpSession session = request.getSession();
070    
071                    Boolean httpsInitial = (Boolean)session.getAttribute(
072                            WebKeys.HTTPS_INITIAL);
073    
074                    if (httpsInitial == null) {
075                            httpsInitial = Boolean.valueOf(request.isSecure());
076    
077                            session.setAttribute(WebKeys.HTTPS_INITIAL, httpsInitial);
078    
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug("Setting httpsInitial to " + httpsInitial);
081                            }
082                    }
083    
084                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
085                            request);
086    
087                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
088    
089                    return null;
090            }
091    
092            @Override
093            public HttpServletResponse getWrappedHttpServletResponse(
094                    HttpServletRequest request, HttpServletResponse response) {
095    
096                    return new AbsoluteRedirectsResponse(request, response);
097            }
098    
099            @Override
100            public boolean isFilterEnabled() {
101                    return _FILTER_ENABLED;
102            }
103    
104            private static final boolean _FILTER_ENABLED = true;
105    
106            private static Log _log = LogFactoryUtil.getLog(
107                    AbsoluteRedirectsFilter.class);
108    
109    }