1   /**
2    * Copyright (c) 2000-2007 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.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  import com.liferay.portlet.PortletConfigImpl;
37  
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletPreferences;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ViewAction extends PortletAction {
54  
55      public ActionForward render(
56              ActionMapping mapping, ActionForm form, PortletConfig config,
57              RenderRequest req, RenderResponse res)
58          throws Exception {
59  
60          String src = transformSrc(config, req, res);
61  
62          if (Validator.isNull(src)) {
63              return mapping.findForward("/portal/portlet_not_setup");
64          }
65  
66          req.setAttribute(WebKeys.IFRAME_SRC, src);
67  
68          return mapping.findForward("portlet.iframe.view");
69      }
70  
71      protected String getSrc(RenderRequest req, RenderResponse res) {
72          PortletPreferences prefs = req.getPreferences();
73  
74          return prefs.getValue("src", StringPool.BLANK);
75      }
76  
77      protected String getUserName(RenderRequest req, RenderResponse res) {
78          PortletPreferences prefs = req.getPreferences();
79  
80          String userName = prefs.getValue("user-name", StringPool.BLANK);
81  
82          if (Validator.isNull(userName)) {
83              userName = req.getRemoteUser();
84          }
85  
86          return userName;
87      }
88  
89      protected String getPassword(RenderRequest req, RenderResponse res)
90          throws PortalException, SystemException {
91  
92          PortletPreferences prefs = req.getPreferences();
93  
94          String password = prefs.getValue("password", StringPool.BLANK);
95  
96          if (Validator.isNull(password)) {
97              password = PortalUtil.getUserPassword(req);
98          }
99  
100         return password;
101     }
102 
103     protected String transformSrc(
104             PortletConfig config, RenderRequest req, RenderResponse res)
105         throws PortalException, SystemException {
106 
107         PortletPreferences prefs = req.getPreferences();
108 
109         String src = getSrc(req, res);
110 
111         boolean auth = GetterUtil.getBoolean(
112             prefs.getValue("auth", StringPool.BLANK));
113         String authType = prefs.getValue("auth-type", StringPool.BLANK);
114         String userName = getUserName(req, res);
115         String password = getPassword(req, res);
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)req.getAttribute(WebKeys.THEME_DISPLAY);
131 
132                 PortletConfigImpl configImpl = (PortletConfigImpl)config;
133 
134                 String portletId = configImpl.getPortletId();
135 
136                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
137                     themeDisplay.getCompanyId(), portletId);
138 
139                 src =
140                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
141                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
142                             portletId;
143             }
144         }
145 
146         return src;
147     }
148 
149 }