1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.iframe.action;
16  
17  import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
18  import com.liferay.portal.kernel.servlet.SessionMessages;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletPreferences;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  /**
34   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class ConfigurationActionImpl extends BaseConfigurationAction {
39  
40      public void processAction(
41              PortletConfig portletConfig, ActionRequest actionRequest,
42              ActionResponse actionResponse)
43          throws Exception {
44  
45          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
46  
47          if (!cmd.equals(Constants.UPDATE)) {
48              return;
49          }
50  
51          String src = ParamUtil.getString(actionRequest, "src");
52  
53          if (!src.startsWith("/") &&
54              !StringUtil.startsWith(src, "http://") &&
55              !StringUtil.startsWith(src, "https://") &&
56              !StringUtil.startsWith(src, "mhtml://")) {
57  
58              src = HttpUtil.getProtocol(actionRequest) + "://" + src;
59          }
60  
61          boolean relative = ParamUtil.getBoolean(actionRequest, "relative");
62  
63          boolean auth = ParamUtil.getBoolean(actionRequest, "auth");
64          String authType = ParamUtil.getString(actionRequest, "authType");
65          String formMethod = ParamUtil.getString(actionRequest, "formMethod");
66          String userName = ParamUtil.getString(actionRequest, "userName");
67          String userNameField = ParamUtil.getString(
68              actionRequest, "userNameField");
69          String password = ParamUtil.getString(actionRequest, "password");
70          String passwordField = ParamUtil.getString(
71              actionRequest, "passwordField");
72          String hiddenVariables = ParamUtil.getString(
73              actionRequest, "hiddenVariables");
74  
75          String[] htmlAttributes = StringUtil.split(ParamUtil.getString(
76              actionRequest, "htmlAttributes"), StringPool.NEW_LINE);
77  
78          String portletResource = ParamUtil.getString(
79              actionRequest, "portletResource");
80  
81          PortletPreferences preferences =
82              PortletPreferencesFactoryUtil.getPortletSetup(
83                  actionRequest, portletResource);
84  
85          preferences.setValue("src", src);
86          preferences.setValue("relative", String.valueOf(relative));
87  
88          preferences.setValue("auth", String.valueOf(auth));
89          preferences.setValue("auth-type", authType);
90          preferences.setValue("form-method", formMethod);
91          preferences.setValue("user-name", userName);
92          preferences.setValue("user-name-field", userNameField);
93          preferences.setValue("password", password);
94          preferences.setValue("password-field", passwordField);
95          preferences.setValue("hidden-variables", hiddenVariables);
96  
97          for (String htmlAttribute : htmlAttributes) {
98              int pos = htmlAttribute.indexOf(StringPool.EQUAL);
99  
100             if (pos == -1) {
101                 continue;
102             }
103 
104             String key = htmlAttribute.substring(0, pos);
105             String value = htmlAttribute.substring(
106                 pos + 1, htmlAttribute.length());
107 
108             preferences.setValue(key, value);
109         }
110 
111         preferences.store();
112 
113         SessionMessages.add(
114             actionRequest, portletConfig.getPortletName() + ".doConfigure");
115     }
116 
117     public String render(
118             PortletConfig portletConfig, RenderRequest renderRequest,
119             RenderResponse renderResponse)
120         throws Exception {
121 
122         return "/html/portlet/iframe/configuration.jsp";
123     }
124 
125 }