001: // VirtualHostFrame.java
002: // $Id: VirtualHostFrame.java,v 1.13 2000/09/05 17:06:49 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.frames;
007:
008: import org.w3c.tools.resources.Attribute;
009: import org.w3c.tools.resources.AttributeRegistry;
010: import org.w3c.tools.resources.StringAttribute;
011: import org.w3c.tools.resources.ContainerResource;
012: import org.w3c.tools.resources.FramedResource;
013: import org.w3c.tools.resources.InvalidResourceException;
014: import org.w3c.tools.resources.LookupResult;
015: import org.w3c.tools.resources.LookupState;
016: import org.w3c.tools.resources.ProtocolException;
017: import org.w3c.tools.resources.ResourceReference;
018: import org.w3c.jigsaw.http.Request;
019:
020: /**
021: * For Virtual Hosting.
022: */
023: public class VirtualHostFrame extends HTTPFrame {
024:
025: /**
026: * Attribute index - The default root (for unknown hosts)
027: */
028: protected static int ATTR_FOLLOWUP = -1;
029:
030: static {
031: Class c = null;
032: Attribute a = null;
033:
034: try {
035: c = Class.forName("org.w3c.jigsaw.frames.VirtualHostFrame");
036: } catch (Exception ex) {
037: ex.printStackTrace();
038: System.exit(1);
039: }
040: // Register our default root:
041: a = new StringAttribute("followup", null, Attribute.EDITABLE);
042: ATTR_FOLLOWUP = AttributeRegistry.registerAttribute(c, a);
043: }
044:
045: protected ResourceReference followup = null;
046:
047: /**
048: * Get the name of the resource used as a followup.
049: * @return A String giving the name of the resource to be used as the
050: * default.
051: */
052:
053: public String getFollowup() {
054: return getString(ATTR_FOLLOWUP, null);
055: }
056:
057: public void registerResource(FramedResource resource) {
058: super .registerOtherResource(resource);
059: }
060:
061: /**
062: * Lookup the followup resource.
063: * @return The loaded resource for the current followup.
064: */
065:
066: public synchronized ResourceReference lookupFollowup() {
067: if (followup == null) {
068: String name = getFollowup();
069: if (name != null) {
070: followup = getServer().loadRoot(name);
071: }
072: if (followup == null) {
073: getServer().errlog(
074: getIdentifier() + "[" + getClass().getName()
075: + "]: " + "unable to restore \"" + name
076: + "\" " + " from root store.");
077: }
078: }
079: return followup;
080: }
081:
082: /**
083: * Lookup the target resource when associated with an unknown resource.
084: * @param ls The current lookup state
085: * @param lr The result
086: * @return true if lookup is done.
087: * @exception ProtocolException If an error relative to the protocol occurs
088: */
089: protected boolean lookupOther(LookupState ls, LookupResult lr)
090: throws ProtocolException {
091: // Try to lookup on the host header:
092: ResourceReference vrroot = null;
093: ContainerResource root = null;
094:
095: root = (ContainerResource) getResource();
096: Request r = (Request) ls.getRequest();
097: if (r != null) {
098: String host = r.getURL().getHost();
099: String protocol = r.getURL().getProtocol();
100: if (host == null) {
101: host = r.getHost();
102: if (host != null) {
103: // must strip the port if different from 80!
104: if (host.endsWith(":80") && protocol.equals("http")) {
105: host = host.substring(0, host
106: .lastIndexOf(":80"));
107: }
108: // and the same for https (443)
109: if (host.endsWith(":443")
110: && protocol.equals("https")) {
111: host = host.substring(0, host
112: .lastIndexOf(":443"));
113: }
114: }
115: } else {
116: int port = r.getURL().getPort();
117: if (port != -1) {
118: if ((protocol.equals("http") && (port != 80))
119: || (protocol.equals("https") && (port != 443))) {
120: host = host + ":" + port;
121: }
122: }
123: }
124: if (host != null) {
125: vrroot = root.lookup(host.toLowerCase());
126: }
127: }
128: if (vrroot == null) {
129: vrroot = lookupFollowup();
130: }
131: // Check for what we got:
132: if (vrroot == null) {
133: return super .lookupOther(ls, lr);
134: }
135: try {
136: lr.setTarget(vrroot);
137: FramedResource resource = (FramedResource) vrroot.lock();
138: boolean done = (resource != null) ? resource.lookup(ls, lr)
139: : false;
140: if (!done) {
141: lr.setTarget(null);
142: }
143: // because the vroot eats the lookup state components
144: // we have to return true.
145: // Should not be continued by the caller.
146: return true;
147: } catch (InvalidResourceException ex) {
148: return false;
149: } finally {
150: vrroot.unlock();
151: }
152: }
153: }
|