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 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    
071                    String password = preferences.getValue(
072                            "basicPassword", StringPool.BLANK);
073    
074                    return IFrameUtil.getPassword(renderRequest, password);
075            }
076    
077            protected String getSrc(
078                    RenderRequest renderRequest, RenderResponse renderResponse) {
079    
080                    PortletPreferences preferences = renderRequest.getPreferences();
081    
082                    String src = preferences.getValue("src", StringPool.BLANK);
083    
084                    src = ParamUtil.getString(renderRequest, "src", src);
085    
086                    return src;
087            }
088    
089            protected String getUserName(
090                            RenderRequest renderRequest, RenderResponse renderResponse)
091                    throws PortalException, SystemException {
092    
093                    PortletPreferences preferences = renderRequest.getPreferences();
094    
095                    String userName = preferences.getValue(
096                            "basicUserName", StringPool.BLANK);
097    
098                    return IFrameUtil.getUserName(renderRequest, userName);
099            }
100    
101            protected String transformSrc(
102                            RenderRequest renderRequest, RenderResponse renderResponse)
103                    throws PortalException, SystemException {
104    
105                    PortletPreferences preferences = renderRequest.getPreferences();
106    
107                    String src = getSrc(renderRequest, renderResponse);
108    
109                    boolean auth = GetterUtil.getBoolean(
110                            preferences.getValue("auth", StringPool.BLANK));
111    
112                    if (auth) {
113                            String authType = preferences.getValue(
114                                    "authType", StringPool.BLANK);
115    
116                            if (authType.equals("basic")) {
117                                    String userName = getUserName(renderRequest, renderResponse);
118                                    String password = getPassword(renderRequest, renderResponse);
119    
120                                    int pos = src.indexOf("://");
121    
122                                    String protocol = src.substring(0, pos + 3);
123                                    String url = src.substring(pos + 3);
124    
125                                    src = protocol + userName + ":" + password + "@" + url;
126                            }
127                            else {
128                                    ThemeDisplay themeDisplay =
129                                            (ThemeDisplay)renderRequest.getAttribute(
130                                                    WebKeys.THEME_DISPLAY);
131    
132                                    String portletId = PortalUtil.getPortletId(renderRequest);
133    
134                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
135                                            themeDisplay.getCompanyId(), portletId);
136    
137                                    src =
138                                            themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
139                                                    "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
140                                                            portletId;
141                            }
142                    }
143    
144                    return src;
145            }
146    
147    }