On 12/14/2009 03:58 PM, Michael McMahon wrote:
The alternative is to select something O(1)-ish but this can
drastically limit what is possible. Though like I said, for my
purposes if you would allow for host name ("foo.bar.com") and a simple
pattern mechanism ("*.bar.com" but not, say, "foo.*.com"), that'd be
OK and it would still let you have an O(1)-ish implementation (e.g.
split by ".", evalulate segments right-to-left, depth first, exact
match first, wildcard match second so longest match wins, not too
unlike how contexts are matched I guess).

I think this might be simpler.

OK, so how about this patch (attached, again just covers the API)?

- DML
diff -r dde3fe2e8164 src/share/classes/com/sun/net/httpserver/HttpHost.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/com/sun/net/httpserver/HttpHost.java	Mon Dec 14 16:22:37 2009 -0600
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package com.sun.net.httpserver;
+
+/**
+ * This class implements a simple HTTP host. A HttpHost may be "real" or "virtual".
+ * A "real" host is one which is bound to an IP address and extends the {...@link HttpServer}
+ * subclass.  A "virtual" host allows for more than one set of registered contexts
+ * per physical server, differentiated by host name, IP address, or other criteria.
+ * <p>
+ * One or more {...@link HttpHandler} objects must be associated with a host
+ * in order to process requests. Each such HttpHandler is registered
+ * with a root URI path which represents the
+ * location of the application or service on this server. The mapping of a handler
+ * to a {...@code HttpHost} is essentially equivalent to the mapping of a handler to a
+ * {...@link HttpServer}.
+ *
+ * @since 1.7
+ */
+public abstract class HttpHost {
+    /**
+     * Creates a {...@code HttpContext}. A {...@code HttpContext} represents a mapping from a
+     * URI path to a exchange handler on this {...@code HttpHost}. Once created, all requests
+     * received by the server for the path will be handled by calling
+     * the given handler object. The context is identified by the path, and
+     * can later be removed from the server using this with the {...@link #removeContext(String)} method.
+     * <p>
+     * The path specifies the root URI path for this context. The first character of path must be
+     * {...@code '/'}. <p>
+     * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
+     * to HttpContext instances.
+     * @param path the root URI path to associate the context with
+     * @param handler the handler to invoke for incoming requests.
+     * @throws IllegalArgumentException if path is invalid, or if a context
+     *          already exists for this path
+     * @throws NullPointerException if either path, or handler are <code>null</code>
+     */
+    public abstract HttpContext createContext (String path, HttpHandler handler) ;
+
+    /**
+     * Creates a {...@code HttpContext} without initially specifying a handler. The handler must later be specified using
+     * {...@link com.sun.net.httpserver.HttpContext#setHandler(com.sun.net.httpserver.HttpHandler)}.  A {...@code HttpContext} represents a mapping from a
+     * URI path to an exchange handler on this {...@code HttpHost}. Once created, and when
+     * the handler has been set, all requests
+     * received by the server for the path will be handled by calling
+     * the handler object. The context is identified by the path, and
+     * can later be removed from the server using this with the {...@link #removeContext(String)} method.
+     * <p>
+     * The path specifies the root URI path for this context. The first character of path must be
+     * {...@code '/'}. <p>
+     * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
+     * to HttpContext instances.
+     * @param path the root URI path to associate the context with
+     * @throws IllegalArgumentException if path is invalid, or if a context
+     *          already exists for this path
+     * @throws NullPointerException if path is <code>null</code>
+     */
+    public abstract HttpContext createContext (String path) ;
+
+    /**
+     * Removes the context identified by the given path from the server.
+     * Removing a context does not affect exchanges currently being processed
+     * but prevents new ones from being accepted.
+     * @param path the path of the handler to remove
+     * @throws IllegalArgumentException if no handler corresponding to this
+     *          path exists.
+     * @throws NullPointerException if path is <code>null</code>
+     */
+    public abstract void removeContext (String path) throws IllegalArgumentException ;
+
+    /**
+     * Removes the given context from the server.
+     * Removing a context does not affect exchanges currently being processed
+     * but prevents new ones from being accepted.
+     * @param context the context to remove
+     * @throws IllegalArgumentException if the given context is not registered on this host
+     * @throws NullPointerException if context is <code>null</code>
+     */
+    public abstract void removeContext (HttpContext context)  throws IllegalArgumentException ;
+}
diff -r dde3fe2e8164 src/share/classes/com/sun/net/httpserver/HttpServer.java
--- a/src/share/classes/com/sun/net/httpserver/HttpServer.java	Wed Feb 25 14:32:01 2009 +0000
+++ b/src/share/classes/com/sun/net/httpserver/HttpServer.java	Mon Dec 14 16:22:37 2009 -0600
@@ -87,7 +87,7 @@
  * @since 1.6
  */
 
