Annotation of java/classes/org/w3c/jigsaw/servlet/ServletWrapperFrame.java, revision 1.7
1.1 bmahe 1: // ServletWrapperFrame.java
1.7 ! bmahe 2: // $Id: ServletWrapperFrame.java,v 1.6 1998/05/26 10:02:22 bmahe 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:
8: import java.io.* ;
9: import java.util.*;
10:
11: import javax.servlet.*;
12:
13: import org.w3c.tools.resources.store.*;
14: import org.w3c.util.*;
15: import org.w3c.www.mime.*;
16: import org.w3c.www.http.*;
17: import org.w3c.jigsaw.http.*;
18: import org.w3c.jigsaw.frames.*;
19: import org.w3c.tools.resources.*;
20:
1.2 bmahe 21: import org.w3c.tools.resources.ProtocolException;
22: import org.w3c.tools.resources.NotAProtocolException;
23:
1.1 bmahe 24: /**
25: * @author Alexandre Rafalovitch <alex@access.com.au>
26: * @author Anselm Baird-Smith <abaird@w3.org>
27: * @author Benoit Mahe <bmahe@w3.org>
28: */
29:
30: public class ServletWrapperFrame extends HTTPFrame {
31:
1.5 ylafon 32: public static final
33: String STATE_EXTRA_PATH = "org.w3c.jigsaw.servlet.extraPath";
1.1 bmahe 34:
1.5 ylafon 35: protected ServletWrapper wrapper = null;
1.1 bmahe 36:
1.5 ylafon 37: public void registerResource(FramedResource resource) {
38: super.registerOtherResource(resource);
39: if (resource instanceof ServletWrapper)
40: wrapper = (ServletWrapper) resource;
1.1 bmahe 41: }
42:
1.5 ylafon 43: /**
44: * Create a reply to answer to request on this file.
45: * This method will create a suitable reply (matching the given request)
46: * and will set all its default header values to the appropriate
47: * values. The reply will not have LastModified field setted.
48: * @param request The request to make a reply for.
49: * @return An instance of Reply, suited to answer this request.
50: */
51:
52: public Reply createDefaultReply(Request request, int status) {
53: Reply reply = super.createDefaultReply(request, status);
54: reply.setLastModified( -1 );
55: return reply;
56: }
1.1 bmahe 57:
1.5 ylafon 58: /**
59: * Dispatch the give request to our servlet.
60: * <p>If the servlet cannot be inititalized, we just throw an error message
61: * otherwise, we just delegate that request processing to the underlying
62: * servlet instance.
63: * @param request The request to be processed.
64: * @exception ProtocolException If the wrapped servlet is not initialized.
65: */
66:
67: public ReplyInterface perform(RequestInterface req)
68: throws ProtocolException, NotAProtocolException
69: {
70: ReplyInterface repi = performFrames(req);
71: if (repi != null)
72: return repi;
73:
74: if (! checkRequest(req))
75: return null;
76:
77: Request request = (Request) req;
78:
79: if (wrapper == null) {
80: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
81: reply.setContent("Servlet Wrapper Frame not configured properly: "+
82: "must be attached to a ServletWrapper.");
83: throw new HTTPException(reply);
84: }
85:
86: wrapper.checkServletClass();
87:
88: // Check that the servlet has been initialized properly:
89: if ( ! wrapper.isInited() ) {
90: Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
91: reply.setContent("Servlet not configured properly");
92: throw new HTTPException(reply);
93: }
94: // Dispatch the request:
95: Reply reply = createDefaultReply(request, HTTP.OK);
96: reply.setContentType(MimeType.TEXT_HTML);
97: try {
98: wrapper.service(request, reply);
1.6 bmahe 99: } catch (UnavailableException uex) {
100: reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
101: if (uex.isPermanent()) {
102: reply.setContent("<h2>The servlet is permanently "+
103: "unavailable :</h2>"+
104: "Details: <b>"+uex.getMessage()+"</b>");
105: } else {
106: int delay = uex.getUnavailableSeconds();
107: if (delay > 0)
108: reply.setContent("<h2>The servlet is temporarily "+
109: "unavailable :</h2>"+
1.7 ! bmahe 110: "Delay : "+delay+
1.6 bmahe 111: " seconds<br><br>Details: <b>"+
112: uex.getMessage()+"</b>");
113: else
114: reply.setContent("<h2>The servlet is temporarily "+
115: "unavailable :</h2>"+
116: "Details: <b>"+uex.getMessage()+"</b>");
117: }
1.5 ylafon 118: } catch (Exception ex) {
119: if ( wrapper.debug )
120: ex.printStackTrace();
121: reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
122: reply.setContent("Servlet has thrown exception:" + ex.toString());
123: }
124: return reply;
1.1 bmahe 125: }
126:
1.5 ylafon 127: /**
128: * Jigsaw's lookup on servlets.
129: * Once here, we have reached a leaf servlet (or at least the remaining
130: * lookup is to be done at the servlet itself). We keep track of the
131: * <em>path info</em> and mark that servlet as the target of request.
132: * @param ls The lookup state.
133: * @param lr The lookup result.
134: * @exception ProtocolException If some error occurs.
135: */
136:
137: protected boolean lookupOther(LookupState ls, LookupResult lr)
138: throws ProtocolException
139: {
140: // Get the extra path information:
141: String extraPath = ls.getRemainingPath(true);
142: if ((extraPath == null) || extraPath.equals(""))
143: extraPath = "/";
144: // Keep this path info into the request, if possible:
145: Request request = (Request) ls.getRequest();
146: if ( request != null )
147: request.setState(STATE_EXTRA_PATH, extraPath);
148: lr.setTarget(resource.getResourceReference());
149: return super.lookupOther(ls, lr);
150: }
1.1 bmahe 151:
152: }
153:
154:
155:
156:
157:
158:
159:
160:
Webmaster