/** * Removes an array of virtual host entries, if this removes all entries the _vhosts will be set to null * * @param virtualHosts * Array of virtual hosts that this context responds to. A null/empty array means any hostname is acceptable. Host names may be String * representation of IP addresses. Host names may start with '*.' to wildcard one level of names. Hosts and wildcard hosts may be followed with * '@connectorname', in which case they will match only if the the {@link Connector#getName()} for the request also matches. If an entry is just * '@connectorname' it will match any host if that connector was used. Note - In previous versions if one or more connectorname only entries existed * and non of the connectors matched the handler would not match regardless of any hostname entries. If there is one or more connectorname only * entries and one or more host only entries but no hostname and connector entries we assume the old behavior and will log a warning. The warning * can be removed by removing the host entries that were previously being ignored, or modifying to include a hostname and connectorname entry. */ public void removeVirtualHosts(String[] virtualHosts) { if (virtualHosts == null || virtualHosts.length==0 || _vhosts == null || _vhosts.length == 0) return; // do nothing Set<String> existingVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts())); for (String vh : virtualHosts) existingVirtualHosts.remove(normalizeHostname(vh)); if (existingVirtualHosts.isEmpty()) setVirtualHosts(null); // if we ended up removing them all, just null out _vhosts else setVirtualHosts(existingVirtualHosts.toArray(new String[0])); }
private ContextHandler createContext(String contextPath, final String host) { ServletContextHandler context = new ServletContextHandler(); context.setContextPath(contextPath); @SuppressWarnings("serial") ServletHolder helloholder = new ServletHolder(new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.getWriter().printf("Hello from [%s] context%n", host); } }); context.addServlet(helloholder, "/hello"); context.addServlet(DefaultServlet.class, "/"); ContextHandler vhwrapper = new ContextHandler(); vhwrapper.setHandler(context); vhwrapper.setVirtualHosts(new String[]{host}); return vhwrapper; }
/** * Either set virtual hosts or add to an existing set of virtual hosts. * * @param virtualHosts * Array of virtual hosts that this context responds to. A null/empty array means any hostname is acceptable. Host names may be String * representation of IP addresses. Host names may start with '*.' to wildcard one level of names. Hosts and wildcard hosts may be followed with * '@connectorname', in which case they will match only if the the {@link Connector#getName()} for the request also matches. If an entry is just * '@connectorname' it will match any host if that connector was used. Note - In previous versions if one or more connectorname only entries existed * and non of the connectors matched the handler would not match regardless of any hostname entries. If there is one or more connectorname only * entries and one or more host only entries but no hostname and connector entries we assume the old behavior and will log a warning. The warning * can be removed by removing the host entries that were previously being ignored, or modifying to include a hostname and connectorname entry. */ public void addVirtualHosts(String[] virtualHosts) { if (virtualHosts == null || virtualHosts.length==0) // since this is add, we don't null the old ones return; if (_vhosts==null) { setVirtualHosts(virtualHosts); } else { Set<String> currentVirtualHosts = new HashSet<String>(Arrays.asList(getVirtualHosts())); for (String vh : virtualHosts) currentVirtualHosts.add(normalizeHostname(vh)); setVirtualHosts(currentVirtualHosts.toArray(new String[0])); } }
ServerConnector httpConnector = new ServerConnector(server); httpConnector.setName("unsecured"); // named connector httpConnector.setPort(80); ContextHandler helloHandler = new ContextHandler(); helloHandler.setContextPath("/hello"); helloHandler.setHandler(new HelloHandler("Hello World")); helloHandler.setVirtualHosts(new String[]{"@unsecured"});
setVirtualHosts(filteredHosts);
ContextHandler fooContextHandler = new ContextHandler("/"); fooContextHandler.setVirtualHosts(new String[]{"foo"}); fooContextHandler.setHandler(new FooBarHandler()); ContextHandler bazContextHandler = new ContextHandler("/"); bazContextHandler.setVirtualHosts(new String[]{"baz"}); bazContextHandler.setHandler(new BazBarHandler()); HandlerCollection handler = new HandlerCollection(); handler.addHandler(fooContextHandler); handler.addHandler(bazContextHandler); server.setHandler(handler);
ctxHelloA.setContextPath("/"); ctxHelloA.setHandler(new io.github.biezhi.jetty.handlers.HelloHandler("Hello Connection A")); ctxHelloA.setVirtualHosts(new String[]{"@connA"}); contexts.addHandler(ctxHelloA); ctxHelloB.setContextPath("/"); ctxHelloB.setHandler(new HelloHandler("Greetings from Connection B")); ctxHelloB.setVirtualHosts(new String[]{"@connB"}); contexts.addHandler(ctxHelloB);
public ContextHandler getHandler() { ContextHandler contextHandler = new ContextHandler(); contextHandler.setContextPath(contextPath); contextHandler.setHandler(wrappedHandler.getHandler()); contextHandler.setVirtualHosts(virtualHosts); return contextHandler; }
public class ManyContexts { public static void main( String[] args ) throws Exception { Server server = new Server(8080); ContextHandler context = new ContextHandler("/"); context.setContextPath("/"); context.setHandler(new HelloHandler("Root Hello")); ContextHandler contextFR = new ContextHandler("/fr"); contextFR.setHandler(new HelloHandler("Bonjoir")); ContextHandler contextIT = new ContextHandler("/it"); contextIT.setHandler(new HelloHandler("Bongiorno")); ContextHandler contextV = new ContextHandler("/"); contextV.setVirtualHosts(new String[] { "127.0.0.2" }); contextV.setHandler(new HelloHandler("Virtual Hello")); ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV }); server.setHandler(contexts); server.start(); server.join(); } }
public static void main(String[] args) throws Exception { Server server = new Server(8080); ContextHandler context = new ContextHandler("/"); context.setContextPath("/"); context.setHandler(new HelloHandler("Root Hello")); ContextHandler contextFR = new ContextHandler("/fr"); contextFR.setHandler(new HelloHandler("Bonjoir")); ContextHandler contextIT = new ContextHandler("/it"); contextIT.setHandler(new HelloHandler("Bongiorno")); ContextHandler contextV = new ContextHandler("/"); contextV.setVirtualHosts(new String[]{"127.0.0.2"}); contextV.setHandler(new HelloHandler("Virtual Hello")); ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[]{context, contextFR, contextIT, contextV}); server.setHandler(contexts); server.start(); server.join(); } }
topologyContextHandler.setVirtualHosts( new String[] { "@" + entry.getKey().toLowerCase(Locale.ROOT) });
context1.setResourceBase(config.resource()); context1.setClassLoader(Thread.currentThread().getContextClassLoader()); context1.setVirtualHosts(config.vhosts());
topologyContextHandler.setVirtualHosts( new String[] { "@" + entry.getKey().toLowerCase(Locale.ROOT) });
context1.setResourceBase(config.resource()); context1.setClassLoader(Thread.currentThread().getContextClassLoader()); context1.setVirtualHosts(config.vhosts());