001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.servlet;
031:
032: import com.caucho.quercus.*;
033: import com.caucho.quercus.env.Env;
034: import com.caucho.quercus.env.QuercusValueException;
035: import com.caucho.quercus.page.QuercusPage;
036: import com.caucho.server.connection.CauchoResponse;
037: import com.caucho.server.resin.Resin;
038: import com.caucho.server.webapp.WebApp;
039: import com.caucho.util.L10N;
040: import com.caucho.vfs.Path;
041: import com.caucho.vfs.Vfs;
042: import com.caucho.vfs.WriteStream;
043:
044: import javax.servlet.ServletConfig;
045: import javax.servlet.ServletException;
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048: import java.io.FileNotFoundException;
049: import java.io.IOException;
050: import java.io.OutputStream;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * Servlet to call PHP through javax.script.
056: */
057: public class ResinQuercusServlet extends QuercusServletImpl {
058: private static final L10N L = new L10N(ResinQuercusServlet.class);
059: private static final Logger log = Logger
060: .getLogger(ResinQuercusServlet.class.getName());
061:
062: private WebApp _webApp;
063:
064: /**
065: * initialize the script manager.
066: */
067: @Override
068: public void init(ServletConfig config) throws ServletException {
069: super .init(config);
070:
071: _webApp = (WebApp) config.getServletContext();
072:
073: ResinQuercus quercus = (ResinQuercus) getQuercus();
074:
075: quercus.setWebApp(_webApp);
076: getQuercus().setPwd(Vfs.lookup());
077:
078: quercus.setIni("caucho.server_id", Resin.getLocal()
079: .getServerId());
080: }
081:
082: /**
083: * Service.
084: */
085: public void service(HttpServletRequest request,
086: HttpServletResponse response) throws ServletException,
087: IOException {
088: Env env = null;
089: WriteStream ws = null;
090:
091: try {
092: Path path = getPath(request);
093:
094: QuercusPage page;
095:
096: try {
097: page = getQuercus().parse(path);
098: } catch (FileNotFoundException ex) {
099: // php/2001
100: log.log(Level.FINER, ex.toString(), ex);
101:
102: response.sendError(HttpServletResponse.SC_NOT_FOUND);
103:
104: return;
105: }
106:
107: // XXX: check if correct. PHP doesn't expect the lower levels
108: // to deal with the encoding, so this may be okay
109: if (response instanceof CauchoResponse) {
110: ws = Vfs.openWrite(((CauchoResponse) response)
111: .getResponseStream());
112: } else {
113: OutputStream out = response.getOutputStream();
114:
115: ws = Vfs.openWrite(out);
116: }
117:
118: Quercus quercus = getQuercus();
119:
120: env = quercus.createEnv(page, ws, request, response);
121: quercus.setServletContext(_servletContext);
122:
123: try {
124: env.start();
125:
126: env.setGlobalValue("request", env.wrapJava(request));
127: env.setGlobalValue("response", env.wrapJava(response));
128: env.setGlobalValue("servletContext", env
129: .wrapJava(_servletContext));
130:
131: String prepend = env.getIniString("auto_prepend_file");
132: if (prepend != null) {
133: Path prependPath = env.lookup(prepend);
134:
135: if (prependPath == null)
136: env.error(L.l(
137: "auto_prepend_file '{0}' not found.",
138: prepend));
139: else {
140: QuercusPage prependPage = getQuercus().parse(
141: prependPath);
142: prependPage.executeTop(env);
143: }
144: }
145:
146: page.executeTop(env);
147:
148: String append = env.getIniString("auto_append_file");
149: if (append != null) {
150: Path appendPath = env.lookup(append);
151:
152: if (appendPath == null)
153: env.error(L.l(
154: "auto_append_file '{0}' not found.",
155: append));
156: else {
157: QuercusPage appendPage = getQuercus().parse(
158: appendPath);
159: appendPage.executeTop(env);
160: }
161: }
162: // return;
163: } catch (QuercusExitException e) {
164: throw e;
165: } catch (QuercusErrorException e) {
166: throw e;
167: } catch (QuercusLineRuntimeException e) {
168: log.log(Level.FINE, e.toString(), e);
169:
170: ws.println(e.getMessage());
171: // return;
172: } catch (QuercusValueException e) {
173: log.log(Level.FINE, e.toString(), e);
174:
175: ws.println(e.toString());
176:
177: // return;
178: } catch (Throwable e) {
179: if (response.isCommitted())
180: e.printStackTrace(ws.getPrintWriter());
181:
182: ws = null;
183:
184: throw e;
185: } finally {
186: if (env != null)
187: env.close();
188:
189: // don't want a flush for an exception
190: if (ws != null)
191: ws.close();
192: }
193: } catch (QuercusDieException e) {
194: // normal exit
195: log.log(Level.FINE, e.toString(), e);
196: } catch (QuercusExitException e) {
197: // normal exit
198: log.log(Level.FINER, e.toString(), e);
199: } catch (QuercusErrorException e) {
200: // error exit
201: log.log(Level.FINE, e.toString(), e);
202: } catch (RuntimeException e) {
203: throw e;
204: } catch (Throwable e) {
205: throw new ServletException(e);
206: }
207: }
208:
209: Path getPath(HttpServletRequest req) {
210: String scriptPath = QuercusRequestAdapter
211: .getPageServletPath(req);
212: String pathInfo = QuercusRequestAdapter.getPagePathInfo(req);
213:
214: Path pwd = Vfs.lookup();
215:
216: Path path = pwd.lookup(req.getRealPath(scriptPath));
217:
218: if (path.isFile())
219: return path;
220:
221: // XXX: include
222:
223: String fullPath;
224: if (pathInfo != null)
225: fullPath = scriptPath + pathInfo;
226: else
227: fullPath = scriptPath;
228:
229: return Vfs.lookup().lookup(req.getRealPath(fullPath));
230: }
231:
232: /**
233: * Returns the Quercus instance.
234: */
235: @Override
236: protected Quercus getQuercus() {
237: synchronized (this ) {
238: if (_quercus == null)
239: _quercus = new ResinQuercus();
240: }
241:
242: return _quercus;
243: }
244: }
|