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.servletcontextinclude;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.model.Layout;
020    import com.liferay.portal.kernel.model.LayoutSet;
021    import com.liferay.portal.kernel.model.Theme;
022    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.kernel.service.ThemeLocalServiceUtil;
024    import com.liferay.portal.kernel.theme.ThemeDisplay;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.PortalUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.servlet.filters.BasePortalFilter;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.taglib.util.ThemeUtil;
032    
033    import javax.servlet.FilterChain;
034    import javax.servlet.FilterConfig;
035    import javax.servlet.RequestDispatcher;
036    import javax.servlet.ServletContext;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class ServletContextIncludeFilter extends BasePortalFilter {
044    
045            @Override
046            public boolean isFilterEnabled() {
047                    if (super.isFilterEnabled() && PropsValues.THEME_JSP_OVERRIDE_ENABLED) {
048                            return true;
049                    }
050    
051                    return false;
052            }
053    
054            @Override
055            public boolean isFilterEnabled(
056                    HttpServletRequest request, HttpServletResponse response) {
057    
058                    try {
059                            Theme theme = getTheme(request);
060    
061                            if (theme == null) {
062                                    return false;
063                            }
064    
065                            Boolean strict = (Boolean)request.getAttribute(
066                                    WebKeys.SERVLET_CONTEXT_INCLUDE_FILTER_STRICT);
067    
068                            if ((strict != null) && strict) {
069                                    return false;
070                            }
071    
072                            FilterConfig filterConfig = getFilterConfig();
073    
074                            ServletContext servletContext = filterConfig.getServletContext();
075    
076                            String portletId = ThemeUtil.getPortletId(request);
077    
078                            String uri = (String)request.getAttribute(
079                                    WebKeys.INVOKER_FILTER_URI);
080    
081                            if (theme.resourceExists(servletContext, portletId, uri)) {
082                                    request.setAttribute(
083                                            WebKeys.SERVLET_CONTEXT_INCLUDE_FILTER_PATH, uri);
084                                    request.setAttribute(
085                                            WebKeys.SERVLET_CONTEXT_INCLUDE_FILTER_THEME, theme);
086    
087                                    return true;
088                            }
089                    }
090                    catch (Exception e) {
091                            _log.error(e, e);
092                    }
093    
094                    return false;
095            }
096    
097            protected Theme getTheme(HttpServletRequest request) throws Exception {
098                    String themeId = ParamUtil.getString(request, "themeId");
099    
100                    if (Validator.isNotNull(themeId)) {
101                            long companyId = PortalUtil.getCompanyId(request);
102    
103                            return ThemeLocalServiceUtil.getTheme(companyId, themeId);
104                    }
105    
106                    long plid = ParamUtil.getLong(request, "plid");
107    
108                    if (plid <= 0) {
109                            plid = ParamUtil.getLong(request, "p_l_id");
110                    }
111    
112                    if (plid > 0) {
113                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
114    
115                            return layout.getTheme();
116                    }
117    
118                    Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
119    
120                    if (theme != null) {
121                            return theme;
122                    }
123    
124                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
125                            WebKeys.THEME_DISPLAY);
126    
127                    if (themeDisplay != null) {
128                            return themeDisplay.getTheme();
129                    }
130    
131                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
132                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
133    
134                    if (layoutSet != null) {
135                            return layoutSet.getTheme();
136                    }
137    
138                    return null;
139            }
140    
141            @Override
142            protected void processFilter(
143                            HttpServletRequest request, HttpServletResponse response,
144                            FilterChain filterChain)
145                    throws Exception {
146    
147                    Theme theme = (Theme)request.getAttribute(
148                            WebKeys.SERVLET_CONTEXT_INCLUDE_FILTER_THEME);
149    
150                    request.setAttribute(WebKeys.THEME, theme);
151    
152                    FilterConfig filterConfig = getFilterConfig();
153    
154                    ServletContext servletContext = filterConfig.getServletContext();
155    
156                    RequestDispatcher requestDispatcher =
157                            servletContext.getRequestDispatcher(
158                                    "/WEB-INF/jsp/_servlet_context_include.jsp");
159    
160                    requestDispatcher.include(request, response);
161            }
162    
163            private static final Log _log = LogFactoryUtil.getLog(
164                    ServletContextIncludeFilter.class);
165    
166    }