001
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
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 private PortletConfigurationPortletPortletConfig(
131 PortletConfigImpl portletConfigImpl) {
132
133 super(
134 portletConfigImpl.getPortlet(),
135 portletConfigImpl.getPortletContext());
136 }
137
138 @Override
139 public ResourceBundle getResourceBundle(Locale locale) {
140 try {
141 PortletRequest portletRequest =
142 _portletRequestThreadLocal.get();
143
144 long companyId = PortalUtil.getCompanyId(portletRequest);
145
146 String portletResource = ParamUtil.getString(
147 portletRequest, "portletResource");
148
149 Portlet portlet = PortletLocalServiceUtil.getPortletById(
150 companyId, portletResource);
151
152 HttpServletRequest httpServletRequest =
153 PortalUtil.getHttpServletRequest(portletRequest);
154
155 PortletConfig portletConfig = PortletConfigFactoryUtil.create(
156 portlet, httpServletRequest.getServletContext());
157
158 return new AggregateResourceBundle(
159 super.getResourceBundle(locale),
160 portletConfig.getResourceBundle(locale));
161 }
162 catch (Exception e) {
163 _log.error(e, e);
164 }
165
166 return super.getResourceBundle(locale);
167 }
168 }
169
170 }