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