1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.iframe.action;
21  
22  import com.liferay.portal.kernel.portlet.ConfigurationAction;
23  import com.liferay.portal.kernel.servlet.SessionMessages;
24  import com.liferay.portal.kernel.util.Constants;
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  /**
38   * <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ConfigurationActionImpl implements ConfigurationAction {
44  
45      public void processAction(
46              PortletConfig portletConfig, ActionRequest actionRequest,
47              ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          if (!cmd.equals(Constants.UPDATE)) {
53              return;
54          }
55  
56          String src = ParamUtil.getString(actionRequest, "src");
57  
58          if (!src.startsWith("/") &&
59              !StringUtil.startsWith(src, "http://") &&
60              !StringUtil.startsWith(src, "https://") &&
61              !StringUtil.startsWith(src, "mhtml://")) {
62  
63              src = HttpUtil.getProtocol(actionRequest) + "://" + src;
64          }
65  
66          boolean relative = ParamUtil.getBoolean(actionRequest, "relative");
67  
68          boolean auth = ParamUtil.getBoolean(actionRequest, "auth");
69          String authType = ParamUtil.getString(actionRequest, "authType");
70          String formMethod = ParamUtil.getString(actionRequest, "formMethod");
71          String userName = ParamUtil.getString(actionRequest, "userName");
72          String password = ParamUtil.getString(actionRequest, "password");
73          String hiddenVariables = ParamUtil.getString(
74              actionRequest, "hiddenVariables");
75  
76          String[] htmlAttributes = StringUtil.split(ParamUtil.getString(
77              actionRequest, "htmlAttributes"), "\n");
78  
79          String portletResource = ParamUtil.getString(
80              actionRequest, "portletResource");
81  
82          PortletPreferences prefs =
83              PortletPreferencesFactoryUtil.getPortletSetup(
84                  actionRequest, portletResource);
85  
86          prefs.setValue("src", src);
87          prefs.setValue("relative", String.valueOf(relative));
88  
89          prefs.setValue("auth", String.valueOf(auth));
90          prefs.setValue("auth-type", authType);
91          prefs.setValue("form-method", formMethod);
92          prefs.setValue("user-name", userName);
93          prefs.setValue("password", password);
94          prefs.setValue("hidden-variables", hiddenVariables);
95  
96          for (int i = 0; i < htmlAttributes.length; i++) {
97              int pos = htmlAttributes[i].indexOf("=");
98  
99              if (pos != -1) {
100                 String key = htmlAttributes[i].substring(0, pos);
101                 String value = htmlAttributes[i].substring(
102                     pos + 1, htmlAttributes[i].length());
103 
104                 prefs.setValue(key, value);
105             }
106         }
107 
108         prefs.store();
109 
110         SessionMessages.add(
111             actionRequest, portletConfig.getPortletName() + ".doConfigure");
112     }
113 
114     public String render(
115             PortletConfig portletConfig, RenderRequest renderRequest,
116             RenderResponse renderResponse)
117         throws Exception {
118 
119         return "/html/portlet/iframe/configuration.jsp";
120     }
121 
122 }