001    /**
002     * Copyright (c) 2000-2013 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.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.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.iframe.util.IFrameUtil;
031    
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletPreferences;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class ViewAction extends PortletAction {
045    
046            @Override
047            public ActionForward render(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, RenderRequest renderRequest,
050                            RenderResponse renderResponse)
051                    throws Exception {
052    
053                    String src = transformSrc(renderRequest, renderResponse);
054    
055                    if (Validator.isNull(src) || src.equals(Http.HTTP_WITH_SLASH) ||
056                            src.equals(Http.HTTPS_WITH_SLASH)) {
057    
058                            return actionMapping.findForward("/portal/portlet_not_setup");
059                    }
060    
061                    renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
062    
063                    return actionMapping.findForward("portlet.iframe.view");
064            }
065    
066            protected String getPassword(
067                            RenderRequest renderRequest, RenderResponse renderResponse)
068                    throws PortalException, SystemException {
069    
070                    PortletPreferences portletPreferences = renderRequest.getPreferences();
071    
072                    String password = portletPreferences.getValue(
073                            "basicPassword", StringPool.BLANK);
074    
075                    return IFrameUtil.getPassword(renderRequest, password);
076            }
077    
078            protected String getSrc(
079                    RenderRequest renderRequest, RenderResponse renderResponse) {
080    
081                    PortletPreferences portletPreferences = renderRequest.getPreferences();
082    
083                    String src = portletPreferences.getValue("src", StringPool.BLANK);
084    
085                    src = ParamUtil.getString(renderRequest, "src", src);
086    
087                    return src;
088            }
089    
090            protected String getUserName(
091                            RenderRequest renderRequest, RenderResponse renderResponse)
092                    throws PortalException, SystemException {
093    
094                    PortletPreferences portletPreferences = renderRequest.getPreferences();
095    
096                    String userName = portletPreferences.getValue(
097                            "basicUserName", StringPool.BLANK);
098    
099                    return IFrameUtil.getUserName(renderRequest, userName);
100            }
101    
102            protected String transformSrc(
103                            RenderRequest renderRequest, RenderResponse renderResponse)
104                    throws PortalException, SystemException {
105    
106                    PortletPreferences portletPreferences = renderRequest.getPreferences();
107    
108                    String src = getSrc(renderRequest, renderResponse);
109    
110                    boolean auth = GetterUtil.getBoolean(
111                            portletPreferences.getValue("auth", StringPool.BLANK));
112    
113                    if (!auth) {
114                            return src;
115                    }
116    
117                    String authType = portletPreferences.getValue(
118                            "authType", StringPool.BLANK);
119    
120                    if (authType.equals("basic")) {
121                            String userName = getUserName(renderRequest, renderResponse);
122                            String password = getPassword(renderRequest, renderResponse);
123    
124                            int pos = src.indexOf("://");
125    
126                            String protocol = src.substring(0, pos + 3);
127                            String url = src.substring(pos + 3);
128    
129                            src = protocol + userName + ":" + password + "@" + url;
130                    }
131                    else {
132                            ThemeDisplay themeDisplay =
133                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
134    
135                            String portletId = PortalUtil.getPortletId(renderRequest);
136    
137                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
138                                    themeDisplay.getCompanyId(), portletId);
139    
140                            src =
141                                    themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
142                                            "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
143                                                    portletId;
144                    }
145    
146                    return src;
147            }
148    
149    }