1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.ConfigurationAction;
28  import com.liferay.portal.kernel.portlet.ResourceServingConfigurationAction;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Portlet;
33  import com.liferay.portal.security.auth.PrincipalException;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.security.permission.PermissionChecker;
36  import com.liferay.portal.service.PortletLocalServiceUtil;
37  import com.liferay.portal.service.permission.PortletPermissionUtil;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.PortletPreferencesFactoryUtil;
43  import com.liferay.portlet.portletconfiguration.util.PortletConfigurationUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  import javax.portlet.PortletRequest;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  import javax.portlet.ResourceRequest;
53  import javax.portlet.ResourceResponse;
54  
55  import javax.servlet.ServletContext;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="EditConfigurationAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   */
66  public class EditConfigurationAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          Portlet portlet = null;
74  
75          try {
76              portlet = getPortlet(actionRequest);
77          }
78          catch (PrincipalException pe) {
79              SessionErrors.add(
80                  actionRequest, PrincipalException.class.getName());
81  
82              setForward(actionRequest, "portlet.portlet_configuration.error");
83  
84              return;
85          }
86  
87          ConfigurationAction configurationAction = getConfigurationAction(
88              portlet);
89  
90          if (configurationAction != null) {
91              configurationAction.processAction(
92                  portletConfig, actionRequest, actionResponse);
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         Portlet portlet = null;
102 
103         try {
104             portlet = getPortlet(renderRequest);
105         }
106         catch (PrincipalException pe) {
107             SessionErrors.add(
108                 renderRequest, PrincipalException.class.getName());
109 
110             return mapping.findForward("portlet.portlet_configuration.error");
111         }
112 
113         renderResponse.setTitle(getTitle(portlet, renderRequest));
114 
115         ConfigurationAction configurationAction = getConfigurationAction(
116             portlet);
117 
118         if (configurationAction != null) {
119             String path = configurationAction.render(
120                 portletConfig, renderRequest, renderResponse);
121 
122             if (_log.isDebugEnabled()) {
123                 _log.debug("Configuration action returned render path " + path);
124             }
125 
126             if (Validator.isNotNull(path)) {
127                 renderRequest.setAttribute(
128                     WebKeys.CONFIGURATION_ACTION_PATH, path);
129             }
130             else {
131                 _log.error("Configuration action returned a null path");
132             }
133         }
134 
135         return mapping.findForward(getForward(
136             renderRequest, "portlet.portlet_configuration.edit_configuration"));
137     }
138 
139     public void serveResource(
140             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
142         throws Exception {
143 
144         Portlet portlet = null;
145 
146         try {
147             portlet = getPortlet(resourceRequest);
148         }
149         catch (PrincipalException pe) {
150             return;
151         }
152 
153         ResourceServingConfigurationAction resourceServingConfigurationAction =
154             (ResourceServingConfigurationAction)getConfigurationAction(
155                 portlet);
156 
157         if (resourceServingConfigurationAction != null) {
158             resourceServingConfigurationAction.serveResource(
159                 portletConfig, resourceRequest, resourceResponse);
160         }
161     }
162 
163     protected ConfigurationAction getConfigurationAction(Portlet portlet)
164         throws Exception {
165 
166         if (portlet == null) {
167             return null;
168         }
169 
170         ConfigurationAction configurationAction =
171             portlet.getConfigurationActionInstance();
172 
173         if (configurationAction == null) {
174             _log.error(
175                 "Configuration action for portlet " + portlet.getPortletId() +
176                     " is null");
177         }
178 
179         return configurationAction;
180     }
181 
182     protected Portlet getPortlet(PortletRequest portletRequest)
183         throws Exception {
184 
185         long companyId = PortalUtil.getCompanyId(portletRequest);
186 
187         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
188             WebKeys.THEME_DISPLAY);
189 
190         PermissionChecker permissionChecker =
191             themeDisplay.getPermissionChecker();
192 
193         String portletId = ParamUtil.getString(
194             portletRequest, "portletResource");
195 
196         if (!PortletPermissionUtil.contains(
197                 permissionChecker, themeDisplay.getPlid(), portletId,
198                 ActionKeys.CONFIGURATION)) {
199 
200             throw new PrincipalException();
201         }
202 
203         return PortletLocalServiceUtil.getPortletById(companyId, portletId);
204     }
205 
206     protected String getTitle(Portlet portlet, RenderRequest renderRequest)
207         throws Exception {
208 
209         ServletContext servletContext =
210             (ServletContext)renderRequest.getAttribute(WebKeys.CTX);
211 
212         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
213             WebKeys.THEME_DISPLAY);
214 
215         PortletPreferences portletSetup =
216             PortletPreferencesFactoryUtil.getPortletSetup(
217                 renderRequest, portlet.getPortletId());
218 
219         String title = PortletConfigurationUtil.getPortletTitle(
220             portletSetup, themeDisplay.getLanguageId());
221 
222         if (Validator.isNull(title)) {
223             title = PortalUtil.getPortletTitle(
224                 portlet, servletContext, themeDisplay.getLocale());
225         }
226 
227         return title;
228     }
229 
230     private static Log _log =
231         LogFactoryUtil.getLog(EditConfigurationAction.class);
232 
233 }