public void startServer() throws Exception { executeUpgrader(); Authenticator auth = new MyAuthenticator(config); server = HttpServerProvider.provider().createHttpServer( new InetSocketAddress(config.getManagerDetails().getManagerPort()), 50); AjaxState ajaxState = new AjaxStateImpl(); createContext(server, "/", new WebRootHandler()); //$NON-NLS-1$ createContext(server, "/pages/", new PagesHandler(config), auth); //$NON-NLS-1$ createContext(server, "/server/", new ServerHandler(config), auth); //$NON-NLS-1$ createContext(server, "/deploy/", new DeployHandler(config, ajaxState), auth); //$NON-NLS-1$ createContext(server, "/ajax/", new AjaxProgressHandler(ajaxState)); //$NON-NLS-1$ createContext(server, "/upload/", new UploadHandler(config)); //$NON-NLS-1$ server.start(); }
private void createContext(HttpServer server, String path, HttpHandler handler, Authenticator auth) { final HttpContext context = server.createContext(path, handler); if( auth != null ) { context.setAuthenticator(auth); } context.getFilters().addAll(filters); }
@Override public Authenticator setAuthenticator(Authenticator auth) { Authenticator previous = _authenticator; _authenticator = auth; return previous; }
private Authenticator getAuthenticator() { Authenticator authenticator = new BasicAuthenticator(REALM) { public boolean checkCredentials(String username, String password) { return USERNAME.equals(username) && PASSWORD.equals(password); } }; return authenticator; }
protected Collection<HttpContext> createContexts() { final Filter[] filtersArray = createFilters(); final Collection<Filter> filtersList; if (filtersArray != null && filtersArray.length != 0) { filtersList = Arrays.asList(filtersArray); } else { filtersList = Collections.emptyList(); } final Collection<HttpContext> httpContexts = new ArrayList<HttpContext>(); final Authenticator authenticator = httpServerConfiguration.getAuthenticator(); final HttpPathHandler[] handlersArray = createHandlers(); if (handlersArray != null && handlersArray.length > 0) { for (final HttpPathHandler handler : handlersArray) { final HttpContext httpContext = server.createContext(handler.getPath(), handler); if (filtersArray != null && filtersArray.length != 0) { httpContext.getFilters().addAll(filtersList); } if (authenticator != null) { httpContext.setAuthenticator(authenticator); } httpContexts.add(httpContext); } } else { logger.log(Level.WARNING, JFaceMessages.get("msg.httpserver.configuration.handlers.none")); } return httpContexts; }
@Test public void shouldSetFiltersAndAuthenticatorsOnHttpContext() throws Exception { // setup Map<String, HttpHandler> contexts = new HashMap<String, HttpHandler>(); HttpHandler httpHandler = mock(HttpHandler.class); contexts.put("test1", httpHandler); Filter filter = mock(Filter.class); List<Filter> filters = new ArrayList<Filter>(); filters.add(filter); Authenticator authenticator = mock(Authenticator.class); simpleHttpServerFactoryBean.setContexts(contexts); simpleHttpServerFactoryBean.setFilters(filters); simpleHttpServerFactoryBean.setAuthenticator(authenticator); List<Filter> httpContextFilters = new ArrayList<Filter>(); HttpContext httpContext = mock(HttpContext.class); when(httpContext.getFilters()).thenReturn(httpContextFilters); when(httpServer.createContext("test1", httpHandler)).thenReturn(httpContext); // act simpleHttpServerFactoryBean.afterPropertiesSet(); // assert verify(httpContext).setAuthenticator(authenticator); assertThat(httpContextFilters.contains(filter), is(true)); }
@Override public void handle(String target, HttpServletRequest req, HttpServletResponse resp, int dispatch) throws IOException, ServletException { if (!target.startsWith(getContextPath())) return; JettyHttpExchange jettyHttpExchange = new JettyHttpExchange(_context, req, resp); // TODO: add filters processing Authenticator auth = _context.getAuthenticator(); try { if (auth != null) handleAuthentication(resp, jettyHttpExchange, auth); else _handler.handle(jettyHttpExchange); } catch(Exception ex) { PrintWriter writer = new PrintWriter(jettyHttpExchange.getResponseBody()); resp.setStatus(500); writer.println("<h2>HTTP ERROR: 500</h2>"); writer.println("<pre>INTERNAL_SERVER_ERROR</pre>"); writer.println("<p>RequestURI=" + req.getRequestURI() + "</p>"); writer.println("<pre>"); ex.printStackTrace(writer); writer.println("</pre>"); writer.println("<p><i><small><a href=\"http://jetty.mortbay.org\">Powered by jetty://</a></small></i></p>"); writer.close(); } finally { Request base_request = (req instanceof Request) ? (Request)req:HttpConnection.getCurrentConnection().getRequest(); base_request.setHandled(true); } }
@Override public Authenticator getAuthenticator() { return _authenticator; }
public ODataJerseyServer setHttpServerAuthenticator(Authenticator authenticator) { httpServerAuthenticator = authenticator; return this; }
@Nullable Authenticator getAuthenticator();
@Override public Authenticator getAuthenticator() { return null; // no authentication }
@BeforeClass public static void init() throws InterruptedException, IOException { LoggingSupport.setRootLevel(Level.FINE); certificate = File.createTempFile("cert-", ".jks"); logger.info(certificate.toString()); FileOutputStream fos = null; try { fos = new FileOutputStream(certificate); fos.write(DatatypeConverter.parseBase64Binary(JKS)); } finally { IOUtils.closeQuietly(fos); } final IHttpServerConfig configuration = new HttpServerDefaultConfig() { @Override public Authenticator getAuthenticator() { return authenticator; } @Override public BaseHttpHandler[] getHandlers() { final BaseHttpHandler h1 = new BaseHttpHandler(this) { @Override protected void doGet(final HttpExchange exchange) throws IOException, HttpException { sendResponse(exchange, loremSmallTxt.getBytes()); } }; h1.setPath(HANDLER_PATH_TXT); final BaseHttpHandler h2 = new DisabledHandler(this); h2.setEnabled(false); final BaseHttpHandler h3 = new RequestParameterHandler(this); final FilesHandler h4 = new FilesHandler(this, certificate.getParentFile().getPath(), "/files/"); h4.setAttachment(true); final ResourcesHandler h5 = new ResourcesHandler(this, getClass().getPackage(), "/resources/"); h5.setAttachment(true); h5.setCacheControl("max-age=86400"); final ResourcesHandler h6 = new ResourcesHandler(this, (Package) null, "/root/"); final FilesHandler h7 = new FilesHandler(this, certificate.getParentFile().getPath() + "/backtracking", "/backtracking/"); return new BaseHttpHandler[] { h1, h2, h3, h4, h5, h6, h7 }; } @Override public char[] getKeyPass() { return "keypass".toCharArray(); } @Override public String getKeyStoreFileName() { return certificate.getPath(); } @Override public int getPort() { return port; } @Override public char[] getStorePass() { return "storepass".toCharArray(); } @Override public boolean isSslEnabled() { return sslEnabled; } @Override public boolean isTraceMethodEnabled() { return true; } }; server = new LightweightHttpServer(configuration); }
/** * Register a common {@link com.sun.net.httpserver.Authenticator} to be * applied to all locally registered {@link #setContexts contexts}. */ public void setAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; }
/** * Register a common {@link com.sun.net.httpserver.Authenticator} to be * applied to all detected {@link javax.jws.WebService} annotated beans. */ public void setAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; }
/** * Register a common {@link com.sun.net.httpserver.Authenticator} to be applied to all locally registered * {@link #setContexts contexts}. */ public void setAuthenticator(Authenticator anAuthenticator) { this.authenticator = anAuthenticator; }