Annotation of java/classes/org/w3c/jigsaw/servlet/ServletWrapperFrame.java, revision 1.16
1.1 bmahe 1: // ServletWrapperFrame.java
1.16 ! ylafon 2: // $Id: ServletWrapperFrame.java,v 1.15 2000/08/16 21:37:46 ylafon Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.servlet;
7:
1.15 ylafon 8: import javax.servlet.ServletException;
9: import javax.servlet.UnavailableException;
1.1 bmahe 10:
1.16 ! ylafon 11: import java.io.PipedInputStream;
! 12: import java.io.PipedOutputStream;
! 13:
1.15 ylafon 14: import org.w3c.www.mime.MimeType;
1.1 bmahe 15:
1.15 ylafon 16: import org.w3c.www.http.HTTP;
17: import org.w3c.www.http.HttpEntityMessage;
18: import org.w3c.www.http.HttpMessage;
19: import org.w3c.www.http.HttpReplyMessage;
20:
21: import org.w3c.jigsaw.http.HTTPException;
22: import org.w3c.jigsaw.http.Reply;
23: import org.w3c.jigsaw.http.Request;
24:
25: import org.w3c.jigsaw.frames.HTTPFrame;
26:
27: import org.w3c.tools.resources.FramedResource;
28: import org.w3c.tools.resources.LookupResult;
29: import org.w3c.tools.resources.LookupState;
30: import org.w3c.tools.resources.ProtocolException;
31: import org.w3c.tools.resources.ReplyInterface;
32: import org.w3c.tools.resources.RequestInterface;
33: import org.w3c.tools.resources.Resource;
34: import org.w3c.tools.resources.ResourceException;
35: import org.w3c.tools.resources.ResourceFrame;
1.1 bmahe 36:
1.2 bmahe 37: import org.w3c.tools.resources.ProtocolException;
1.11 bmahe 38: import org.w3c.tools.resources.ResourceException;
1.2 bmahe 39:
1.1 bmahe 40: /**
41: * @author Alexandre Rafalovitch <alex@access.com.au>
42: * @author Anselm Baird-Smith <abaird@w3.org>
43: * @author Benoit Mahe <bmahe@w3.org>
44: */
45:
46: public class ServletWrapperFrame extends HTTPFrame {
47:
1.5 ylafon 48: protected ServletWrapper wrapper = null;
1.1 bmahe 49:
1.9 bmahe 50: /**
51: * Register our resource. Must be an instance of ServletWrapper.
52: */
1.5 ylafon 53: public void registerResource(FramedResource resource) {
54: super.registerOtherResource(resource);
55: if (resource instanceof ServletWrapper)
56: wrapper = (ServletWrapper) resource;
1.1 bmahe 57: }
58:
1.5 ylafon 59: /**
60: * Create a reply to answer to request on this file.
61: * This method will create a suitable reply (matching the given request)
62: * and will set all its default header values to the appropriate
63: * values. The reply will not have LastModified field setted.
64: * @param request The request to make a reply for.
65: * @return An instance of Reply, suited to answer this request.
66: */
67:
68: public Reply createDefaultReply(Request request, int status) {
69: Reply reply = super.createDefaultReply(request, status);
70: reply.setLastModified( -1 );
71: return reply;
72: }
1.1 bmahe 73:
1.5 ylafon 74: /**
75: * Dispatch the give request to our servlet.
76: * <p>If the servlet cannot be inititalized, we just throw an error message
77: * otherwise, we just delegate that request processing to the underlying
78: * servlet instance.
79: * @param request The request to be processed.
80: * @exception ProtocolException If the wrapped servlet is not initialized.
1.11 bmahe 81: * @exception ResourceException If the resource got a fatal error.
1.5 ylafon 82: */
83:
84: public ReplyInterface perform(RequestInterface req)
1.11 bmahe 85: throws ProtocolException, ResourceException
1.5 ylafon 86: {
87: ReplyInterface repi = performFrames(req);
88: if (repi != null)
89: return repi;
90:
91: if (! checkRequest(req))
92: return null;
93:
94: Request request = (Request) req;
95:
96: if (wrapper == null) {
97: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
98: reply.setContent("Servlet Wrapper Frame not configured properly: "+
99: "must be attached to a ServletWrapper.");
100: throw new HTTPException(reply);
101: }
102:
1.10 bmahe 103: try {
104: wrapper.checkServlet();
105: } catch (ClassNotFoundException ex) {
106: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
107: reply.setContent("The server was unable to find the "+
108: "servlet class : "+ex.getMessage());
109: if ( wrapper.debug )
110: ex.printStackTrace();
111: throw new HTTPException(reply);
112: } catch (ServletException ex) {
113: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
114: reply.setContent("The server was unable to initialize the "+
115: "servlet : "+ex.getMessage());
116: if ( wrapper.debug )
117: ex.printStackTrace();
118: throw new HTTPException(reply);
119: }
1.5 ylafon 120:
1.8 bmahe 121: // Check that the servlet has been initialized properly:
1.5 ylafon 122: if ( ! wrapper.isInited() ) {
123: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
124: reply.setContent("Servlet not configured properly");
125: throw new HTTPException(reply);
126: }
127: // Dispatch the request:
128: Reply reply = createDefaultReply(request, HTTP.OK);
129: reply.setContentType(MimeType.TEXT_HTML);
130: try {
1.16 ! ylafon 131: PipedInputStream pis = new PipedInputStream();
! 132: PipedOutputStream pos = new PipedOutputStream(pis);
! 133: reply.setState(JigsawHttpServletResponse.STREAM, pos);
! 134: reply.setStream(pis);
! 135: Object o = new Object();
! 136: reply.setState(JigsawHttpServletResponse.MONITOR, o);
! 137: // wait until the reply is constructed by the processing thread
! 138: synchronized (o) {
! 139: wrapper.service(request, reply);
! 140: o.wait();
! 141: }
1.6 bmahe 142: } catch (UnavailableException uex) {
1.13 ylafon 143: reply = request.makeReply(HTTP.SERVICE_UNAVAILABLE);
1.6 bmahe 144: if (uex.isPermanent()) {
145: reply.setContent("<h2>The servlet is permanently "+
146: "unavailable :</h2>"+
147: "Details: <b>"+uex.getMessage()+"</b>");
148: } else {
149: int delay = uex.getUnavailableSeconds();
1.13 ylafon 150: if (delay > 0) {
151: reply.setRetryAfter(delay);
1.6 bmahe 152: reply.setContent("<h2>The servlet is temporarily "+
153: "unavailable :</h2>"+
1.7 bmahe 154: "Delay : "+delay+
1.6 bmahe 155: " seconds<br><br>Details: <b>"+
156: uex.getMessage()+"</b>");
1.13 ylafon 157: } else {
1.6 bmahe 158: reply.setContent("<h2>The servlet is temporarily "+
159: "unavailable :</h2>"+
160: "Details: <b>"+uex.getMessage()+"</b>");
1.13 ylafon 161: }
1.6 bmahe 162: }
1.5 ylafon 163: } catch (Exception ex) {
164: if ( wrapper.debug )
165: ex.printStackTrace();
166: reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
167: reply.setContent("Servlet has thrown exception:" + ex.toString());
168: }
169: return reply;
1.1 bmahe 170: }
1.15 ylafon 171:
1.5 ylafon 172: /**
173: * Jigsaw's lookup on servlets.
174: * Once here, we have reached a leaf servlet (or at least the remaining
175: * lookup is to be done at the servlet itself). We keep track of the
176: * <em>path info</em> and mark that servlet as the target of request.
177: * @param ls The lookup state.
178: * @param lr The lookup result.
179: * @exception ProtocolException If some error occurs.
180: */
181:
182: protected boolean lookupOther(LookupState ls, LookupResult lr)
183: throws ProtocolException
184: {
185: // Get the extra path information:
186: String extraPath = ls.getRemainingPath(true);
187: if ((extraPath == null) || extraPath.equals(""))
188: extraPath = "/";
189: // Keep this path info into the request, if possible:
190: Request request = (Request) ls.getRequest();
1.12 bmahe 191: if ( request != null ) {
1.14 bmahe 192: if (request.getState(JigsawRequestDispatcher.PATH_INFO_P) == null)
193: request.setState(JigsawRequestDispatcher.PATH_INFO_P,
194: extraPath);
1.12 bmahe 195: }
1.5 ylafon 196: lr.setTarget(resource.getResourceReference());
197: return super.lookupOther(ls, lr);
198: }
1.1 bmahe 199:
200: }
Webmaster