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