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.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            public ActionForward render(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            RenderRequest renderRequest, RenderResponse renderResponse)
048                    throws Exception {
049    
050                    String src = transformSrc(renderRequest, renderResponse);
051    
052                    if (Validator.isNull(src)) {
053                            return mapping.findForward("/portal/portlet_not_setup");
054                    }
055    
056                    renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
057    
058                    return mapping.findForward("portlet.iframe.view");
059            }
060    
061            protected String getSrc(
062                    RenderRequest renderRequest, RenderResponse renderResponse) {
063    
064                    PortletPreferences preferences = renderRequest.getPreferences();
065    
066                    String src = preferences.getValue("src", StringPool.BLANK);
067    
068                    src = ParamUtil.getString(renderRequest, "src", src);
069    
070                    return src;
071            }
072    
073            protected String getUserName(
074                            RenderRequest renderRequest, RenderResponse renderResponse)
075                    throws PortalException, SystemException {
076    
077                    PortletPreferences preferences = renderRequest.getPreferences();
078                    String userName = preferences.getValue("user-name", StringPool.BLANK);
079    
080                    return IFrameUtil.getUserName(renderRequest, userName);
081            }
082    
083            protected String getPassword(
084                            RenderRequest renderRequest, RenderResponse renderResponse)
085                    throws PortalException, SystemException {
086    
087                    PortletPreferences preferences = renderRequest.getPreferences();
088                    String password = preferences.getValue("password", StringPool.BLANK);
089    
090                    return IFrameUtil.getPassword(renderRequest, password);
091            }
092    
093            protected String transformSrc(
094                            RenderRequest renderRequest, RenderResponse renderResponse)
095                    throws PortalException, SystemException {
096    
097                    PortletPreferences preferences = renderRequest.getPreferences();
098    
099                    String src = getSrc(renderRequest, renderResponse);
100    
101                    boolean auth = GetterUtil.getBoolean(
102                            preferences.getValue("auth", StringPool.BLANK));
103                    String authType = preferences.getValue("auth-type", StringPool.BLANK);
104                    String userName = getUserName(renderRequest, renderResponse);
105                    String password = getPassword(renderRequest, renderResponse);
106    
107                    if (auth) {
108                            if (authType.equals("basic")) {
109                                    int pos = src.indexOf("://");
110    
111                                    String protocol = src.substring(0, pos + 3);
112                                    String url = src.substring(pos + 3, src.length());
113    
114                                    src =
115                                            protocol + userName + ":" + password +
116                                            "@" + url;
117                            }
118                            else {
119                                    ThemeDisplay themeDisplay =
120                                            (ThemeDisplay)renderRequest.getAttribute(
121                                                    WebKeys.THEME_DISPLAY);
122    
123                                    String portletId = PortalUtil.getPortletId(renderRequest);
124    
125                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
126                                            themeDisplay.getCompanyId(), portletId);
127    
128                                    src =
129                                            themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
130                                                    "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
131                                                            portletId;
132                            }
133                    }
134    
135                    return src;
136            }
137    
138    }