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.kernel.portlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.settings.LocalizedValuesMap;
020    import com.liferay.portal.kernel.settings.ModifiableSettings;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.PortletConstants;
027    
028    import java.io.IOException;
029    
030    import java.util.Locale;
031    import java.util.Map;
032    
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletRequest;
035    
036    import javax.servlet.RequestDispatcher;
037    import javax.servlet.ServletContext;
038    import javax.servlet.ServletException;
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Iv??n Zaera
044     */
045    public class BaseJSPSettingsConfigurationAction
046            extends SettingsConfigurationAction
047            implements ConfigurationAction, ResourceServingConfigurationAction {
048    
049            public String getJspPath(HttpServletRequest request) {
050                    PortletConfig selPortletConfig = getSelPortletConfig(request);
051    
052                    String configTemplate = selPortletConfig.getInitParameter(
053                            "config-template");
054    
055                    if (Validator.isNotNull(configTemplate)) {
056                            return configTemplate;
057                    }
058    
059                    String configJSP = selPortletConfig.getInitParameter("config-jsp");
060    
061                    if (Validator.isNotNull(configJSP)) {
062                            return configJSP;
063                    }
064    
065                    return "/configuration.jsp";
066            }
067    
068            @Override
069            public void include(
070                            PortletConfig portletConfig, HttpServletRequest request,
071                            HttpServletResponse response)
072                    throws Exception {
073    
074                    ServletContext servletContext = getServletContext(request);
075    
076                    RequestDispatcher requestDispatcher =
077                            servletContext.getRequestDispatcher(getJspPath(request));
078    
079                    try {
080                            requestDispatcher.include(request, response);
081                    }
082                    catch (ServletException se) {
083                            _log.error("Unable to include JSP " + getJspPath(request), se);
084    
085                            throw new IOException(
086                                    "Unable to include " + getJspPath(request), se);
087                    }
088            }
089    
090            public void setServletContext(ServletContext servletContext) {
091                    _servletContext = servletContext;
092            }
093    
094            protected ServletContext getServletContext(HttpServletRequest request) {
095                    if (_servletContext != null) {
096                            return _servletContext;
097                    }
098    
099                    String portletResource = ParamUtil.getString(
100                            request, "portletResource");
101    
102                    if (Validator.isNotNull(portletResource)) {
103                            String rootPortletId = PortletConstants.getRootPortletId(
104                                    portletResource);
105    
106                            PortletBag portletBag = PortletBagPool.get(rootPortletId);
107    
108                            return portletBag.getServletContext();
109                    }
110    
111                    return (ServletContext)request.getAttribute(WebKeys.CTX);
112            }
113    
114            protected void removeDefaultValue(
115                    PortletRequest portletRequest, ModifiableSettings modifiableSettings,
116                    String key, LocalizedValuesMap localizedMap) {
117    
118                    String defaultValue = localizedMap.getDefaultValue();
119    
120                    Map<Locale, String> localizedMapValues = localizedMap.getValues();
121    
122                    for (Locale locale : localizedMapValues.keySet()) {
123                            String languageKeyId = key + "_" + LocaleUtil.toLanguageId(locale);
124    
125                            String value = getParameter(portletRequest, languageKeyId);
126    
127                            if (defaultValue.equals(value) ||
128                                    StringUtil.equalsIgnoreBreakLine(defaultValue, value)) {
129    
130                                    modifiableSettings.reset(languageKeyId);
131                            }
132                    }
133            }
134    
135            private static final Log _log = LogFactoryUtil.getLog(
136                    BaseJSPSettingsConfigurationAction.class);
137    
138            private ServletContext _servletContext;
139    
140    }