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