Annotation of java/classes/org/w3c/jigsaw/servlet/ServletMapperFrame.java, revision 1.9
1.1 bmahe 1: // ServletMapperFrame.java
1.9 ! bmahe 2: // $Id: ServletMapperFrame.java,v 1.8 2000/04/21 08:07:42 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.util.*;
9: import java.io.* ;
10: import java.net.*;
11:
12: import org.w3c.tools.resources.*;
13: import org.w3c.jigsaw.http.*;
14: import org.w3c.jigsaw.frames.*;
15: import org.w3c.www.http.*;
16:
17: import org.w3c.tools.resources.ProtocolException;
18: import org.w3c.tools.resources.ResourceException;
19:
20: /**
21: * Perform an internal redirect.
22: */
23: public class ServletMapperFrame extends HTTPFrame {
24: /**
25: * Attributes index - The index for the target attribute.
26: */
27: protected static int ATTR_TARGET = -1 ;
28:
29: static {
30: Attribute a = null ;
31: Class cls = null ;
32: // Get a pointer to our class:
33: try {
34: cls = Class.forName("org.w3c.jigsaw.servlet.ServletMapperFrame") ;
35: } catch (Exception ex) {
36: ex.printStackTrace() ;
37: System.exit(1) ;
38: }
39: a = new StringAttribute("servlet-url"
40: , null
41: , Attribute.EDITABLE);
42: ATTR_TARGET = AttributeRegistry.registerAttribute(cls, a) ;
43: }
44:
45: protected String getTarget() {
46: return (String) getValue(ATTR_TARGET, null);
47: }
48:
49: /**
1.7 bmahe 50: * Gets, from the first line of the HTTP request,
51: * the part of this request's URI that is to the left of any query string.
52: */
53: public String getRequestURI(Request request)
54: {
55: String uri = null;
56: //fixme test
57: if (request.isProxy()) {
58: uri = request.getURL().toExternalForm();
59: } else {
60: uri = request.getURLPath();
61: }
62: if (request.hasQueryString()) {
63: String query = request.getQueryString();
64: int idx = uri.lastIndexOf(query);
1.8 bmahe 65: if (idx != -1) {
66: uri = uri.substring(0, idx-1);
67: }
1.7 bmahe 68: }
69: return uri;
70: }
71:
72: /**
1.1 bmahe 73: * Perform the request.
74: * @param req The request to handle.
75: * @exception ProtocolException If request couldn't be processed.
76: * @exception ResourceException If the resource got a fatal error.
77: */
78: public ReplyInterface perform(RequestInterface req)
79: throws ProtocolException, ResourceException
80: {
81: Reply reply = (Reply) performFrames(req);
82: if (reply != null)
83: return reply;
84: Request request = (Request) req;
85: httpd server = (httpd) getServer();
86: String host = request.getHost();
1.7 bmahe 87: request.setState(JigsawRequestDispatcher.REQUEST_URI_P,
88: getRequestURI(request));
89: request.setState(JigsawRequestDispatcher.QUERY_STRING_P,
90: request.getQueryString());
1.6 bmahe 91: request.setState(JigsawRequestDispatcher.SERVLET_PATH_P, getURLPath());
1.1 bmahe 92: try {
93: String target = null;
94: if (request.hasQueryString())
95: target = getTarget()+"?"+request.getQueryString();
96: else
97: target = getTarget();
98:
99: if (host == null)
100: request.setURL(new URL(server.getURL(), target));
101: else
102: request.setURL(new URL(server.getURL().getProtocol(),
103: host, target));
1.3 bmahe 104: request.setInternal(true);
1.1 bmahe 105: } catch (MalformedURLException ex) {
106: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
107: error.setContent("<html><head><title>Server Error</title>"+
108: "</head><body><h1>Server misconfigured</h1>"+
109: "<p>The resource <b>"+getIdentifier()+"</b>"+
110: "has an invalid target attribute : <p><b>"+
111: getTarget()+"</b></body></html>");
112: throw new HTTPException (error);
113: }
114: return server.perform(request);
1.9 ! bmahe 115: }
! 116:
! 117: protected boolean lookupResource(LookupState ls, LookupResult lr)
! 118: throws ProtocolException
! 119: {
! 120: // Get the extra path information:
! 121: String extraPath = ls.getRemainingPath(true);
! 122: if ((extraPath == null) || extraPath.equals(""))
! 123: extraPath = "/";
! 124: // Keep this path info into the request, if possible:
! 125: Request request = (Request) ls.getRequest();
! 126: if ( request != null ) {
! 127: if (request.getState(JigsawRequestDispatcher.PATH_INFO_P) == null)
! 128: request.setState(JigsawRequestDispatcher.PATH_INFO_P,
! 129: extraPath);
! 130: }
! 131: lr.setTarget(resource.getResourceReference());
! 132: return super.lookupResource(ls, lr);
1.1 bmahe 133: }
134: }
Webmaster