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.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 = protocol + userName + ":" + password + "@" + url;
116                            }
117                            else {
118                                    ThemeDisplay themeDisplay =
119                                            (ThemeDisplay)renderRequest.getAttribute(
120                                                    WebKeys.THEME_DISPLAY);
121    
122                                    String portletId = PortalUtil.getPortletId(renderRequest);
123    
124                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
125                                            themeDisplay.getCompanyId(), portletId);
126    
127                                    src =
128                                            themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
129                                                    "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
130                                                            portletId;
131                            }
132                    }
133    
134                    return src;
135            }
136    
137    }