1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.iframe.util.IFrameUtil;
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  public class ViewAction extends PortletAction {
54  
55      public ActionForward render(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              RenderRequest renderRequest, RenderResponse renderResponse)
58          throws Exception {
59  
60          String src = transformSrc(renderRequest, renderResponse);
61  
62          if (Validator.isNull(src)) {
63              return mapping.findForward("/portal/portlet_not_setup");
64          }
65  
66          renderRequest.setAttribute(WebKeys.IFRAME_SRC, src);
67  
68          return mapping.findForward("portlet.iframe.view");
69      }
70  
71      protected String getSrc(
72          RenderRequest renderRequest, RenderResponse renderResponse) {
73  
74          PortletPreferences preferences = renderRequest.getPreferences();
75  
76          String src = preferences.getValue("src", StringPool.BLANK);
77  
78          src = ParamUtil.getString(renderRequest, "src", src);
79  
80          return src;
81      }
82  
83      protected String getUserName(
84              RenderRequest renderRequest, RenderResponse renderResponse)
85          throws PortalException, SystemException {
86  
87          PortletPreferences preferences = renderRequest.getPreferences();
88          String userName = preferences.getValue("user-name", StringPool.BLANK);
89  
90          return IFrameUtil.getUserName(renderRequest, userName);
91      }
92  
93      protected String getPassword(
94          RenderRequest renderRequest, RenderResponse renderResponse) {
95  
96          PortletPreferences preferences = renderRequest.getPreferences();
97          String password = preferences.getValue("password", StringPool.BLANK);
98  
99          return IFrameUtil.getPassword(renderRequest, password);
100     }
101 
102     protected String transformSrc(
103             RenderRequest renderRequest, RenderResponse renderResponse)
104         throws PortalException, SystemException {
105 
106         PortletPreferences preferences = renderRequest.getPreferences();
107 
108         String src = getSrc(renderRequest, renderResponse);
109 
110         boolean auth = GetterUtil.getBoolean(
111             preferences.getValue("auth", StringPool.BLANK));
112         String authType = preferences.getValue("auth-type", StringPool.BLANK);
113         String userName = getUserName(renderRequest, renderResponse);
114         String password = getPassword(renderRequest, renderResponse);
115 
116         if (auth) {
117             if (authType.equals("basic")) {
118                 int pos = src.indexOf("://");
119 
120                 String protocol = src.substring(0, pos + 3);
121                 String url = src.substring(pos + 3, src.length());
122 
123                 src =
124                     protocol + userName + ":" + password +
125                     "@" + url;
126             }
127             else {
128                 ThemeDisplay themeDisplay =
129                     (ThemeDisplay)renderRequest.getAttribute(
130                         WebKeys.THEME_DISPLAY);
131 
132                 String portletId = PortalUtil.getPortletId(renderRequest);
133 
134                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
135                     themeDisplay.getCompanyId(), portletId);
136 
137                 src =
138                     themeDisplay.getPathMain() + "/" + portlet.getStrutsPath() +
139                         "/proxy?p_l_id=" + themeDisplay.getPlid() + "&p_p_id=" +
140                             portletId;
141             }
142         }
143 
144         return src;
145     }
146 
147 }