001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.php;
022:
023: import com.liferay.portal.kernel.servlet.PortletServlet;
024: import com.liferay.portal.kernel.servlet.StringServletResponse;
025: import com.liferay.util.servlet.DynamicServletConfig;
026:
027: import java.io.IOException;
028: import java.io.PrintWriter;
029:
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import javax.portlet.ActionRequest;
035: import javax.portlet.ActionResponse;
036: import javax.portlet.GenericPortlet;
037: import javax.portlet.PortletException;
038: import javax.portlet.PortletURL;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041:
042: import javax.servlet.ServletConfig;
043: import javax.servlet.http.HttpServlet;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.apache.portals.bridges.common.ScriptPostProcess;
050:
051: /**
052: * <a href="PHPPortlet.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Jorge Ferrer
055: *
056: */
057: public class PHPPortlet extends GenericPortlet {
058:
059: public static final String PHP_URI_PARAM = "phpURI";
060:
061: public void init() throws PortletException {
062: editUri = getInitParameter("edit-uri");
063: helpUri = getInitParameter("help-uri");
064: viewUri = getInitParameter("view-uri");
065: }
066:
067: public void doDispatch(RenderRequest req, RenderResponse res)
068: throws IOException, PortletException {
069:
070: String phpUri = req.getParameter(PHP_URI_PARAM);
071:
072: if (phpUri != null) {
073: processPHP(phpUri, req, res);
074: } else {
075: super .doDispatch(req, res);
076: }
077: }
078:
079: public void doEdit(RenderRequest req, RenderResponse res)
080: throws IOException, PortletException {
081:
082: if (req.getPreferences() == null) {
083: super .doEdit(req, res);
084: } else {
085: processPHP(editUri, req, res);
086: }
087: }
088:
089: public void doHelp(RenderRequest req, RenderResponse res)
090: throws IOException, PortletException {
091:
092: processPHP(helpUri, req, res);
093: }
094:
095: public void doView(RenderRequest req, RenderResponse res)
096: throws IOException, PortletException {
097:
098: processPHP(viewUri, req, res);
099: }
100:
101: public void processAction(ActionRequest req, ActionResponse res)
102: throws IOException, PortletException {
103:
104: String phpURI = req.getParameter(PHP_URI_PARAM);
105:
106: if (phpURI != null) {
107: res.setRenderParameter(PHP_URI_PARAM, phpURI);
108: }
109: }
110:
111: public void destroy() {
112: if (quercusServlet != null) {
113: quercusServlet.destroy();
114: }
115: }
116:
117: protected synchronized void initQuercus(ServletConfig config)
118: throws PortletException {
119:
120: if (quercusServlet == null) {
121: try {
122: quercusServlet = (HttpServlet) Class.forName(
123: _QUERCUS_SERVLET).newInstance();
124:
125: Map params = new HashMap();
126:
127: Enumeration enu = config.getInitParameterNames();
128:
129: while (enu.hasMoreElements()) {
130: String name = (String) enu.nextElement();
131:
132: if (!name.equals("portlet-class")) {
133: params.put(name, config.getInitParameter(name));
134: }
135: }
136:
137: config = new DynamicServletConfig(config, params);
138:
139: quercusServlet.init(config);
140: } catch (Exception e) {
141: throw new PortletException(e);
142: }
143: }
144: }
145:
146: protected void processPHP(String phpURI, RenderRequest req,
147: RenderResponse res) throws IOException, PortletException {
148:
149: try {
150: ServletConfig config = (ServletConfig) req
151: .getAttribute(PortletServlet.PORTLET_SERVLET_CONFIG);
152:
153: initQuercus(config);
154:
155: HttpServletRequest httpReq = (HttpServletRequest) req
156: .getAttribute(PortletServlet.PORTLET_SERVLET_REQUEST);
157: HttpServletResponse httpRes = (HttpServletResponse) req
158: .getAttribute(PortletServlet.PORTLET_SERVLET_RESPONSE);
159:
160: PHPServletRequest phpReq = new PHPServletRequest(httpReq,
161: config, req, res, getPortletConfig(), phpURI);
162:
163: StringServletResponse phpRes = new StringServletResponse(
164: httpRes);
165:
166: quercusServlet.service(phpReq, phpRes);
167:
168: String result = phpRes.getString();
169:
170: if (phpRes.getContentType().startsWith("text/")) {
171: result = rewriteURLs(result, res.createRenderURL());
172: }
173:
174: PrintWriter writer = httpRes.getWriter();
175:
176: writer.write(result.toCharArray());
177: } catch (Exception e) {
178: _log.error("Error processing PHP", e);
179: }
180: }
181:
182: protected String rewriteURLs(String page, PortletURL portletURL) {
183: ScriptPostProcess processor = new ScriptPostProcess();
184:
185: processor.setInitalPage(new StringBuffer(page));
186: processor.postProcessPage(portletURL, PHP_URI_PARAM);
187:
188: return processor.getFinalizedPage();
189: }
190:
191: private static final String _QUERCUS_SERVLET = "com.caucho.quercus.servlet.QuercusServlet";
192:
193: private static Log _log = LogFactory.getLog(PHPPortlet.class);
194:
195: protected String editUri;
196: protected String helpUri;
197: protected String viewUri;
198: protected HttpServlet quercusServlet;
199:
200: }
|