Server server = new Server(8080); WebSocketHandler wsHandler = new WebSocketHandler() { @Override public void configure(WebSocketServletFactory factory) { factory.register(MyEchoSocket.class); } }; ContextHandler context = new ContextHandler(); context.setContextPath("/echo"); context.setHandler(wsHandler); server.addHandler(context); server.start(); server.join();
@Test public void shouldAddRootRequestHandler() throws Exception { jetty9Server.configure(); jetty9Server.startHandlers(); ContextHandler rootRequestHandler = getLoadedHandlers().get(GoServerLoadingIndicationHandler.class); assertThat(rootRequestHandler.getContextPath(), is("/")); }
/** * Servlet spec 3.1 pg. 147. */ @Override public boolean checkPathsWithUncoveredHttpMethods() { Set<String> paths = getPathsWithUncoveredHttpMethods(); if (paths != null && !paths.isEmpty()) { for (String p:paths) LOG.warn("{} has uncovered http methods for path: {}",ContextHandler.getCurrentContext(), p); if (LOG.isDebugEnabled()) LOG.debug(new Throwable()); return true; } return false; }
@Signature public void addWebSocket(Environment env, String path, ArrayMemory _handlers) { WebSocketParam param = _handlers.toBean(env, WebSocketParam.class); ContextHandler contextHandler = new ContextHandler(path); contextHandler.setHandler(new WebSocketHandler() { @Override public void configure(WebSocketServletFactory factory) { factory.setCreator(new WebSocketCreator() { @Override public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) { return new WebSocket(env, param); } }); } }); handlers.addHandler(contextHandler); }
@Override public void start() { writeTimeToFile("startingAt"); ContextHandler context = new ContextHandler(); context.setContextPath("/"); context.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(context); context.setHandler(new AbstractHandler() { @Override public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { if ("/ping".equals(target)) { request.setHandled(true); httpServletResponse.getWriter().print("ping"); } else if ("/restart".equals(target)) { writeTimeToFile("restartAskedAt"); request.setHandled(true); processCommands.askForRestart(); httpServletResponse.getWriter().print("ok"); } else if ("/kill".equals(target)) { writeTimeToFile("killedAt"); System.exit(0); } } }); try { server.start(); } catch (Exception e) { throw new IllegalStateException("Fail to start Jetty", e); } }
public void start() throws Exception { server = new Server(port); ContextHandler context = new ContextHandler(); context.setContextPath("/"); context.setResourceBase("."); context.setClassLoader(Thread.currentThread().getContextClassLoader()); context.setHandler(new JettyObjectHandler(object)); server.setHandler(context); server.start(); }
Server server = new Server(port); ContextHandler contextHandler = new ContextHandler(); contextHandler.setContextPath("/hello"); contextHandler.setResourceBase("."); contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader()); contextHandler.setHandler(new HelloHandler("")); server.setHandler(contextHandlerCollection); server.start(); System.out.println("Server started at port " + port + " with context handler for /hello"); System.in.read(); ContextHandler contextHandler2 = new ContextHandler(); contextHandler2.setContextPath("/hello2"); contextHandler2.setResourceBase("."); contextHandler2.setClassLoader(Thread.currentThread().getContextClassLoader()); contextHandler2.setHandler(new HelloHandler("2")); contextHandler2.start(); response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>Hello World" + string + "</h1>");
Server server = new Server(8080); server.setSessionIdManager(idmanager); ContextHandler context = new ContextHandler("/"); server.setHandler(context); context.setHandler(sessions);
public static void main(String[] args) throws Exception { Server server = new Server(8080); // Add a single handler on context "/hello" ContextHandler context = new ContextHandler(); context.setContextPath("/hello"); context.setHandler(new HelloHandler()); // Can be accessed using http://localhost:8080/hello server.setHandler(context); // Start the server server.start(); server.join(); } }
server = new Server(port); resourceHandler.setDirectoriesListed(true); ContextHandler sendGcodeContext = new ContextHandler(); sendGcodeContext.setContextPath("/sendGcode"); sendGcodeContext.setBaseResource(getBaseResource()); sendGcodeContext.setClassLoader(Thread.currentThread().getContextClassLoader()); sendGcodeContext.setHandler(new SendGcodeHandler()); ContextHandler adjustManualLocationContext = new ContextHandler(); adjustManualLocationContext.setContextPath("/adjustManualLocation"); adjustManualLocationContext.setBaseResource(getBaseResource()); adjustManualLocationContext.setClassLoader(Thread.currentThread().getContextClassLoader()); adjustManualLocationContext.setHandler(new AdjustManualLocationHandler()); ContextHandler getSystemStateContext = new ContextHandler(); getSystemStateContext.setContextPath("/getSystemState"); getSystemStateContext.setBaseResource(getBaseResource()); getSystemStateContext.setClassLoader(Thread.currentThread().getContextClassLoader()); getSystemStateContext.setHandler(new GetSystemStateHandler()); ContextHandler configContext = new ContextHandler(); configContext.setContextPath("/config"); configContext.setBaseResource(getBaseResource()); configContext.setClassLoader(Thread.currentThread().getContextClassLoader()); configContext.setHandler(new ConfigHandler()); configContext.setInitParameter("cacheControl", "max-age=0, public");
"jetty.threads"); Server server = new Server(pool); server.setStopAtShutdown(false); https.setPort(conf.getInt("application.securePort")); server.addConnector(https); server.addConnector(http); ContextHandler sch = new ContextHandler(); sch.setAttribute(DecoratedObjectFactory.ATTR, new DecoratedObjectFactory()); sch.getServletContext(), wsConfig); webSocketServerFactory.setCreator((req, rsp) -> { JettyWebSocket ws = new JettyWebSocket(); sch.setContextPath("/"); sch.setHandler(new JettyHandler(handler, webSocketServerFactory, conf .getString("application.tmpdir"), conf.getBytes("jetty.FileSizeThreshold").intValue()));
public SentryWebServer(List<EventListener> listeners, int port, Configuration conf) { this.port = port; server = new Server(port); ServletContextHandler servletContextHandler = new ServletContextHandler(); ServletHolder servletHolder = new ServletHolder(AdminServlet.class); servletContextHandler.addServlet(confServletHolder, "/conf"); servletContextHandler.getServletContext() .setAttribute(ConfServlet.CONF_CONTEXT_ATTRIBUTE, conf); URL url = this.getClass().getResource(RESOURCE_DIR); try { resourceHandler.setBaseResource(Resource.newResource(url.toString())); } catch (IOException e) { LOGGER.error("Got exception while setBaseResource for Sentry Service web UI", e); ContextHandler contextHandler= new ContextHandler(); contextHandler.setHandler(resourceHandler); server.setHandler(contextHandlerCollection);
ResourceHandler staticResource = new ResourceHandler(); staticResource.setDirectoriesListed(true); staticResource.setWelcomeFiles(new String[] { "index.html" }); staticResource.setResourceBase("/path/to/your/files"); ContextHandler staticContextHandler = new ContextHandler(); staticContextHandler.setContextPath("/*"); staticContextHandler.setHandler(staticResource); Server server = new Server(8080); server.setHandler(staticContextHandler);
if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method)) baseRequest.setHandled(true); return; ServletContext servlet_context = request.getServletContext(); if (servlet_context==null) servlet_context=ContextHandler.getCurrentContext(); if (servlet_context==null) LOG.warn("No ServletContext for error page {}",error_page); LOG.warn("Error page loop {}",error_page); try if (LOG.isDebugEnabled()) LOG.debug("error page dispatch {}->{}",error_page,dispatcher); if(dispatcher!=null) generateAcceptableResponse(baseRequest,request,response,response.getStatus(),baseRequest.getResponse().getReason());
Resource rootResource = Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(root.toURI().toURL())); if (rootResource.exists()&& !rootResource.isDirectory() && !rootResource.toString().startsWith("jar:")) if (_contextHandler != null && _contextHandler.getBaseResource() == null) _contextHandler.setBaseResource(rootResource); LOG.debug("Context classloader = " + cl); try _contextHandler.setClassLoader(classLoader); contextPath = (String)_properties.get(OSGiWebappConstants.SERVICE_PROP_CONTEXT_PATH); if (contextPath != null) _contextHandler.setContextPath(contextPath); _contextHandler.setAttribute(OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext()); String[] targets = _contextHandler.getProtectedTargets(); int length = (targets==null?0:targets.length); updatedTargets = new String[OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length]; System.arraycopy(OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS, 0, updatedTargets, length, OSGiWebappConstants.DEFAULT_PROTECTED_OSGI_TARGETS.length); _contextHandler.setProtectedTargets(updatedTargets);
/** Add a context handler. * @param contextPath The context path to add * @return the ContextHandler just added */ public ContextHandler addContext(String contextPath,String resourceBase) { try { ContextHandler context = _contextClass.newInstance(); context.setContextPath(contextPath); context.setResourceBase(resourceBase); addHandler(context); return context; } catch (Exception e) { LOG.debug(e); throw new Error(e); } }
if (ctx != null) if(__log.isDebugEnabled()) __log.debug("Using the Context that is bound on the thread"); return ctx; if (__log.isDebugEnabled() && loader != null) __log.debug("Trying thread context classloader"); while (ctx == null && loader != null) if (ContextHandler.getCurrentContext() != null) if (__log.isDebugEnabled() && loader != null) __log.debug("Trying classloader of current org.eclipse.jetty.server.handler.ContextHandler"); loader = ContextHandler.getCurrentContext().getContextHandler().getClassLoader(); ctx = (Context)__contextMap.get(loader);
@Override public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException Request baseRequest=Request.getBaseRequest(request); response = new ServletResponseHttpWrapper(response); final DispatcherType old_type = baseRequest.getDispatcherType(); final Attributes old_attr=baseRequest.getAttributes(); final MultiMap<String> old_query_params=baseRequest.getQueryParameters(); try if (_named!=null) _contextHandler.handle(_named,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response); attr._contextPath=_contextHandler.getContextPath(); baseRequest.setAttributes(attr); _contextHandler.handle(_pathInContext, baseRequest, (HttpServletRequest)request, (HttpServletResponse)response);
throws Exception ContextHandler.Context context=ContextHandler.getCurrentContext(); _servletContext=context==null?new ContextHandler.StaticContext():context; _contextHandler=(ServletContextHandler)(context==null?null:context.getContextHandler()); if (LOG.isDebugEnabled()) LOG.debug("Adding Default404Servlet to {}",this); addServletWithMapping(Default404Servlet.class,"/"); updateMappings();
return null; ContextHandler context = getChildHandlerByClass(ContextHandler.class); if (context!=null && context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) host=context.getVirtualHosts()[0]; if (host==null) host=InetAddress.getLocalHost().getHostAddress(); String path=context==null?null:context.getContextPath(); if (path==null) path="/"; LOG.warn(e); return null;