public void run() { Properties jettyProperties = new Properties(); String pFileName = PropertiesUtil.getJettyPropertiesFile(); try { jettyProperties.load(new FileInputStream(pFileName)); } catch (IOException e) { log.error("load properties from {} error.", pFileName); return; } // crate server server = new Server(Integer.parseInt(jettyProperties.getProperty(PropList.HTTP_PORT))); // Create the ResourceHandler. It is the object that will actually handle the request for a // given file. It is // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in // other examples. ResourceHandler resource_handler = new ResourceHandler(); // Configure the ResourceHandler. Setting the resource base indicates where the files should be // served out of. // In this example it is the current directory but it can be configured to anything that the jvm // has access to. resource_handler.setDirectoriesListed(true); resource_handler.setWelcomeFiles(new String[] {"index.html"}); resource_handler.setResourceBase(PropertiesUtil.getWebBaseDir()); log.debug(PropertiesUtil.getWebBaseDir()); // Add a single handler on context "/hello" ContextHandler context = new ContextHandler(); context.setContextPath("/LoadT"); context.setHandler(new LoadTHandler()); // Add the ResourceHandler to the server. GzipHandler gzip = new GzipHandler(); server.setHandler(gzip); // make handler chain HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] {resource_handler, context, new DefaultHandler()}); gzip.setHandler(handlers); try { server.start(); } catch (Exception e) { log.error("embedded jetty server start error.", e); } server.dumpStdErr(); /* try { server.join(); } catch (InterruptedException e) { log.info("jetty server interrupted", e); }*/ }
private void addResourceHandler(String contextPath, String filePath) throws Exception { if (handlerCollection != null) { logger.log(Level.INFO, "Adding resource : " + contextPath + "=>" + filePath); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setResourceBase(filePath); logger.log(Level.INFO, "serving: " + resourceHandler.getBaseResource()); ContextHandler contextHandler = new ContextHandler(contextPath); contextHandler.setHandler(resourceHandler); handlerCollection.addHandler(contextHandler); try { resourceHandler.start(); contextHandler.start(); } catch (Exception e) { logger.log(Level.INFO, "Could not start resource context", e); } } }
@BeforeClass public static void startServer() throws Exception { server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(0); server.addConnector(connector); ContextHandlerCollection contexts = new ContextHandlerCollection(); File dir = MavenTestingUtils.getTargetTestingDir(ResourceHandlerRangeTest.class.getSimpleName()); FS.ensureEmpty(dir); File rangeFile = new File(dir, "range.txt"); try (FileWriter writer = new FileWriter(rangeFile)) { writer.append("0123456789"); writer.flush(); } ContextHandler contextHandler = new ContextHandler(); ResourceHandler contentResourceHandler = new ResourceHandler(); contextHandler.setBaseResource(Resource.newResource(dir.getAbsolutePath())); contextHandler.setHandler(contentResourceHandler); contextHandler.setContextPath("/"); contexts.addHandler(contextHandler); server.setHandler(contexts); server.start(); String host = connector.getHost(); if (host == null) { host = "localhost"; } int port = connector.getLocalPort(); serverUri = new URI(String.format("http://%s:%d/", host, port)); }
@Test public void handlesSecureWebSocketConnection() throws Exception { final String rawHttpRequest = "GET /streamhubws/ HTTP/1.1\r\n" + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n" + "Host: localhost:7979\r\n" + "Origin: http://localhost:7979\r\n" + "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\r\n" + "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\r\n\r\n" + "Tm[K T2u"; final String expectedResponse = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n" + "Server: StreamHub\r\n" + "Sec-WebSocket-Origin: http://localhost:7979\r\n" + "Sec-WebSocket-Location: ws://localhost:7979/streamhubws/\r\n" + "Sec-WebSocket-Protocol: StreamHubWS\r\n\r\n" + "fQJ,fN/4F4!~K~MH"; context.checking( new Expectations() { { allowing(connection).getRequest(); will(returnValue(request)); one(request).getContext(); will(returnValue("/streamhubws")); one(request).getUrl(); will(returnValue("/streamhubws/")); allowing(connection).readBytes(); will(returnValue(rawHttpRequest.getBytes())); one(connection).setReadableEventInterceptor(with(any(Connection.class))); one(connection).write(ByteBuffer.wrap(expectedResponse.getBytes())); one(request).isKeepAliveConnection(); will(returnValue(true)); } }); cometHandler.handle(connection); }