1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.iframe.action;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.Portlet;
31  import com.liferay.portal.service.PortletLocalServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  
37  import javax.portlet.PortletConfig;
38  import javax.portlet.PortletPreferences;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class ViewAction extends PortletAction {
53  
54      public ActionForward render(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              RenderRequest renderRequest, RenderResponse renderResponse)
57          throws Exception {
58  
59          String src = transformSrc(renderRequest, renderResponse);
60  
61          if (Validator.isNull(src)) {
62              return mapping.findForward("/portal/portlet_not_setup");
63          }
64  
65          renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
66  
67          return mapping.findForward("portlet.iframe.view");
68      }
69  
70      protected String getSrc(
71          RenderRequest renderRequest, RenderResponse renderResponse) {
72  
73          PortletPreferences preferences = renderRequest.getPreferences();
74  
75          String src = preferences.getValue("src", StringPool.BLANK);
76  
77          src = ParamUtil.getString(renderRequest, "src", src);
78  
79          return src;
80      }
81  
82      protected String getUserName(
83          RenderRequest renderRequest, RenderResponse renderResponse) {
84  
85          PortletPreferences preferences = renderRequest.getPreferences();
86  
87          String userName = preferences.getValue("user-name", StringPool.BLANK);
88  
89          if (Validator.isNull(userName)) {
90              userName = renderRequest.getRemoteUser();
91          }
92  
93          return userName;
94      }
95  
96      protected String getPassword(
97          RenderRequest renderRequest, RenderResponse renderResponse) {
98  
99          PortletPreferences preferences = renderRequest.getPreferences();
100 
101         String password = preferences.getValue("password", StringPool.BLANK);
102 
103         if (Validator.isNull(password)) {
104             password = PortalUtil.getUserPassword(renderRequest);
105         }
106 
107         return password;
108     }
109 
110     protected String transformSrc(
111             RenderRequest renderRequest, RenderResponse renderResponse)
112         throws SystemException {
113 
114         PortletPreferences preferences = renderRequest.getPreferences();
115 
116         String src = getSrc(renderRequest, renderResponse);
117 
118         boolean auth = GetterUtil.getBoolean(
119             preferences.getValue("auth", StringPool.BLANK));
120         String authType = preferences.getValue("auth-type", StringPool.BLANK);
121         String userName = getUserName(renderRequest, renderResponse);
122         String password = getPassword(renderRequest, renderResponse);
123 
124         if (auth) {
125             if (authType.equals("basic")) {
126                 int pos = src.indexOf("://");
127 
128                 String protocol = src.substring(0, pos + 3);
129                 String url = src.substring(pos + 3, src.length());
130 
131                 src =
132                     protocol + userName + ":" + password +
133                     "@" + url;
134             }
135             else {
136                 ThemeDisplay themeDisplay =
137                     (ThemeDisplay)renderRequest.getAttribute(
138                         WebKeys.THEME_DISPLAY);
139 
140                 String portletId = PortalUtil.getPortletId(renderRequest);
141 
142                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
143                     themeDisplay.getCompanyId(), portletId);
144 
145                 src =
146                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
147                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
148                             portletId;
149             }
150         }
151 
152         return src;
153     }
154 
155 }