/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { HttpServer server = HttpServerFactory.create("http://localhost:8080/"); HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class); server.removeContext("/"); HttpContext cc = server.createContext("/", handler); cc.setAuthenticator( new BasicAuthenticator("mms") { @Override public boolean checkCredentials(String user, String pass) { User tmp = new sql().getUser(user, pass); if (tmp == null) return false; else return true; } }); server.start(); JOptionPane.showMessageDialog(null, "Server beenden", "Close", JOptionPane.ERROR_MESSAGE); server.stop(0); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void handle(HttpExchange ex) throws IOException { if (ex == null) { throw new NullPointerException(); } HttpContext context = ex.getHttpContext(); new Filter.Chain(context.getFilters(), context.getHandler()).doFilter(ex); }
@Override public synchronized HttpContext createContext(String path) { HttpContext context = instantiateContext(path); for (HttpContext trailContext : contexts) { if (path.equals(trailContext.getPath())) { throw new IllegalArgumentException("Handler already exists for path"); } } contexts.add(context); return context; }
private void addCtx(String path, HttpHandler h) { HttpContext ctx = server.createContext(path, h); if (configuration.isWebAuthenticate()) { ctx.setAuthenticator( new BasicAuthenticator("") { @Override public boolean checkCredentials(String user, String pwd) { LOGGER.debug("authenticate " + user); return pwd.equals(users.get(user)); // return true; } }); } }
public synchronized MockHttpExchange createExchange(String method, String path) { HttpContext best = null; int bestLength = -1; for (HttpContext context : contexts) { if (path.startsWith(context.getPath()) && context.getPath().length() > bestLength) { best = context; bestLength = context.getPath().length(); } } if (best == null) { return null; } return new MockHttpExchange(method, path, best); }
/* * Removes a context. If the server doesn't have anymore contexts, it * would stop the server and server is removed from servers Map. */ /*package*/ void removeContext(HttpContext context) { InetSocketAddress inetAddress = context.getServer().getAddress(); synchronized (servers) { ServerState state = servers.get(inetAddress); int instances = state.noOfContexts(); if (instances < 2) { ((ExecutorService) state.getServer().getExecutor()).shutdown(); state.getServer().stop(0); servers.remove(inetAddress); } else { state.getServer().removeContext(context); state.oneLessContext(context.getPath()); } } }
/* */ public synchronized void removeContext(HttpContext paramHttpContext) throws IllegalArgumentException { /* 239 */ if (!(paramHttpContext instanceof HttpContextImpl)) { /* 240 */ throw new IllegalArgumentException("wrong HttpContext type"); /* */ } /* 242 */ this.contexts.remove((HttpContextImpl)paramHttpContext); /* 243 */ this.logger.config("context removed: " + paramHttpContext.getPath()); /* */ }
/** * Initialize this JolokiaServer with the given HttpServer. The calle is responsible for managing * (starting/stopping) the HttpServer. * * @param pServer server to use * @param pConfig configuration * @param pLazy whether the initialization should be done lazy or not */ protected final void init(HttpServer pServer, JolokiaServerConfig pConfig, boolean pLazy) { config = pConfig; lazy = pLazy; // Create proper context along with handler final String contextPath = pConfig.getContextPath(); jolokiaHttpHandler = new JolokiaHttpHandler(pConfig.getJolokiaConfig()); HttpContext context = pServer.createContext(contextPath, jolokiaHttpHandler); // Add authentication if configured final Authenticator authenticator = pConfig.getAuthenticator(); if (authenticator != null) { context.setAuthenticator(authenticator); } url = detectAgentUrl(pServer, pConfig, contextPath); }
@Override public synchronized void removeContext(String path) { if (path == null) { throw new NullPointerException(); } Iterator<HttpContext> iter = contexts.iterator(); while (iter.hasNext()) { HttpContext context = iter.next(); if (context.getPath().equals(path)) { iter.remove(); // Completed. return; } } // Not found. throw new IllegalArgumentException(); }
private static HttpServer createHttpServer(ExecutorService execs) throws Exception { InetSocketAddress inetAddress = new InetSocketAddress(0); HttpServer testServer = HttpServer.create(inetAddress, 5); testServer.setExecutor(execs); HttpContext context = testServer.createContext("/test"); context.setHandler( new HttpHandler() { public void handle(HttpExchange msg) { try { synchronized (lock) { ++s_received; if ((s_received % 1000) == 0) { System.out.println("Received=" + s_received); } } String method = msg.getRequestMethod(); if (method.equals("POST")) { InputStream is = msg.getRequestBody(); byte[] buf = readFully(is); is.close(); writePostReply(msg, buf); } else { System.out.println("****** METHOD not handled ***** " + method); System.out.println("Received=" + s_received); } synchronized (lock) { ++sent; if ((sent % 1000) == 0) { System.out.println("sent=" + sent); } } } catch (Exception e) { e.printStackTrace(); } finally { msg.close(); } } }); return testServer; }
@Override public void removeContext(HttpContext context) { removeContext(context.getPath()); }
@Override public synchronized HttpContext createContext(String path, HttpHandler handler) { HttpContext context = createContext(path); context.setHandler(handler); return context; }
public static void main(String[] args) throws Exception { final String filename = "log4j.properties"; PropertyConfigurator.configure(SeleniumGridExtras.class.getClassLoader().getResource(filename)); logger.info("Loaded Grid Logger from " + filename); RuntimeConfig.load(true); SelfHealingGrid.checkStatus(RuntimeConfig.getGridExtrasPort(), RuntimeConfig.getConfig()); HttpServer server = HttpServer.create(new InetSocketAddress(RuntimeConfig.getGridExtrasPort()), 0); List<ExecuteOSTask> tasks = new LinkedList<ExecuteOSTask>(); for (String module : RuntimeConfig.getConfig().getActivatedModules()) { tasks.add((ExecuteOSTask) Class.forName(module).newInstance()); } logger.debug(RuntimeConfig.getSeleniungGridExtrasHomePath()); logger.info("Initializing Task Modules"); for (final ExecuteOSTask task : tasks) { if (task.initialize()) { HttpContext context = server.createContext( task.getEndpoint(), new HttpExecutor() { @Override String execute(Map params) { logger.debug( "End-point " + task.getEndpoint() + " was called with HTTP params " + params.toString()); String result = new GsonBuilder().setPrettyPrinting().create().toJson(task.execute(params)); return result; } }); context.getFilters().add(new ParameterFilter()); } } logger.info("API documentation"); logger.info("/api - Located here"); HttpContext context = server.createContext( "/api", new HttpExecutor() { @Override String execute(Map params) { String apiDocs = ApiDocumentation.getApiDocumentation(); logger.debug(apiDocs); return apiDocs; } }); if (RuntimeConfig.getConfig().getAutoStartHub()) { logger.info("Grid Hub was set to Autostart"); ExecuteOSTask grid = new StartGrid(); logger.debug(grid.execute("hub").toString().toString()); } if (RuntimeConfig.getConfig().getAutoStartNode()) { logger.info("Grid NodeConfig was set to Autostart"); ExecuteOSTask grid = new StartGrid(); logger.debug(grid.execute("node").toString().toString()); } context.getFilters().add(new ParameterFilter()); server.setExecutor(null); server.start(); logger.info("Server has been started"); }