001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.iframe.action;
016    
017    import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.PortletPreferences;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class ConfigurationActionImpl extends BaseConfigurationAction {
037    
038            public void processAction(
039                            PortletConfig portletConfig, ActionRequest actionRequest,
040                            ActionResponse actionResponse)
041                    throws Exception {
042    
043                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
044    
045                    if (!cmd.equals(Constants.UPDATE)) {
046                            return;
047                    }
048    
049                    String src = ParamUtil.getString(actionRequest, "src");
050    
051                    if (!src.startsWith("/") &&
052                            !StringUtil.startsWith(src, "http://") &&
053                            !StringUtil.startsWith(src, "https://") &&
054                            !StringUtil.startsWith(src, "mhtml://")) {
055    
056                            src = HttpUtil.getProtocol(actionRequest) + "://" + src;
057                    }
058    
059                    boolean relative = ParamUtil.getBoolean(actionRequest, "relative");
060    
061                    boolean auth = ParamUtil.getBoolean(actionRequest, "auth");
062                    String authType = ParamUtil.getString(actionRequest, "authType");
063                    String formMethod = ParamUtil.getString(actionRequest, "formMethod");
064                    String userName = ParamUtil.getString(actionRequest, "userName");
065                    String userNameField = ParamUtil.getString(
066                            actionRequest, "userNameField");
067                    String password = ParamUtil.getString(actionRequest, "password");
068                    String passwordField = ParamUtil.getString(
069                            actionRequest, "passwordField");
070                    String hiddenVariables = ParamUtil.getString(
071                            actionRequest, "hiddenVariables");
072    
073                    String[] htmlAttributes = StringUtil.split(ParamUtil.getString(
074                            actionRequest, "htmlAttributes"), StringPool.NEW_LINE);
075    
076                    String portletResource = ParamUtil.getString(
077                            actionRequest, "portletResource");
078    
079                    PortletPreferences preferences =
080                            PortletPreferencesFactoryUtil.getPortletSetup(
081                                    actionRequest, portletResource);
082    
083                    preferences.setValue("src", src);
084                    preferences.setValue("relative", String.valueOf(relative));
085    
086                    preferences.setValue("auth", String.valueOf(auth));
087                    preferences.setValue("auth-type", authType);
088                    preferences.setValue("form-method", formMethod);
089                    preferences.setValue("user-name", userName);
090                    preferences.setValue("user-name-field", userNameField);
091                    preferences.setValue("password", password);
092                    preferences.setValue("password-field", passwordField);
093                    preferences.setValue("hidden-variables", hiddenVariables);
094    
095                    for (String htmlAttribute : htmlAttributes) {
096                            int pos = htmlAttribute.indexOf(StringPool.EQUAL);
097    
098                            if (pos == -1) {
099                                    continue;
100                            }
101    
102                            String key = htmlAttribute.substring(0, pos);
103                            String value = htmlAttribute.substring(
104                                    pos + 1, htmlAttribute.length());
105    
106                            preferences.setValue(key, value);
107                    }
108    
109                    preferences.store();
110    
111                    SessionMessages.add(
112                            actionRequest, portletConfig.getPortletName() + ".doConfigure");
113            }
114    
115            public String render(
116                            PortletConfig portletConfig, RenderRequest renderRequest,
117                            RenderResponse renderResponse)
118                    throws Exception {
119    
120                    return "/html/portlet/iframe/configuration.jsp";
121            }
122    
123    }