1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.iframe.action;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.service.PortletLocalServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.iframe.util.IFrameUtil;
35  
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletPreferences;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class ViewAction extends PortletAction {
52  
53      public ActionForward render(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              RenderRequest renderRequest, RenderResponse renderResponse)
56          throws Exception {
57  
58          String src = transformSrc(renderRequest, renderResponse);
59  
60          if (Validator.isNull(src)) {
61              return mapping.findForward("/portal/portlet_not_setup");
62          }
63  
64          renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
65  
66          return mapping.findForward("portlet.iframe.view");
67      }
68  
69      protected String getSrc(
70          RenderRequest renderRequest, RenderResponse renderResponse) {
71  
72          PortletPreferences preferences = renderRequest.getPreferences();
73  
74          String src = preferences.getValue("src", StringPool.BLANK);
75  
76          src = ParamUtil.getString(renderRequest, "src", src);
77  
78          return src;
79      }
80  
81      protected String getUserName(
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws PortalException, SystemException {
84  
85          PortletPreferences preferences = renderRequest.getPreferences();
86          String userName = preferences.getValue("user-name", StringPool.BLANK);
87  
88          return IFrameUtil.getUserName(renderRequest, userName);
89      }
90  
91      protected String getPassword(
92          RenderRequest renderRequest, RenderResponse renderResponse) {
93  
94          PortletPreferences preferences = renderRequest.getPreferences();
95          String password = preferences.getValue("password", StringPool.BLANK);
96  
97          return IFrameUtil.getPassword(renderRequest, password);
98      }
99  
100     protected String transformSrc(
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws PortalException, SystemException {
103 
104         PortletPreferences preferences = renderRequest.getPreferences();
105 
106         String src = getSrc(renderRequest, renderResponse);
107 
108         boolean auth = GetterUtil.getBoolean(
109             preferences.getValue("auth", StringPool.BLANK));
110         String authType = preferences.getValue("auth-type", StringPool.BLANK);
111         String userName = getUserName(renderRequest, renderResponse);
112         String password = getPassword(renderRequest, renderResponse);
113 
114         if (auth) {
115             if (authType.equals("basic")) {
116                 int pos = src.indexOf("://");
117 
118                 String protocol = src.substring(0, pos + 3);
119                 String url = src.substring(pos + 3, src.length());
120 
121                 src =
122                     protocol + userName + ":" + password +
123                     "@" + url;
124             }
125             else {
126                 ThemeDisplay themeDisplay =
127                     (ThemeDisplay)renderRequest.getAttribute(
128                         WebKeys.THEME_DISPLAY);
129 
130                 String portletId = PortalUtil.getPortletId(renderRequest);
131 
132                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
133                     themeDisplay.getCompanyId(), portletId);
134 
135                 src =
136                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
137                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
138                             portletId;
139             }
140         }
141 
142         return src;
143     }
144 
145 }