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.webproxy;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.servlet.StringServletResponse;
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.struts.StrutsUtil;
31  import com.liferay.portlet.RenderResponseImpl;
32  
33  import java.io.IOException;
34  import java.io.PrintWriter;
35  
36  import javax.portlet.PortletException;
37  import javax.portlet.PortletPreferences;
38  import javax.portlet.PortletRequestDispatcher;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.portletbridge.portlet.PortletBridgePortlet;
43  
44  /**
45   * <a href="WebProxyPortlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class WebProxyPortlet extends PortletBridgePortlet {
51  
52      public void init() {
53          try {
54              super.init();
55  
56              _enabled = true;
57          }
58          catch (Exception e) {
59              if (_log.isWarnEnabled()) {
60                  _log.warn(e.getMessage());
61              }
62          }
63  
64          if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
65              _log.info(
66                  "WebProxyPortlet will not be enabled unless Liferay's " +
67                      "serializer.jar and xalan.jar files are copied to the " +
68                          "JDK's endorsed directory");
69          }
70      }
71  
72      public void doView(
73              RenderRequest renderRequest, RenderResponse renderResponse)
74          throws IOException, PortletException {
75  
76          if (!_enabled) {
77              printError(renderResponse);
78  
79              return;
80          }
81  
82          PortletPreferences preferences = renderRequest.getPreferences();
83  
84          String initUrl = preferences.getValue("initUrl", StringPool.BLANK);
85  
86          if (Validator.isNull(initUrl)) {
87              PortletRequestDispatcher portletRequestDispatcher =
88                  getPortletContext().getRequestDispatcher(
89                      StrutsUtil.TEXT_HTML_DIR + "/portal/portlet_not_setup.jsp");
90  
91              portletRequestDispatcher.include(renderRequest, renderResponse);
92          }
93          else {
94              super.doView(renderRequest, renderResponse);
95  
96              RenderResponseImpl renderResponseImpl =
97                  (RenderResponseImpl)renderResponse;
98  
99              StringServletResponse stringResponse = (StringServletResponse)
100                 renderResponseImpl.getHttpServletResponse();
101 
102             String output = stringResponse.getString();
103 
104             output = StringUtil.replace(output, "//pbhs/", "/pbhs/");
105 
106             stringResponse.setString(output);
107         }
108     }
109 
110     protected void printError(RenderResponse renderResponse)
111         throws IOException {
112 
113         renderResponse.setContentType(ContentTypes.TEXT_HTML_UTF8);
114 
115         PrintWriter writer = renderResponse.getWriter();
116 
117         writer.print(
118             "WebProxyPortlet will not be enabled unless Liferay's " +
119                 "serializer.jar and xalan.jar files are copied to the " +
120                     "JDK's endorsed directory");
121 
122         writer.close();
123     }
124 
125     private static Log _log = LogFactoryUtil.getLog(WebProxyPortlet.class);
126 
127     private boolean _enabled;
128 
129 }