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.portlet.portletconfiguration;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.language.AggregateResourceBundle;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.service.PortletLocalServiceUtil;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portlet.PortletConfigFactoryUtil;
027    import com.liferay.portlet.PortletConfigImpl;
028    import com.liferay.portlet.StrutsPortlet;
029    
030    import java.io.IOException;
031    
032    import java.util.Locale;
033    import java.util.ResourceBundle;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.EventRequest;
038    import javax.portlet.EventResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletException;
041    import javax.portlet.PortletRequest;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    import javax.portlet.ResourceRequest;
045    import javax.portlet.ResourceResponse;
046    
047    import javax.servlet.http.HttpServletRequest;
048    
049    /**
050     * @author Carlos Sierra Andr??s
051     */
052    public class PortletConfigurationPortlet extends StrutsPortlet {
053    
054            @Override
055            public void init(PortletConfig portletConfig) throws PortletException {
056                    if (portletConfig instanceof PortletConfigImpl) {
057                            PortletConfigurationPortletPortletConfig
058                                    portletConfigurationPortletPortletConfig =
059                                            new PortletConfigurationPortletPortletConfig(
060                                                    (PortletConfigImpl)portletConfig);
061    
062                            super.init(portletConfigurationPortletPortletConfig);
063                    }
064                    else {
065                            super.init(portletConfig);
066                    }
067            }
068    
069            @Override
070            public void processAction(
071                            ActionRequest actionRequest, ActionResponse actionResponse)
072                    throws IOException, PortletException {
073    
074                    _portletRequestThreadLocal.set(actionRequest);
075    
076                    actionRequest.setAttribute(
077                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
078    
079                    super.processAction(actionRequest, actionResponse);
080            }
081    
082            @Override
083            public void processEvent(
084                            EventRequest eventRequest, EventResponse eventResponse)
085                    throws IOException, PortletException {
086    
087                    _portletRequestThreadLocal.set(eventRequest);
088    
089                    eventRequest.setAttribute(
090                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
091    
092                    super.processEvent(eventRequest, eventResponse);
093            }
094    
095            @Override
096            public void render(
097                            RenderRequest renderRequest, RenderResponse renderResponse)
098                    throws IOException, PortletException {
099    
100                    _portletRequestThreadLocal.set(renderRequest);
101    
102                    renderRequest.setAttribute(
103                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
104    
105                    super.render(renderRequest, renderResponse);
106            }
107    
108            @Override
109            public void serveResource(
110                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
111                    throws IOException, PortletException {
112    
113                    _portletRequestThreadLocal.set(resourceRequest);
114    
115                    resourceRequest.setAttribute(
116                            JavaConstants.JAVAX_PORTLET_CONFIG, getPortletConfig());
117    
118                    super.serveResource(resourceRequest, resourceResponse);
119            }
120    
121            private static final Log _log = LogFactoryUtil.getLog(
122                    PortletConfigurationPortlet.class);
123    
124            private final ThreadLocal<PortletRequest> _portletRequestThreadLocal =
125                    new AutoResetThreadLocal<PortletRequest>("_portletRequestThreadLocal");
126    
127            private class PortletConfigurationPortletPortletConfig
128                    extends PortletConfigImpl {
129    
130                    @Override
131                    public ResourceBundle getResourceBundle(Locale locale) {
132                            try {
133                                    PortletRequest portletRequest =
134                                            _portletRequestThreadLocal.get();
135    
136                                    long companyId = PortalUtil.getCompanyId(portletRequest);
137    
138                                    String portletResource = ParamUtil.getString(
139                                            portletRequest, "portletResource");
140    
141                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
142                                            companyId, portletResource);
143    
144                                    HttpServletRequest httpServletRequest =
145                                            PortalUtil.getHttpServletRequest(portletRequest);
146    
147                                    PortletConfig portletConfig = PortletConfigFactoryUtil.create(
148                                            portlet, httpServletRequest.getServletContext());
149    
150                                    return new AggregateResourceBundle(
151                                            super.getResourceBundle(locale),
152                                            portletConfig.getResourceBundle(locale));
153                            }
154                            catch (Exception e) {
155                                    _log.error(e, e);
156                            }
157    
158                            return super.getResourceBundle(locale);
159                    }
160    
161                    private PortletConfigurationPortletPortletConfig(
162                            PortletConfigImpl portletConfigImpl) {
163    
164                            super(
165                                    portletConfigImpl.getPortlet(),
166                                    portletConfigImpl.getPortletContext());
167                    }
168    
169            }
170    
171    }