001: // ServletMapperFrame.java
002: // $Id: ServletMapperFrame.java,v 1.13 2001/11/12 13:57:15 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010:
011: import org.w3c.tools.resources.Attribute;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.LookupResult;
014: import org.w3c.tools.resources.LookupState;
015: import org.w3c.tools.resources.ProtocolException;
016: import org.w3c.tools.resources.ReplyInterface;
017: import org.w3c.tools.resources.RequestInterface;
018: import org.w3c.tools.resources.Resource;
019: import org.w3c.tools.resources.ResourceException;
020: import org.w3c.tools.resources.ResourceFrame;
021: import org.w3c.tools.resources.StringAttribute;
022:
023: import org.w3c.jigsaw.http.HTTPException;
024: import org.w3c.jigsaw.http.Reply;
025: import org.w3c.jigsaw.http.Request;
026: import org.w3c.jigsaw.http.httpd;
027:
028: import org.w3c.jigsaw.frames.HTTPFrame;
029:
030: import org.w3c.www.http.HTTP;
031: import org.w3c.www.http.HttpMessage;
032: import org.w3c.www.http.HttpRequestMessage;
033:
034: import org.w3c.tools.resources.ProtocolException;
035: import org.w3c.tools.resources.ResourceException;
036:
037: /**
038: * Perform an internal redirect.
039: */
040: public class ServletMapperFrame extends HTTPFrame {
041: /**
042: * Attributes index - The index for the target attribute.
043: */
044: protected static int ATTR_TARGET = -1;
045:
046: static {
047: Attribute a = null;
048: Class cls = null;
049: // Get a pointer to our class:
050: try {
051: cls = Class
052: .forName("org.w3c.jigsaw.servlet.ServletMapperFrame");
053: } catch (Exception ex) {
054: ex.printStackTrace();
055: System.exit(1);
056: }
057: a = new StringAttribute("servlet-url", null, Attribute.EDITABLE);
058: ATTR_TARGET = AttributeRegistry.registerAttribute(cls, a);
059: }
060:
061: protected String getTarget() {
062: return (String) getValue(ATTR_TARGET, null);
063: }
064:
065: /**
066: * Gets, from the first line of the HTTP request,
067: * the part of this request's URI that is to the left of any query string.
068: */
069: public String getRequestURI(Request request) {
070: String uri = null;
071: //fixme test
072: if (request.isProxy()) {
073: uri = request.getURL().toExternalForm();
074: } else {
075: uri = request.getURLPath();
076: }
077: if (request.hasQueryString()) {
078: String query = request.getQueryString();
079: int idx = uri.lastIndexOf(query);
080: if (idx != -1) {
081: uri = uri.substring(0, idx - 1);
082: }
083: }
084: return uri;
085: }
086:
087: /**
088: * Perform the request.
089: * @param req The request to handle.
090: * @exception ProtocolException If request couldn't be processed.
091: * @exception ResourceException If the resource got a fatal error.
092: */
093: public ReplyInterface perform(RequestInterface req)
094: throws ProtocolException, ResourceException {
095: Reply reply = (Reply) performFrames(req);
096: if (reply != null)
097: return reply;
098: Request request = (Request) req;
099: httpd server = (httpd) getServer();
100: String host = request.getHost();
101: request.setState(Request.ORIG_URL_STATE, request.getURL());
102: request.setState(JigsawRequestDispatcher.REQUEST_URI_P,
103: getRequestURI(request));
104: request.setState(JigsawRequestDispatcher.QUERY_STRING_P,
105: request.getQueryString());
106: request.setState(JigsawRequestDispatcher.SERVLET_PATH_P,
107: getURLPath());
108: try {
109: String target = null;
110: if (request.hasQueryString())
111: target = getTarget() + "?" + request.getQueryString();
112: else
113: target = getTarget();
114:
115: if (host == null) {
116: request.setURL(new URL(server.getURL(), target));
117: } else {
118: int ic = host.indexOf(':');
119: // we will take care of '[' later (ipv6 address)
120: if (ic < 0) {
121: request.setURL(new URL(server.getURL()
122: .getProtocol(), host, target));
123: } else {
124: request.setURL(new URL(server.getURL()
125: .getProtocol(), host.substring(0, ic),
126: Integer.parseInt(host.substring(ic + 1)),
127: target));
128: }
129: }
130: request.setInternal(true);
131: } catch (MalformedURLException ex) {
132: Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
133: error.setContent("<html><head><title>Server Error</title>"
134: + "</head><body><h1>Server misconfigured</h1>"
135: + "<p>The resource <b>" + getIdentifier() + "</b>"
136: + "has an invalid target attribute : <p><b>"
137: + getTarget() + "</b></body></html>");
138: throw new HTTPException(error);
139: }
140: return server.perform(request);
141: }
142:
143: protected boolean lookupResource(LookupState ls, LookupResult lr)
144: throws ProtocolException {
145: // Get the extra path information:
146: String extraPath = ls.getRemainingPath(true);
147: if ((extraPath == null) || extraPath.equals(""))
148: extraPath = "/";
149: // Keep this path info into the request, if possible:
150: Request request = (Request) ls.getRequest();
151: if (request != null) {
152: if (request.getState(JigsawRequestDispatcher.PATH_INFO_P) == null)
153: request.setState(JigsawRequestDispatcher.PATH_INFO_P,
154: extraPath);
155: }
156: lr.setTarget(resource.getResourceReference());
157: return super.lookupResource(ls, lr);
158: }
159: }
|