001: /*
002: * @(#)DynamicPageServlet.java 1.2 04/12/06
003: *
004: * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.servlet;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013: import javax.servlet.ServletException;
014: import javax.servlet.ServletConfig;
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.InputStreamReader;
018: import java.io.StringReader;
019: import java.io.StringWriter;
020: import java.io.Reader;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.net.URL;
024: import java.util.HashSet;
025: import java.util.Set;
026: import pnuts.lang.Pnuts;
027: import pnuts.lang.Runtime;
028: import pnuts.lang.Context;
029: import org.pnuts.servlet.protocol.pea.Handler;
030:
031: /**
032: * Dynamic Web page with embeded scripts.
033: *
034: * This servlet converts the dynamic page to a Pnuts script, when needed, then
035: * forward to the script.
036: *
037: */
038: public class DynamicPageServlet extends PnutsServlet {
039:
040: private String workdir;
041:
042: public void init() throws ServletException {
043: ServletConfig conf = getServletConfig();
044: String workdir = conf.getInitParameter("workdir");
045: if (workdir != null) {
046: this .workdir = workdir;
047: }
048: super .init();
049: }
050:
051: protected Pnuts parseFile(File file, String encoding,
052: PnutsServletContext psc) throws IOException {
053: Context context = psc.context;
054: FileInputStream fin = new FileInputStream(file);
055: Reader reader;
056: if (encoding != null) {
057: reader = new InputStreamReader(fin, encoding);
058: } else {
059: reader = Runtime.getScriptReader(fin, context);
060: }
061: StringWriter sw = new StringWriter();
062: Set scriptURLs = new HashSet();
063: scriptURLs.add(file.toURL());
064: try {
065: DynamicPage.convert(reader, sw, null, null, context,
066: scriptURLs);
067: psc.scriptURLs = scriptURLs;
068: } finally {
069: reader.close();
070: }
071: URL url = new URL(null, "pea:" + file.toURL(), new Handler(
072: context, scriptURLs));
073: return Pnuts.parse(new StringReader(sw.toString()), url,
074: context);
075: }
076:
077: void do_service(HttpServletRequest request,
078: HttpServletResponse response) throws ServletException,
079: IOException {
080: if (workdir == null) {
081: super .do_service(request, response);
082: return;
083: }
084: try {
085: String s_path = request.getServletPath();
086: String path = "";
087: if (s_path != null) {
088: path = s_path;
089: }
090: if (path == null) {
091: path = request.getPathInfo();
092: }
093: String real_path = getServletContext().getRealPath(path);
094:
095: File file = null;
096: if (real_path != null) {
097: file = new File(real_path);
098: }
099: if (file == null || !file.exists()) {
100: if (debug) {
101: throw new FileNotFoundException(real_path);
102: } else {
103: response
104: .sendError(HttpServletResponse.SC_NOT_FOUND);
105: return;
106: }
107: }
108: String filename = file.getName();
109: if (filename.endsWith(".pnut")) {
110: response.sendError(
111: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
112: "");
113: return;
114: }
115: int idx = filename.lastIndexOf('.');
116: String s;
117: if (idx > 0) {
118: s = filename.substring(0, idx) + ".pnut";
119: } else {
120: s = filename + ".pnut";
121: }
122: File dir = file.getParentFile();
123: if (workdir != null) {
124: dir = new File(dir, workdir);
125: if (!dir.exists()) {
126: dir.mkdirs();
127: }
128: }
129: File outputFile = new File(dir, s);
130:
131: if (!outputFile.exists()
132: || outputFile.lastModified() < file.lastModified()) {
133: DynamicPage.convert(file, outputFile, encoding,
134: new Context());
135: }
136: request.getRequestDispatcher(s).forward(request, response);
137:
138: } catch (Throwable e) {
139: if (debug) {
140: e.printStackTrace();
141: } else {
142: System.err.println(e);
143: }
144: response.sendError(
145: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "");
146: }
147: }
148:
149: }
|