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