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