001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-10-18 09:51:30 +0200 (Di, 18 Okt 2005) $
025: * $Author: til132 $
026: * $Revision: 175 $
027: */
028: package net.sf.regain.util.sharedtag.simple;
029:
030: import java.io.ByteArrayOutputStream;
031: import java.io.File;
032: import java.io.PrintStream;
033:
034: import net.sf.regain.RegainException;
035: import net.sf.regain.util.sharedtag.PageRequest;
036: import net.sf.regain.util.sharedtag.PageResponse;
037:
038: import org.apache.log4j.Logger;
039:
040: import simple.http.Request;
041: import simple.http.Response;
042: import simple.http.serve.BasicResource;
043: import simple.http.serve.Context;
044:
045: /**
046: * A simpleweb resource representing one JSP page.
047: *
048: * @see simple.http.serve.Resource
049: * @author Til Schneider, www.murfman.de
050: */
051: public class SharedTagResource extends BasicResource {
052:
053: /** The encoding used by the simpleweb implementation of shared tags. */
054: public static final String SIMPLE_TAG_ENCODING = "UTF-8";
055:
056: /** The logger for this class */
057: private static Logger mLog = Logger
058: .getLogger(SharedTagResource.class);
059:
060: /** The base directory where the provided files are located. */
061: private static File mBaseDir;
062:
063: /** The root executer holding the parsed JSP page. */
064: private Executer mRootTagExecuter;
065:
066: /**
067: * Creates a new instance of SharedTagResource.
068: *
069: * @param context The context of this resource.
070: * @param root The root executer holding the parsed JSP page.
071: * @throws RegainException If parsing the JSP file failed.
072: */
073: public SharedTagResource(Context context, Executer root)
074: throws RegainException {
075: super (context);
076:
077: mRootTagExecuter = root;
078: }
079:
080: /**
081: * Processes a request.
082: *
083: * @param req The request.
084: * @param resp The response.
085: * @throws Exception If executing the JSP page failed.
086: */
087: protected synchronized void process(Request req, Response resp)
088: throws Exception {
089: process(req, resp, mRootTagExecuter, null);
090: }
091:
092: /**
093: * Processes a request.
094: *
095: * @param req The request.
096: * @param resp The response.
097: * @param executer The executer to use.
098: * @param error The error to show. Is <code>null</code> if no error page is
099: * shown.
100: * @throws Exception If executing the JSP page failed.
101: */
102: private synchronized void process(Request req, Response resp,
103: Executer executer, Throwable error) throws Exception {
104: // Write the page to a buffer first
105: // If an exception should be thrown the user gets a clear error message
106: ByteArrayOutputStream stream = new ByteArrayOutputStream();
107: PrintStream printStream = new PrintStream(stream, false,
108: SIMPLE_TAG_ENCODING);
109:
110: PageRequest request = new SimplePageRequest(req);
111: PageResponse response = new SimplePageResponse(this , req, resp,
112: printStream, SIMPLE_TAG_ENCODING);
113:
114: // Add the error to the page attributes
115: if (error != null) {
116: request.setContextAttribute("page.exception", error);
117: }
118:
119: try {
120: executer.execute(request, response);
121: } catch (RedirectException exc) {
122: // Send a redirect
123: resp.set("Location", exc.getUrl());
124: handle(req, resp, 303);
125: return;
126: } catch (Exception exc) {
127: mLog.error("Processing page failed", exc);
128: if (error == null) {
129: // This is the normal page -> Show the error page
130: try {
131: Executer errorExecuter = loadErrorPage();
132: process(req, resp, errorExecuter, exc);
133: } catch (RegainException loadingExc) {
134: mLog.error("Processing error page failed",
135: loadingExc);
136:
137: // Throw the original error, so a simple error page is shown
138: throw exc;
139: }
140: } else {
141: // This already is the error page -> Show a simple error
142: throw exc;
143: }
144: } finally {
145: printStream.close();
146: stream.close();
147: }
148:
149: // The page has been generated without exception -> Send it to the user
150: resp.set("Content-Type", "text/html; charset="
151: + SIMPLE_TAG_ENCODING);
152: PrintStream pageStream = resp.getPrintStream();
153: try {
154: stream.writeTo(pageStream);
155: } finally {
156: pageStream.close();
157: }
158: }
159:
160: /**
161: * Loads the error page executer.
162: *
163: * @return The error page executer.
164: * @throws RegainException If loading the error page executer failed.
165: */
166: private Executer loadErrorPage() throws RegainException {
167: if (mBaseDir == null) {
168: mBaseDir = new File(context.getBasePath());
169: }
170:
171: return new ExecuterParser().parse(mBaseDir, "errorpage.jsp");
172: }
173:
174: }
|