1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.webproxy;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.StringServletResponse;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.ServerDetector;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.struts.StrutsUtil;
26  import com.liferay.portlet.RenderResponseImpl;
27  
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  
31  import javax.portlet.PortletException;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.PortletRequestDispatcher;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.portletbridge.portlet.PortletBridgePortlet;
38  
39  /**
40   * <a href="WebProxyPortlet.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class WebProxyPortlet extends PortletBridgePortlet {
45  
46      public void init() {
47          try {
48              super.init();
49  
50              _enabled = true;
51          }
52          catch (Exception e) {
53              if (_log.isWarnEnabled()) {
54                  _log.warn(e.getMessage());
55              }
56          }
57  
58          if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
59              _log.info(
60                  "WebProxyPortlet will not be enabled unless Liferay's " +
61                      "serializer.jar and xalan.jar files are copied to the " +
62                          "JDK's endorsed directory");
63          }
64      }
65  
66      public void doView(
67              RenderRequest renderRequest, RenderResponse renderResponse)
68          throws IOException, PortletException {
69  
70          if (!_enabled) {
71              printError(renderResponse);
72  
73              return;
74          }
75  
76          PortletPreferences preferences = renderRequest.getPreferences();
77  
78          String initUrl = preferences.getValue("initUrl", StringPool.BLANK);
79  
80          if (Validator.isNull(initUrl)) {
81              PortletRequestDispatcher portletRequestDispatcher =
82                  getPortletContext().getRequestDispatcher(
83                      StrutsUtil.TEXT_HTML_DIR + "/portal/portlet_not_setup.jsp");
84  
85              portletRequestDispatcher.include(renderRequest, renderResponse);
86          }
87          else {
88              super.doView(renderRequest, renderResponse);
89  
90              RenderResponseImpl renderResponseImpl =
91                  (RenderResponseImpl)renderResponse;
92  
93              StringServletResponse stringResponse = (StringServletResponse)
94                  renderResponseImpl.getHttpServletResponse();
95  
96              String output = stringResponse.getString();
97  
98              output = StringUtil.replace(output, "//pbhs/", "/pbhs/");
99  
100             stringResponse.setString(output);
101         }
102     }
103 
104     protected void printError(RenderResponse renderResponse)
105         throws IOException {
106 
107         renderResponse.setContentType(ContentTypes.TEXT_HTML_UTF8);
108 
109         PrintWriter writer = renderResponse.getWriter();
110 
111         writer.print(
112             "WebProxyPortlet will not be enabled unless Liferay's " +
113                 "serializer.jar and xalan.jar files are copied to the " +
114                     "JDK's endorsed directory");
115 
116         writer.close();
117     }
118 
119     private static Log _log = LogFactoryUtil.getLog(WebProxyPortlet.class);
120 
121     private boolean _enabled;
122 
123 }