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 preferences = renderRequest.getPreferences();
071    
072                    String password = preferences.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 preferences = renderRequest.getPreferences();
082    
083                    String src = preferences.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 preferences = renderRequest.getPreferences();
095    
096                    String userName = preferences.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 preferences = renderRequest.getPreferences();
107    
108                    String src = getSrc(renderRequest, renderResponse);
109    
110                    boolean auth = GetterUtil.getBoolean(
111                            preferences.getValue("auth", StringPool.BLANK));
112    
113                    if (!auth) {
114                            return src;
115                    }
116    
117                    String authType = preferences.getValue("authType", StringPool.BLANK);
118    
119                    if (authType.equals("basic")) {
120                            String userName = getUserName(renderRequest, renderResponse);
121                            String password = getPassword(renderRequest, renderResponse);
122    
123                            int pos = src.indexOf("://");
124    
125                            String protocol = src.substring(0, pos + 3);
126                            String url = src.substring(pos + 3);
127    
128                            src = protocol + userName + ":" + password + "@" + url;
129                    }
130                    else {
131                            ThemeDisplay themeDisplay =
132                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
133    
134                            String portletId = PortalUtil.getPortletId(renderRequest);
135    
136                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
137                                    themeDisplay.getCompanyId(), portletId);
138    
139                            src =
140                                    themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
141                                            "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
142                                                    portletId;
143                    }
144    
145                    return src;
146            }
147    
148    }