001: // GeneratedFrame.java
002: // $Id: GeneratedFrame.java,v 1.11 2000/08/16 21:37:42 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.pagecompile;
007:
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.io.PrintWriter;
011: import java.io.StringBufferInputStream;
012: import org.w3c.jigsaw.http.HTTPException;
013: import org.w3c.jigsaw.http.Reply;
014: import org.w3c.jigsaw.http.Request;
015: import org.w3c.jigsaw.frames.PostableFrame;
016: import org.w3c.www.mime.MimeType;
017: import org.w3c.jigsaw.forms.URLDecoder;
018: import org.w3c.jigsaw.forms.URLDecoderException;
019: import org.w3c.tools.resources.ProtocolException;
020: import org.w3c.tools.resources.ReplyInterface;
021: import org.w3c.tools.resources.RequestInterface;
022: import org.w3c.tools.resources.ResourceException;
023:
024: import org.w3c.www.http.HTTP;
025: import org.w3c.tools.resources.ProtocolException;
026: import org.w3c.tools.resources.ResourceException;
027:
028: /**
029: * @version $Revision: 1.11 $
030: * @author Benoît Mahé (bmahe@w3.org)
031: */
032: public abstract class GeneratedFrame extends PostableFrame {
033: /**
034: * Get the 'convert GET to POST' flag.
035: * Always return false in GeneratedFrame, could be overriden.
036: * @return a boolean.
037: */
038: public boolean getConvertGetFlag() {
039: return false;
040: }
041:
042: /**
043: * Perform the request
044: * @param req The request to handle.
045: * @exception ProtocolException If processsing the request failed.
046: * @exception ResourceException If the resource got a fatal error.
047: */
048:
049: public ReplyInterface perform(RequestInterface req)
050: throws ProtocolException, ResourceException {
051: try {
052: return super .perform(req);
053: } catch (ProtocolException ex) {
054: throw ex;
055: } catch (ResourceException ex) {
056: throw ex;
057: } catch (Exception ex) {
058: Request request = (Request) req;
059: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
060: PageCompileOutputStream err = new PageCompileOutputStream();
061: PrintWriter writer = new PrintWriter(err);
062: writer.print("The generated frame at\n\n"
063: + request.getURL() + "\n\n"
064: + "reported this exception : \n\n"
065: + ex.getMessage() + "\n\nStack trace : \n\n");
066: ex.printStackTrace(writer);
067: writer.flush();
068: writer.close();
069: error.setStream(err.getInputStream());
070: error.setContentLength(err.size());
071: error.setContentType(MimeType.TEXT_PLAIN);
072: return error;
073: }
074: }
075:
076: /**
077: * The default GET method.
078: * @param request The request to handle.
079: * @exception ProtocolException If processsing the request failed.
080: * @exception ResourceException If the resource got a fatal error.
081: */
082: public Reply get(Request request) throws ProtocolException,
083: ResourceException {
084: if (getConvertGetFlag() && request.hasState("query")) {
085: String query = request.getQueryString();
086: InputStream in = new StringBufferInputStream(query);
087: URLDecoder d = new URLDecoder(in, getOverrideFlag());
088: try {
089: d.parse();
090: } catch (URLDecoderException e) {
091: Reply error = request.makeReply(HTTP.BAD_REQUEST);
092: error.setContent("Invalid request: "
093: + "unable to decode form data.");
094: throw new HTTPException(error);
095: } catch (IOException e) {
096: Reply error = request.makeReply(HTTP.BAD_REQUEST);
097: error
098: .setContent("Invalid request: unable to read form data.");
099: throw new HTTPException(error);
100: }
101: return handle(request, d);
102: }
103: Reply reply = createDefaultReply(request, HTTP.OK);
104: PageCompileOutputStream out = new PageCompileOutputStream();
105: try {
106: get(request, reply, out);
107: } catch (IOException ex) {
108: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
109: PageCompileOutputStream err = new PageCompileOutputStream();
110: PrintWriter writer = new PrintWriter(err);
111: writer.print("The generated frame at\n\n"
112: + request.getURL() + "\n\n"
113: + "reported this exception : \n\n"
114: + ex.getMessage() + "\n\nStack trace : \n\n");
115: ex.printStackTrace(writer);
116: writer.flush();
117: writer.close();
118: error.setStream(err.getInputStream());
119: error.setContentLength(err.size());
120: error.setContentType(MimeType.TEXT_PLAIN);
121: return error;
122: }
123: reply.setStream(out.getInputStream());
124: reply.setContentLength(out.size());
125: return reply;
126: }
127:
128: /**
129: * Handle the form submission, after posted data parsing.
130: * This methos always return "Method POST not allowed".
131: * @param request The request proper.
132: * @param reply The reply.
133: * @param data The parsed data content.
134: * @param out the output stream.
135: * @exception ProtocolException If form data processing failed.
136: * @exception IOException If an IO error occurs.
137: * @see org.w3c.jigsaw.forms.URLDecoder
138: */
139: protected void post(Request request, Reply reply, URLDecoder data,
140: PageCompileOutputStream out) throws ProtocolException,
141: IOException {
142: Reply error = request.makeReply(HTTP.NOT_ALLOWED);
143: if (allowed != null)
144: error.setHeaderValue(Reply.H_ALLOW, allowed);
145: error.setContent("Method POST not allowed on this resource.");
146: throw new HTTPException(error);
147: }
148:
149: /**
150: * Handle the form submission, after posted data parsing.
151: * @param request The request proper.
152: * @param data The parsed data content.
153: * @exception ProtocolException If form data processing failed.
154: * @see org.w3c.jigsaw.forms.URLDecoder
155: */
156:
157: public Reply handle(Request request, URLDecoder data)
158: throws ProtocolException {
159: PageCompileOutputStream out = new PageCompileOutputStream();
160: Reply reply = createDefaultReply(request, HTTP.OK);
161:
162: try {
163: post(request, reply, data, out);
164: } catch (IOException ex) {
165: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
166: PageCompileOutputStream err = new PageCompileOutputStream();
167: PrintWriter writer = new PrintWriter(err);
168: writer.print("The generated frame at\n\n"
169: + request.getURL() + "\n\n"
170: + "reported this exception : \n\n"
171: + ex.getMessage() + "\n\nStack trace : \n\n");
172: ex.printStackTrace(writer);
173: writer.flush();
174: writer.close();
175: error.setStream(err.getInputStream());
176: error.setContentLength(err.size());
177: error.setContentType(MimeType.TEXT_PLAIN);
178: return error;
179: }
180: reply.setStream(out.getInputStream());
181: reply.setContentLength(out.size());
182: return reply;
183: }
184:
185: /**
186: * All java code extracted between <java type=code> and </java> from
187: * the jhtml page will be put in this method body.
188: * @param request the incomming request.
189: * @param reply the reply.
190: * @param out the output stream.
191: * @exception IOException if an IO error occurs.
192: */
193: abstract protected void get(Request request, Reply reply,
194: PageCompileOutputStream out) throws IOException;
195: }
|