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.PortletException;
033    import javax.portlet.PortletPreferences;
034    import javax.portlet.PortletRequestDispatcher;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.portletbridge.portlet.PortletBridgePortlet;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class WebProxyPortlet extends PortletBridgePortlet {
044    
045            @Override
046            public void doView(
047                            RenderRequest renderRequest, RenderResponse renderResponse)
048                    throws IOException, PortletException {
049    
050                    if (!_enabled) {
051                            printError(renderResponse);
052    
053                            return;
054                    }
055    
056                    PortletPreferences preferences = renderRequest.getPreferences();
057    
058                    String initUrl = preferences.getValue("initUrl", StringPool.BLANK);
059    
060                    if (Validator.isNull(initUrl)) {
061                            PortletRequestDispatcher portletRequestDispatcher =
062                                    getPortletContext().getRequestDispatcher(
063                                            StrutsUtil.TEXT_HTML_DIR + "/portal/portlet_not_setup.jsp");
064    
065                            portletRequestDispatcher.include(renderRequest, renderResponse);
066                    }
067                    else {
068                            super.doView(renderRequest, renderResponse);
069    
070                            RenderResponseImpl renderResponseImpl =
071                                    (RenderResponseImpl)renderResponse;
072    
073                            BufferCacheServletResponse bufferCacheServletResponse =
074                                    (BufferCacheServletResponse)
075                                            renderResponseImpl.getHttpServletResponse();
076    
077                            String output = bufferCacheServletResponse.getString();
078    
079                            output = StringUtil.replace(
080                                    output, "//pbhs/", PortalUtil.getPathContext() + "/pbhs/");
081    
082                            bufferCacheServletResponse.setString(output);
083                    }
084            }
085    
086            @Override
087            public void init() {
088                    try {
089                            super.init();
090    
091                            _enabled = true;
092                    }
093                    catch (Exception e) {
094                            if (_log.isWarnEnabled()) {
095                                    _log.warn(e.getMessage());
096                            }
097                    }
098    
099                    if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
100                            _log.info(
101                                    "WebProxyPortlet will not be enabled unless Liferay's " +
102                                            "serializer.jar and xalan.jar files are copied to the " +
103                                                    "JDK's endorsed directory");
104                    }
105            }
106    
107            protected void printError(RenderResponse renderResponse)
108                    throws IOException {
109    
110                    renderResponse.setContentType(ContentTypes.TEXT_HTML_UTF8);
111    
112                    PrintWriter writer = renderResponse.getWriter();
113    
114                    writer.print(
115                            "WebProxyPortlet will not be enabled unless Liferay's " +
116                                    "serializer.jar and xalan.jar files are copied to the " +
117                                            "JDK's endorsed directory");
118    
119                    writer.close();
120            }
121    
122            private static Log _log = LogFactoryUtil.getLog(WebProxyPortlet.class);
123    
124            private boolean _enabled;
125    
126    }