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.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.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                            StringServletResponse stringResponse = (StringServletResponse)
074                                    renderResponseImpl.getHttpServletResponse();
075    
076                            String output = stringResponse.getString();
077    
078                            output = StringUtil.replace(
079                                    output, "//pbhs/", PortalUtil.getPathContext() + "/pbhs/");
080    
081                            stringResponse.setString(output);
082                    }
083            }
084    
085            @Override
086            public void init() {
087                    try {
088                            super.init();
089    
090                            _enabled = true;
091                    }
092                    catch (Exception e) {
093                            if (_log.isWarnEnabled()) {
094                                    _log.warn(e.getMessage());
095                            }
096                    }
097    
098                    if (!_enabled && ServerDetector.isWebLogic() && _log.isInfoEnabled()) {
099                            _log.info(
100                                    "WebProxyPortlet will not be enabled unless Liferay's " +
101                                            "serializer.jar and xalan.jar files are copied to the " +
102                                                    "JDK's endorsed directory");
103                    }
104            }
105    
106            protected void printError(RenderResponse renderResponse)
107                    throws IOException {
108    
109                    renderResponse.setContentType(ContentTypes.TEXT_HTML_UTF8);
110    
111                    PrintWriter writer = renderResponse.getWriter();
112    
113                    writer.print(
114                            "WebProxyPortlet will not be enabled unless Liferay's " +
115                                    "serializer.jar and xalan.jar files are copied to the " +
116                                            "JDK's endorsed directory");
117    
118                    writer.close();
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(WebProxyPortlet.class);
122    
123            private boolean _enabled;
124    
125    }