-public abstract class HttpServer {
+public abstract class HttpServer extends HttpHost {
 
     /**
      */
@@ -187,64 +187,59 @@
      */
     public abstract void stop (int delay);
 
-    /**
-     * Creates a HttpContext. A HttpContext represents a mapping from a
-     * URI path to a exchange handler on this HttpServer. Once created, all requests
-     * received by the server for the path will be handled by calling
-     * the given handler object. The context is identified by the path, and
-     * can later be removed from the server using this with the {...@link #removeContext(String)} method.
-     * <p>
-     * The path specifies the root URI path for this context. The first character of path must be
-     * '/'. <p>
-     * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
-     * to HttpContext instances.
-     * @param path the root URI path to associate the context with
-     * @param handler the handler to invoke for incoming requests.
-     * @throws IllegalArgumentException if path is invalid, or if a context
-     *          already exists for this path
-     * @throws NullPointerException if either path, or handler are <code>null</code>
-     */
-    public abstract HttpContext createContext (String path, HttpHandler handler) ;
+    /** {...@inheritdoc} */
+    public abstract HttpContext createContext (final String path, final HttpHandler handler);
+
+    /** {...@inheritdoc} */
+    public abstract HttpContext createContext (final String path);
+
+    /** {...@inheritdoc} */
+    public abstract void removeContext (final String path) throws IllegalArgumentException;
+
+    /** {...@inheritdoc} */
+    public abstract void removeContext (final HttpContext context) throws IllegalArgumentException;
 
     /**
-     * Creates a HttpContext without initially specifying a handler. The handler must later be specified using
-     * {...@link HttpContext#setHandler(HttpHandler)}.  A HttpContext represents a mapping from a
-     * URI path to an exchange handler on this HttpServer. Once created, and when
-     * the handler has been set, all requests
-     * received by the server for the path will be handled by calling
-     * the handler object. The context is identified by the path, and
-     * can later be removed from the server using this with the {...@link #removeContext(String)} method.
+     * Creates a virtual host with the given pattern.  A pattern is a sequence of
+     * host name segments seprated by <code>"."</code>, optionally preceded by a
+     * <code>"*."</code> wildcard designator which means "any host or sequence of
+     * hosts".
      * <p>
-     * The path specifies the root URI path for this context. The first character of path must be
-     * '/'. <p>
-     * The class overview describes how incoming request URIs are <a href="#mapping_description">mapped</a>
-     * to HttpContext instances.
-     * @param path the root URI path to associate the context with
-     * @throws IllegalArgumentException if path is invalid, or if a context
-     *          already exists for this path
-     * @throws NullPointerException if path is <code>null</code>
+     * The base implementation throws {...@code java.lang.UnsupportedOperationException}
+     * to avoid breaking backwards compatibility with existing implementations.
+     *
+     * @param pattern the virtual host pattern to match
+     * @return the virtual host
+     *
+     * @since 1.7
      */
-    public abstract HttpContext createContext (String path) ;
+    public HttpHost createVirtualHost(final String pattern) {
+        throw new UnsupportedOperationException("Virutal hosts not supported by this implementation");
+    }
 
     /**
-     * Removes the context identified by the given path from the server.
-     * Removing a context does not affect exchanges currently being processed
-     * but prevents new ones from being accepted.
-     * @param path the path of the handler to remove
-     * @throws IllegalArgumentException if no handler corresponding to this
-     *          path exists.
-     * @throws NullPointerException if path is <code>null</code>
+     * Removes the virtual host identified by the given pattern from the server.
+     * Removing a virtual host does not affect exchanges currently being
+     * processed but prevents new ones from being accepted.
+     *
+     * @param pattern the virtual host pattern previously registered
+     * @throws IllegalArgumentException if no such pattern is registered
      */
-    public abstract void removeContext (String path) throws IllegalArgumentException ;
+    public void removeVirtualHost(final String pattern) throws IllegalArgumentException {
+        throw new IllegalArgumentException("No virtual host with the pattern " + pattern + " exists in this server");
+    }
 
     /**
-     * Removes the given context from the server.
-     * Removing a context does not affect exchanges currently being processed
-     * but prevents new ones from being accepted.
-     * @param context the context to remove
-     * @throws NullPointerException if context is <code>null</code>
+     * Remove the given virtual host from the server.  Removing a virtual host
+     * does not affect exchanges currently being
+     * processed but prevents new ones from being accepted.
+     *
+     * @param host the host to remove
+     * @throws IllegalArgumentException if no such host is registered
      */
-    public abstract void removeContext (HttpContext context) ;
+    public void removeVirtualHost(final HttpHost host) throws IllegalArgumentException {
+        throw new IllegalArgumentException("No virtual host " + host + " exists in this server");
+    }
 
     /**
      * returns the address this server is listening on

Reply via email to