1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.iframe.action;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Portlet;
24  import com.liferay.portal.service.PortletLocalServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.iframe.util.IFrameUtil;
30  
31  import javax.portlet.PortletConfig;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class ViewAction extends PortletAction {
46  
47      public ActionForward render(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              RenderRequest renderRequest, RenderResponse renderResponse)
50          throws Exception {
51  
52          String src = transformSrc(renderRequest, renderResponse);
53  
54          if (Validator.isNull(src)) {
55              return mapping.findForward("/portal/portlet_not_setup");
56          }
57  
58          renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
59  
60          return mapping.findForward("portlet.iframe.view");
61      }
62  
63      protected String getSrc(
64          RenderRequest renderRequest, RenderResponse renderResponse) {
65  
66          PortletPreferences preferences = renderRequest.getPreferences();
67  
68          String src = preferences.getValue("src", StringPool.BLANK);
69  
70          src = ParamUtil.getString(renderRequest, "src", src);
71  
72          return src;
73      }
74  
75      protected String getUserName(
76              RenderRequest renderRequest, RenderResponse renderResponse)
77          throws PortalException, SystemException {
78  
79          PortletPreferences preferences = renderRequest.getPreferences();
80          String userName = preferences.getValue("user-name", StringPool.BLANK);
81  
82          return IFrameUtil.getUserName(renderRequest, userName);
83      }
84  
85      protected String getPassword(
86          RenderRequest renderRequest, RenderResponse renderResponse) {
87  
88          PortletPreferences preferences = renderRequest.getPreferences();
89          String password = preferences.getValue("password", StringPool.BLANK);
90  
91          return IFrameUtil.getPassword(renderRequest, password);
92      }
93  
94      protected String transformSrc(
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws PortalException, SystemException {
97  
98          PortletPreferences preferences = renderRequest.getPreferences();
99  
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 =
116                     protocol + userName + ":" + password +
117                     "@" + url;
118             }
119             else {
120                 ThemeDisplay themeDisplay =
121                     (ThemeDisplay)renderRequest.getAttribute(
122                         WebKeys.THEME_DISPLAY);
123 
124                 String portletId = PortalUtil.getPortletId(renderRequest);
125 
126                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
127                     themeDisplay.getCompanyId(), portletId);
128 
129                 src =
130                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
131                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
132                             portletId;
133             }
134         }
135 
136         return src;
137     }
138 
139 }