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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.iframe.util.IFrameUtil;
38  
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class ViewAction extends PortletAction {
55  
56      public ActionForward render(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              RenderRequest renderRequest, RenderResponse renderResponse)
59          throws Exception {
60  
61          String src = transformSrc(renderRequest, renderResponse);
62  
63          if (Validator.isNull(src)) {
64              return mapping.findForward("/portal/portlet_not_setup");
65          }
66  
67          renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
68  
69          return mapping.findForward("portlet.iframe.view");
70      }
71  
72      protected String getSrc(
73          RenderRequest renderRequest, RenderResponse renderResponse) {
74  
75          PortletPreferences preferences = renderRequest.getPreferences();
76  
77          String src = preferences.getValue("src", StringPool.BLANK);
78  
79          src = ParamUtil.getString(renderRequest, "src", src);
80  
81          return src;
82      }
83  
84      protected String getUserName(
85              RenderRequest renderRequest, RenderResponse renderResponse)
86          throws PortalException, SystemException {
87  
88          PortletPreferences preferences = renderRequest.getPreferences();
89          String userName = preferences.getValue("user-name", StringPool.BLANK);
90  
91          return IFrameUtil.getUserName(renderRequest, userName);
92      }
93  
94      protected String getPassword(
95          RenderRequest renderRequest, RenderResponse renderResponse) {
96  
97          PortletPreferences preferences = renderRequest.getPreferences();
98          String password = preferences.getValue("password", StringPool.BLANK);
99  
100         return IFrameUtil.getPassword(renderRequest, password);
101     }
102 
103     protected String transformSrc(
104             RenderRequest renderRequest, RenderResponse renderResponse)
105         throws PortalException, SystemException {
106 
107         PortletPreferences preferences = renderRequest.getPreferences();
108 
109         String src = getSrc(renderRequest, renderResponse);
110 
111         boolean auth = GetterUtil.getBoolean(
112             preferences.getValue("auth", StringPool.BLANK));
113         String authType = preferences.getValue("auth-type", StringPool.BLANK);
114         String userName = getUserName(renderRequest, renderResponse);
115         String password = getPassword(renderRequest, renderResponse);
116 
117         if (auth) {
118             if (authType.equals("basic")) {
119                 int pos = src.indexOf("://");
120 
121                 String protocol = src.substring(0, pos + 3);
122                 String url = src.substring(pos + 3, src.length());
123 
124                 src =
125                     protocol + userName + ":" + password +
126                     "@" + url;
127             }
128             else {
129                 ThemeDisplay themeDisplay =
130                     (ThemeDisplay)renderRequest.getAttribute(
131                         WebKeys.THEME_DISPLAY);
132 
133                 String portletId = PortalUtil.getPortletId(renderRequest);
134 
135                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
136                     themeDisplay.getCompanyId(), portletId);
137 
138                 src =
139                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
140                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
141                             portletId;
142             }
143         }
144 
145         return src;
146     }
147 
148 }