Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / frames / VirtualHostFrame.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/frames/VirtualHostFrame.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/frames/VirtualHostFrame.java
new file mode 100644 (file)
index 0000000..a283e44
--- /dev/null
@@ -0,0 +1,155 @@
+// VirtualHostFrame.java\r
+// $Id: VirtualHostFrame.java,v 1.2 2010/06/15 17:52:52 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1998.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.frames;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.AttributeRegistry;\r
+import org.w3c.tools.resources.StringAttribute;\r
+import org.w3c.tools.resources.ContainerResource;\r
+import org.w3c.tools.resources.FramedResource;\r
+import org.w3c.tools.resources.InvalidResourceException;\r
+import org.w3c.tools.resources.LookupResult;\r
+import org.w3c.tools.resources.LookupState;\r
+import org.w3c.tools.resources.ProtocolException;\r
+import org.w3c.tools.resources.ResourceReference;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+/**\r
+ * For Virtual Hosting.\r
+ */\r
+public class VirtualHostFrame extends HTTPFrame {\r
+\r
+    /**\r
+     * Attribute index - The default root (for unknown hosts)\r
+     */\r
+    protected static int ATTR_FOLLOWUP = -1;\r
+\r
+    static {\r
+       Class     c = null;\r
+       Attribute a = null;\r
+\r
+       try {\r
+           c = Class.forName("org.w3c.jigsaw.frames.VirtualHostFrame");\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+       } catch (Exception ex) {\r
+           ex.printStackTrace();\r
+           System.exit(1);\r
+       }\r
+       // Register our default root:\r
+       a = new StringAttribute("followup"\r
+                               , null\r
+                               , Attribute.EDITABLE);\r
+       ATTR_FOLLOWUP = AttributeRegistry.registerAttribute(c, a);\r
+    }\r
+\r
+    protected ResourceReference followup = null;\r
+\r
+    /**\r
+     * Get the name of the resource used as a followup.\r
+     * @return A String giving the name of the resource to be used as the\r
+     * default.\r
+     */\r
+\r
+    public String getFollowup() {\r
+       return getString(ATTR_FOLLOWUP, null);\r
+    }\r
+\r
+    public void registerResource(FramedResource resource) {\r
+       super.registerOtherResource(resource);\r
+    }\r
+\r
+    /**\r
+     * Lookup the followup resource.\r
+     * @return The loaded resource for the current followup.\r
+     */\r
+\r
+    public synchronized ResourceReference lookupFollowup() {\r
+       if ( followup == null ) {\r
+           String name  = getFollowup();\r
+           if ( name != null ) {\r
+               followup = getServer().loadRoot(name);\r
+           }\r
+           if ( followup == null ) {\r
+               getServer().errlog(getIdentifier()\r
+                                  + "[" + getClass().getName() + "]: "\r
+                                  + "unable to restore \"" + name + "\" "\r
+                                  + " from root store.");\r
+           }\r
+       }\r
+       return followup;\r
+    }\r
+\r
+    /**\r
+     * Lookup the target resource when associated with an unknown resource.\r
+     * @param ls The current lookup state\r
+     * @param lr The result\r
+     * @return true if lookup is done.\r
+     * @exception ProtocolException If an error relative to the protocol occurs\r
+     */\r
+    protected boolean lookupOther(LookupState ls, LookupResult lr) \r
+       throws ProtocolException\r
+    {\r
+       // Try to lookup on the host header:\r
+       ResourceReference vrroot = null;\r
+       ContainerResource root = null;\r
+       \r
+       root = (ContainerResource)getResource();\r
+       Request r = (Request)ls.getRequest();\r
+       if ( r != null ) {\r
+           String host = r.getURL().getHost();\r
+           String protocol = r.getURL().getProtocol();\r
+           if (host == null) {\r
+               host = r.getHost();\r
+               if ( host != null ) {\r
+                   // must strip the port if different from 80!\r
+                   if (host.endsWith(":80") && protocol.equals("http")) {\r
+                       host = host.substring(0, host.lastIndexOf(":80"));\r
+                   }\r
+                   // and the same for https (443)\r
+                   if (host.endsWith(":443") && protocol.equals("https")) {\r
+                       host = host.substring(0, host.lastIndexOf(":443"));\r
+                   }\r
+               }\r
+           } else {\r
+               int port = r.getURL().getPort();\r
+               if (port != -1) {\r
+                   if ( (protocol.equals("http") && (port != 80)) ||\r
+                        (protocol.equals("https") && (port != 443)) ) {\r
+                       host = host + ":" + port;\r
+                   }\r
+               }\r
+           }\r
+           if (host != null) {\r
+               vrroot = root.lookup(host.toLowerCase());\r
+           }\r
+       }\r
+       if ( vrroot == null ) {\r
+           vrroot  = lookupFollowup();\r
+       }\r
+       // Check for what we got:\r
+       if (vrroot == null) {\r
+           return super.lookupOther(ls, lr);\r
+       }\r
+       try {\r
+           lr.setTarget(vrroot);\r
+           FramedResource resource = (FramedResource) vrroot.lock();\r
+           boolean done = \r
+             (resource != null ) ? resource.lookup(ls, lr) : false;\r
+           if (! done) {\r
+               lr.setTarget(null);\r
+           }\r
+           // because the vroot eats the lookup state components\r
+           // we have to return true.\r
+           // Should not be continued by the caller.\r
+           return true;\r
+       } catch (InvalidResourceException ex) {\r
+           return false;\r
+       } finally {\r
+           vrroot.unlock();\r
+       }\r
+    }\r
+}\r