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