001    /**
002     * Copyright (c) 2000-2013 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.BufferCacheServletResponse;
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.portal.util.PortalUtil;
027    import com.liferay.portlet.RenderResponseImpl;
028    
029    import java.io.IOException;
030    import java.io.PrintWriter;
031    
032    import javax.portlet.PortletContext;
033    import javax.portlet.PortletException;
034    import javax.portlet.PortletPreferences;
035    import javax.portlet.PortletRequestDispatcher;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.portletbridge.portlet.PortletBridgePortlet;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class WebProxyPortlet extends PortletBridgePortlet {
045    
046            @Override
047            public void doView(
048                            RenderRequest renderRequest, RenderResponse renderResponse)
049                    throws IOException, PortletException {
050    
051                    if (!_enabled) {
052                            printError(renderResponse);
053    
054                            return;
055                    }
056    
057                    PortletPreferences preferences = renderRequest.getPreferences();
058    
059                    String initUrl = preferences.getValue("initUrl", StringPool.BLANK);
060    
061                    if (Validator.isNull(initUrl)) {
062                            PortletContext portletContext = getPortletContext();
063    
064                            PortletRequestDispatcher portletRequestDispatcher =
065                                    portletContext.getRequestDispatcher(
066                                            StrutsUtil.TEXT_HTML_DIR + "/portal/portlet_not_setup.jsp");
067    
068                            portletRequestDispatcher.include(renderRequest, renderResponse);
069                    }
070                    else {
071                            super.doView(renderRequest, renderResponse);
072    
073                            RenderResponseImpl renderResponseImpl =
074                                    (RenderResponseImpl)renderResponse;
075    
076                            BufferCacheServletResponse bufferCacheServletResponse =
077                                    (BufferCacheServletResponse)
078                                            renderResponseImpl.getHttpServletResponse();
079    
080                            String output = bufferCacheServletResponse.getString();
081    
082                            output = StringUtil.replace(
083                                    output, "//pbhs/", PortalUtil.getPathContext() + "/pbhs/");
084    
085                            bufferCacheServletResponse.setString(output);
086                    }
087            }
088    
089            @Override
090            public void init() {
091                    try {
092                            super.init();
093    
094                            _enabled = true;
095                    }
096                    catch (Exception e) {
097                            if (_log.isWarnEnabled()) {
098                                    _log.warn(e.getMessage());
099                            }
100                    }
101    
102                    if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
103                            _log.info(
104                                    "WebProxyPortlet will not be enabled unless Liferay's " +
105                                            "serializer.jar and xalan.jar files are copied to the " +
106                                                    "JDK's endorsed directory");
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    }