001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.webproxy;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.StringServletResponse;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.ServerDetector;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.struts.StrutsUtil;
026    import com.liferay.portlet.RenderResponseImpl;
027    
028    import java.io.IOException;
029    import java.io.PrintWriter;
030    
031    import javax.portlet.PortletException;
032    import javax.portlet.PortletPreferences;
033    import javax.portlet.PortletRequestDispatcher;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.portletbridge.portlet.PortletBridgePortlet;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class WebProxyPortlet extends PortletBridgePortlet {
043    
044            public void init() {
045                    try {
046                            super.init();
047    
048                            _enabled = true;
049                    }
050                    catch (Exception e) {
051                            if (_log.isWarnEnabled()) {
052                                    _log.warn(e.getMessage());
053                            }
054                    }
055    
056                    if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
057                            _log.info(
058                                    "WebProxyPortlet will not be enabled unless Liferay's " +
059                                            "serializer.jar and xalan.jar files are copied to the " +
060                                                    "JDK's endorsed directory");
061                    }
062            }
063    
064            public void doView(
065                            RenderRequest renderRequest, RenderResponse renderResponse)
066                    throws IOException, PortletException {
067    
068                    if (!_enabled) {
069                            printError(renderResponse);
070    
071                            return;
072                    }
073    
074                    PortletPreferences preferences = renderRequest.getPreferences();
075    
076                    String initUrl = preferences.getValue("initUrl", StringPool.BLANK);
077    
078                    if (Validator.isNull(initUrl)) {
079                            PortletRequestDispatcher portletRequestDispatcher =
080                                    getPortletContext().getRequestDispatcher(
081                                            StrutsUtil.TEXT_HTML_DIR + "/portal/portlet_not_setup.jsp");
082    
083                            portletRequestDispatcher.include(renderRequest, renderResponse);
084                    }
085                    else {
086                            super.doView(renderRequest, renderResponse);
087    
088                            RenderResponseImpl renderResponseImpl =
089                                    (RenderResponseImpl)renderResponse;
090    
091                            StringServletResponse stringResponse = (StringServletResponse)
092                                    renderResponseImpl.getHttpServletResponse();
093    
094                            String output = stringResponse.getString();
095    
096                            output = StringUtil.replace(output, "//pbhs/", "/pbhs/");
097    
098                            stringResponse.setString(output);
099                    }
100            }
101    
102            protected void printError(RenderResponse renderResponse)
103                    throws IOException {
104    
105                    renderResponse.setContentType(ContentTypes.TEXT_HTML_UTF8);
106    
107                    PrintWriter writer = renderResponse.getWriter();
108    
109                    writer.print(
110                            "WebProxyPortlet will not be enabled unless Liferay's " +
111                                    "serializer.jar and xalan.jar files are copied to the " +
112                                            "JDK's endorsed directory");
113    
114                    writer.close();
115            }
116    
117            private static Log _log = LogFactoryUtil.getLog(WebProxyPortlet.class);
118    
119            private boolean _enabled;
120    
121    }