Java Code Examples for com.sun.net.httpserver.Authenticator

The following are top voted examples for showing how to use com.sun.net.httpserver.Authenticator. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: Equella   File: Main.java   View source code 7 votes vote down vote up
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();
}
 
Example 2
Project: Equella   File: Main.java   View source code 5 votes vote down vote up
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);
}
 
Example 3
Project: openbravo-pos   File: JettyHttpContext.java   View source code 5 votes vote down vote up
@Override
public Authenticator setAuthenticator(Authenticator auth)
{
	Authenticator previous = _authenticator;
	_authenticator = auth;
    return previous;
}
 
Example 4
Project: oreva   File: BasicAuthenticationTest.java   View source code 5 votes vote down vote up
private Authenticator getAuthenticator() {
  Authenticator authenticator = new BasicAuthenticator(REALM) {
    public boolean checkCredentials(String username, String password) {
      return USERNAME.equals(username) && PASSWORD.equals(password);
    }
  };
  return authenticator;
}
 
Example 5
Project: JFaceUtils   File: LightweightHttpServer.java   View source code 5 votes vote down vote up
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;
}
 
Example 6
Project: OpenbravoPOS   File: JettyHttpContext.java   View source code 5 votes vote down vote up
@Override
public Authenticator setAuthenticator(Authenticator auth)
{
	Authenticator previous = _authenticator;
	_authenticator = auth;
    return previous;
}
 
Example 7
Project: pi   File: SimpleHttpServerFactoryBeanTest.java   View source code 5 votes vote down vote up
@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));
}
 
Example 8
Project: openbravo-pos   File: J2SE6ContextHandler.java   View source code 4 votes vote down vote up
@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);
     }
     
 }
 
Example 9
Project: openbravo-pos   File: JettyHttpContext.java   View source code 4 votes vote down vote up
@Override
public Authenticator getAuthenticator()
{
    return _authenticator;
}
 
Example 10
Project: oreva   File: ODataJerseyServer.java   View source code 4 votes vote down vote up
public ODataJerseyServer setHttpServerAuthenticator(Authenticator authenticator) {
  httpServerAuthenticator = authenticator;
  return this;
}
 
Example 11
Project: JFaceUtils   File: IHttpServerConfig.java   View source code 4 votes vote down vote up
@Nullable
Authenticator getAuthenticator();
 
Example 12
Project: JFaceUtils   File: HttpServerDefaultConfig.java   View source code 4 votes vote down vote up
@Override
public Authenticator getAuthenticator() {
	return null; // no authentication
}
 
Example 13
Project: JFaceUtils   File: LightweightHttpServerTest.java   View source code 4 votes vote down vote up
@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);
}
 
Example 14
Project: OpenbravoPOS   File: J2SE6ContextHandler.java   View source code 4 votes vote down vote up
@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);
     }
     
 }
 
Example 15
Project: OpenbravoPOS   File: JettyHttpContext.java   View source code 4 votes vote down vote up
@Override
public Authenticator getAuthenticator()
{
    return _authenticator;
}
 
Example 16
Project: lams   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 17
Project: lams   File: SimpleHttpServerJaxWsServiceExporter.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 18
Project: spring4-understanding   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 19
Project: spring4-understanding   File: SimpleHttpServerJaxWsServiceExporter.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 20
Project: my-spring-cache-redis   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 21
Project: spring   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 22
Project: class-guard   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 23
Project: class-guard   File: SimpleHttpServerJaxWsServiceExporter.java   View source code 2 votes vote down vote up
/**
 * 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;
}
 
Example 24
Project: pi   File: SimpleHttpServerFactoryBean.java   View source code 2 votes vote down vote up
/**
 * 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;
}