1   /**
2    * Copyright (c) 2000-2008 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.PortletConfigImpl;
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 config,
58              RenderRequest req, RenderResponse res)
59          throws Exception {
60  
61          String src = transformSrc(config, req, res);
62  
63          if (Validator.isNull(src)) {
64              return mapping.findForward("/portal/portlet_not_setup");
65          }
66  
67          req.setAttribute(WebKeys.IFRAME_SRC, src);
68  
69          return mapping.findForward("portlet.iframe.view");
70      }
71  
72      protected String getSrc(RenderRequest req, RenderResponse res) {
73          PortletPreferences prefs = req.getPreferences();
74  
75          String src = prefs.getValue("src", StringPool.BLANK);
76  
77          src = ParamUtil.getString(req, "src", src);
78  
79          return src;
80      }
81  
82      protected String getUserName(RenderRequest req, RenderResponse res) {
83          PortletPreferences prefs = req.getPreferences();
84  
85          String userName = prefs.getValue("user-name", StringPool.BLANK);
86  
87          if (Validator.isNull(userName)) {
88              userName = req.getRemoteUser();
89          }
90  
91          return userName;
92      }
93  
94      protected String getPassword(RenderRequest req, RenderResponse res)
95          throws PortalException, SystemException {
96  
97          PortletPreferences prefs = req.getPreferences();
98  
99          String password = prefs.getValue("password", StringPool.BLANK);
100 
101         if (Validator.isNull(password)) {
102             password = PortalUtil.getUserPassword(req);
103         }
104 
105         return password;
106     }
107 
108     protected String transformSrc(
109             PortletConfig config, RenderRequest req, RenderResponse res)
110         throws PortalException, SystemException {
111 
112         PortletPreferences prefs = req.getPreferences();
113 
114         String src = getSrc(req, res);
115 
116         boolean auth = GetterUtil.getBoolean(
117             prefs.getValue("auth", StringPool.BLANK));
118         String authType = prefs.getValue("auth-type", StringPool.BLANK);
119         String userName = getUserName(req, res);
120         String password = getPassword(req, res);
121 
122         if (auth) {
123             if (authType.equals("basic")) {
124                 int pos = src.indexOf("://");
125 
126                 String protocol = src.substring(0, pos + 3);
127                 String url = src.substring(pos + 3, src.length());
128 
129                 src =
130                     protocol + userName + ":" + password +
131                     "@" + url;
132             }
133             else {
134                 ThemeDisplay themeDisplay =
135                     (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
136 
137                 PortletConfigImpl configImpl = (PortletConfigImpl)config;
138 
139                 String portletId = configImpl.getPortletId();
140 
141                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
142                     themeDisplay.getCompanyId(), portletId);
143 
144                 src =
145                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
146                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
147                             portletId;
148             }
149         }
150 
151         return src;
152     }
153 
154 }