use of org.eclipse.jetty.server.nio.SelectChannelConnector in project http-request by kevinsawicki.
the class ServerTestCase method setUp.
/**
* Set up server with handler
*
* @param handler
* @return port
* @throws Exception
*/
public static String setUp(final Handler handler) throws Exception {
server = new Server();
if (handler != null)
server.setHandler(handler);
Connector connector = new SelectChannelConnector();
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
server.start();
proxy = new Server();
Connector proxyConnector = new SelectChannelConnector();
proxyConnector.setPort(0);
proxy.setConnectors(new Connector[] { proxyConnector });
ServletHandler proxyHandler = new ServletHandler();
RequestHandler proxyCountingHandler = new RequestHandler() {
@Override
public void handle(Request request, HttpServletResponse response) {
proxyHitCount.incrementAndGet();
String auth = request.getHeader("Proxy-Authorization");
auth = auth.substring(auth.indexOf(' ') + 1);
try {
auth = B64Code.decode(auth, CHARSET_UTF8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
int colon = auth.indexOf(':');
proxyUser.set(auth.substring(0, colon));
proxyPassword.set(auth.substring(colon + 1));
request.setHandled(false);
}
};
HandlerList handlerList = new HandlerList();
handlerList.addHandler(proxyCountingHandler);
handlerList.addHandler(proxyHandler);
proxy.setHandler(handlerList);
ServletHolder proxyHolder = proxyHandler.addServletWithMapping("org.eclipse.jetty.servlets.ProxyServlet", "/");
proxyHolder.setAsyncSupported(true);
proxy.start();
proxyPort = proxyConnector.getLocalPort();
return "http://localhost:" + connector.getLocalPort();
}
use of org.eclipse.jetty.server.nio.SelectChannelConnector in project saga by timurstrekalov.
the class InstrumentingProxyServer method start.
public int start() {
final SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(0);
server.addConnector(connector);
final HandlerCollection handlers = new HandlerCollection();
// Setup proxy servlet
final ServletContextHandler context = new ServletContextHandler(handlers, "/");
context.setAttribute(InstrumentingProxyServlet.INSTRUMENTER, instrumenter);
context.addServlet(new ServletHolder(InstrumentingProxyServlet.class), "/*");
// Setup proxy handler to handle CONNECT methods
final ConnectHandler connectProxy = new ConnectHandler();
handlers.addHandler(connectProxy);
server.setHandler(handlers);
try {
server.start();
logger.info("Proxy server started on port {}", connector.getLocalPort());
} catch (final Exception e) {
throw new RuntimeException(e);
}
return connector.getLocalPort();
}
use of org.eclipse.jetty.server.nio.SelectChannelConnector in project saga by timurstrekalov.
the class FileServer method start.
public int start() {
final SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(0);
server.addConnector(connector);
final HandlerCollection handlers = new HandlerCollection();
final ResourceHandler resourceHandler = new ResourceHandler() {
@Override
protected void doResponseHeaders(final HttpServletResponse response, final Resource resource, final String mimeType) {
response.addDateHeader("Expires", 0L);
}
};
resourceHandler.setDirectoriesListed(true);
resourceHandler.setResourceBase(resourceBase);
handlers.addHandler(resourceHandler);
server.setHandler(handlers);
try {
server.start();
logger.info("File server started on port {}", connector.getLocalPort());
} catch (final Exception e) {
throw new RuntimeException(e);
}
return connector.getLocalPort();
}
use of org.eclipse.jetty.server.nio.SelectChannelConnector in project bnd by bndtools.
the class HttpConnectorTest method startJetty.
private static Server startJetty() throws Exception {
Server server = new Server();
// Create the login service
String REQUIRED_ROLE = "users";
HashLoginService loginSvc = new HashLoginService(REQUIRED_ROLE, USER_ROLE_FILE);
server.addBean(loginSvc);
// Start HTTP and HTTPS connectors
SelectChannelConnector httpConnector = new SelectChannelConnector();
httpConnector.setPort(0);
httpConnector.setHost(LOCALHOST);
server.addConnector(httpConnector);
SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
sslConnector.setPort(0);
sslConnector.setHost(LOCALHOST);
SslContextFactory contextFactory = sslConnector.getSslContextFactory();
contextFactory.setKeyStorePath(KEYSTORE_PATH);
contextFactory.setKeyStorePassword(KEYSTORE_PASS);
server.addConnector(sslConnector);
// Create the resource handler to serve files
ResourceHandler resourceHandler = new ETaggingResourceHandler();
resourceHandler.setResourceBase(RESOURCE_BASE);
resourceHandler.setDirectoriesListed(true);
// Setup user role constraints
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { REQUIRED_ROLE });
constraint.setAuthenticate(true);
// Map constraints to the secured directory
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec(SECURED_PATH);
// Setup the constraint handler
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setAuthMethod("BASIC");
securityHandler.setHandler(resourceHandler);
securityHandler.setLoginService(loginSvc);
securityHandler.setConstraintMappings(new ConstraintMapping[] { cm });
// Finally!! Start the server
server.setHandler(securityHandler);
server.start();
while (!server.isRunning()) {
Thread.sleep(10);
}
HTTP_PORT = httpConnector.getLocalPort();
HTTPS_PORT = sslConnector.getLocalPort();
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(HTTP_PORT), Integer.valueOf(HTTPS_PORT));
return server;
}
Aggregations