1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.iframe.action;
24  
25  import com.liferay.portal.kernel.portlet.ConfigurationAction;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.util.Http;
31  import com.liferay.util.servlet.SessionMessages;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.PortletPreferences;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  /**
41   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ConfigurationActionImpl implements ConfigurationAction {
47  
48      public void processAction(
49              PortletConfig config, ActionRequest req, ActionResponse res)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(req, Constants.CMD);
53  
54          if (!cmd.equals(Constants.UPDATE)) {
55              return;
56          }
57  
58          String src = ParamUtil.getString(req, "src");
59  
60          if (!src.startsWith("/") &&
61              !StringUtil.startsWith(src, "http://") &&
62              !StringUtil.startsWith(src, "https://") &&
63              !StringUtil.startsWith(src, "mhtml://")) {
64  
65              src = Http.getProtocol(req) + "://" + src;
66          }
67  
68          boolean relative = ParamUtil.getBoolean(req, "relative");
69  
70          boolean auth = ParamUtil.getBoolean(req, "auth");
71          String authType = ParamUtil.getString(req, "authType");
72          String formMethod = ParamUtil.getString(req, "formMethod");
73          String userName = ParamUtil.getString(req, "userName");
74          String password = ParamUtil.getString(req, "password");
75          String hiddenVariables = ParamUtil.getString(req, "hiddenVariables");
76  
77          String[] htmlAttributes = StringUtil.split(ParamUtil.getString(
78              req, "htmlAttributes"), "\n");
79  
80          String portletResource = ParamUtil.getString(
81              req, "portletResource");
82  
83          PortletPreferences prefs =
84              PortletPreferencesFactoryUtil.getPortletSetup(
85                  req, portletResource, true, true);
86  
87          prefs.setValue("src", src);
88          prefs.setValue("relative", String.valueOf(relative));
89  
90          prefs.setValue("auth", String.valueOf(auth));
91          prefs.setValue("auth-type", authType);
92          prefs.setValue("form-method", formMethod);
93          prefs.setValue("user-name", userName);
94          prefs.setValue("password", password);
95          prefs.setValue("hidden-variables", hiddenVariables);
96  
97          for (int i = 0; i < htmlAttributes.length; i++) {
98              int pos = htmlAttributes[i].indexOf("=");
99  
100             if (pos != -1) {
101                 String key = htmlAttributes[i].substring(0, pos);
102                 String value = htmlAttributes[i].substring(
103                     pos + 1, htmlAttributes[i].length());
104 
105                 prefs.setValue(key, value);
106             }
107         }
108 
109         prefs.store();
110 
111         SessionMessages.add(req, config.getPortletName() + ".doConfigure");
112     }
113 
114     public String render(
115             PortletConfig config, RenderRequest req, RenderResponse res)
116         throws Exception {
117 
118         return "/html/portlet/iframe/configuration.jsp";
119     }
120 
121 }