use of org.mortbay.jetty.Handler in project maven-plugins by apache.
the class ProjectInfoReportUtilsTest method startJetty.
private void startJetty(boolean isSSL, boolean withAuth) throws Exception {
jettyServer = new Server();
jettyServer.setStopAtShutdown(true);
Connector connector = (isSSL ? getSSLConnector() : getDefaultConnector());
jettyServer.setConnectors(new Connector[] { connector });
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setResourceBase(getBasedir() + "/target/classes/");
webapp.setServer(jettyServer);
if (withAuth) {
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { "user", "admin" });
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
SecurityHandler sh = new SecurityHandler();
sh.setUserRealm(new HashUserRealm("MyRealm", getBasedir() + "/src/test/resources/realm.properties"));
sh.setConstraintMappings(new ConstraintMapping[] { cm });
webapp.addHandler(sh);
}
DefaultHandler defaultHandler = new DefaultHandler();
defaultHandler.setServer(jettyServer);
Handler[] handlers = new Handler[2];
handlers[0] = webapp;
handlers[1] = defaultHandler;
jettyServer.setHandlers(handlers);
jettyServer.start();
port = connector.getLocalPort();
}
use of org.mortbay.jetty.Handler in project tomee by apache.
the class JettyHttpServer method init.
@Override
public void init(final Properties props) throws Exception {
final Options options = new Options(props);
port = options.get("port", 8080);
// Create all the Jetty objects but dont' start them
server = new Server();
final Connector connector = new SelectChannelConnector();
connector.setPort(port);
server.setConnectors(new Connector[] { connector });
final ContextHandler context = new ContextHandler();
context.setContextPath("/");
final ServletContext servletContext = context.getServletContext();
server.setHandler(context);
final Handler handler = new AbstractHandler() {
@Override
public void handle(final String target, final HttpServletRequest req, final HttpServletResponse res, final int dispatch) throws IOException, ServletException {
try {
((Request) req).setHandled(true);
final HttpRequest httpRequest = new ServletRequestAdapter(req, res, servletContext);
final HttpResponse httpResponse = new ServletResponseAdapter(res);
JettyHttpServer.this.listener.onMessage(httpRequest, httpResponse);
} catch (IOException | ServletException e) {
throw e;
} catch (Exception e) {
throw new ServletException(e);
}
}
};
final SessionHandler sessionHandler = new SessionHandler();
final SessionManager sessionManager = new HashSessionManager();
sessionManager.setIdManager(new HashSessionIdManager());
sessionHandler.setSessionManager(sessionManager);
sessionHandler.setHandler(handler);
context.setHandler(sessionHandler);
}
use of org.mortbay.jetty.Handler in project gradle by gradle.
the class JettyRun method finishConfigurationBeforeStart.
public void finishConfigurationBeforeStart() throws Exception {
Handler[] handlers = getConfiguredContextHandlers();
org.gradle.api.plugins.jetty.internal.JettyPluginServer plugin = getServer();
Server server = (Server) plugin.getProxiedObject();
HandlerCollection contexts = (HandlerCollection) server.getChildHandlerByClass(ContextHandlerCollection.class);
if (contexts == null) {
contexts = (HandlerCollection) server.getChildHandlerByClass(HandlerCollection.class);
}
for (int i = 0; (handlers != null) && (i < handlers.length); i++) {
contexts.addHandler(handlers[i]);
}
}
use of org.mortbay.jetty.Handler in project maven-plugins by apache.
the class SiteRunMojo method execute.
/**
* @see org.apache.maven.plugin.AbstractMojo#execute()
*/
public void execute() throws MojoExecutionException, MojoFailureException {
checkInputEncoding();
Server server = new Server();
server.setStopAtShutdown(true);
Connector defaultConnector = getDefaultConnector();
server.setConnectors(new Connector[] { defaultConnector });
WebAppContext webapp = createWebApplication();
webapp.setServer(server);
DefaultHandler defaultHandler = new DefaultHandler();
defaultHandler.setServer(server);
Handler[] handlers = new Handler[2];
handlers[0] = webapp;
handlers[1] = defaultHandler;
server.setHandlers(handlers);
getLog().info("Starting Jetty on http://localhost:" + port + "/");
try {
server.start();
} catch (Exception e) {
throw new MojoExecutionException("Error executing Jetty: " + e.getMessage(), e);
}
// Watch it
try {
server.getThreadPool().join();
} catch (InterruptedException e) {
getLog().warn("Jetty was interrupted", e);
}
}
Aggregations