001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.iframe.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Http;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.service.PortletLocalServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.iframe.util.IFrameUtil;
032    
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletPreferences;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ViewAction extends PortletAction {
046    
047            @Override
048            public ActionForward render(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, RenderRequest renderRequest,
051                            RenderResponse renderResponse)
052                    throws Exception {
053    
054                    String src = transformSrc(renderRequest, renderResponse);
055    
056                    if (Validator.isNull(src) || src.equals(Http.HTTP_WITH_SLASH) ||
057                            src.equals(Http.HTTPS_WITH_SLASH)) {
058    
059                            return actionMapping.findForward("/portal/portlet_not_setup");
060                    }
061    
062                    renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
063    
064                    return actionMapping.findForward("portlet.iframe.view");
065            }
066    
067            protected String getPassword(
068                            RenderRequest renderRequest, RenderResponse renderResponse)
069                    throws PortalException, SystemException {
070    
071                    PortletPreferences portletPreferences = renderRequest.getPreferences();
072    
073                    String password = portletPreferences.getValue(
074                            "basicPassword", StringPool.BLANK);
075    
076                    return IFrameUtil.getPassword(renderRequest, password);
077            }
078    
079            protected String getSrc(
080                    RenderRequest renderRequest, RenderResponse renderResponse) {
081    
082                    PortletPreferences portletPreferences = renderRequest.getPreferences();
083    
084                    String src = portletPreferences.getValue("src", StringPool.BLANK);
085    
086                    src = ParamUtil.getString(renderRequest, "src", src);
087    
088                    return src;
089            }
090    
091            protected String getUserName(
092                            RenderRequest renderRequest, RenderResponse renderResponse)
093                    throws PortalException, SystemException {
094    
095                    PortletPreferences portletPreferences = renderRequest.getPreferences();
096    
097                    String userName = portletPreferences.getValue(
098                            "basicUserName", StringPool.BLANK);
099    
100                    return IFrameUtil.getUserName(renderRequest, userName);
101            }
102    
103            protected String transformSrc(
104                            RenderRequest renderRequest, RenderResponse renderResponse)
105                    throws PortalException, SystemException {
106    
107                    PortletPreferences portletPreferences = renderRequest.getPreferences();
108    
109                    String src = getSrc(renderRequest, renderResponse);
110    
111                    boolean auth = GetterUtil.getBoolean(
112                            portletPreferences.getValue("auth", StringPool.BLANK));
113    
114                    if (!auth) {
115                            return src;
116                    }
117    
118                    String authType = portletPreferences.getValue(
119                            "authType", StringPool.BLANK);
120    
121                    if (authType.equals("basic")) {
122                            String userName = getUserName(renderRequest, renderResponse);
123                            String password = getPassword(renderRequest, renderResponse);
124    
125                            int pos = src.indexOf("://");
126    
127                            String protocol = src.substring(0, pos + 3);
128                            String url = src.substring(pos + 3);
129    
130                            return protocol + userName + ":" + password + "@" + url;
131                    }
132    
133                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
134                            WebKeys.THEME_DISPLAY);
135    
136                    String portletId = PortalUtil.getPortletId(renderRequest);
137    
138                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
139                            themeDisplay.getCompanyId(), portletId);
140    
141                    StringBundler sb = new StringBundler(8);
142    
143                    sb.append(themeDisplay.getPortalURL());
144                    sb.append(themeDisplay.getPathMain());
145                    sb.append(StringPool.SLASH);
146                    sb.append(portlet.getStrutsPath());
147                    sb.append("/proxy?p_l_id=");
148                    sb.append(themeDisplay.getPlid());
149                    sb.append("&p_p_id=");
150                    sb.append(portletId);
151    
152                    return sb.toString();
153            }
154    
155    }