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