use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class XmlConfiguredJetty method getWebAppContexts.
public List<WebAppContext> getWebAppContexts() {
List<WebAppContext> contexts = new ArrayList<>();
HandlerCollection handlers = (HandlerCollection) _server.getHandler();
Handler[] children = handlers.getChildHandlers();
for (Handler handler : children) {
if (handler instanceof WebAppContext) {
WebAppContext context = (WebAppContext) handler;
contexts.add(context);
}
}
return contexts;
}
use of org.eclipse.jetty.server.Handler in project buck by facebook.
the class WebServer method createHandlers.
@VisibleForTesting
ImmutableList<ContextHandler> createHandlers() {
Map<String, Handler> contextPathToHandler = Maps.newHashMap();
contextPathToHandler.put(INDEX_CONTEXT_PATH, new TemplateHandler(new IndexHandlerDelegate()));
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(staticContentDirectory);
contextPathToHandler.put(STATIC_CONTEXT_PATH, resourceHandler);
// Handlers for traces.
BuildTraces buildTraces = new BuildTraces(projectFilesystem);
contextPathToHandler.put(TRACE_CONTEXT_PATH, new TemplateHandler(new TraceHandlerDelegate(buildTraces)));
contextPathToHandler.put(TRACES_CONTEXT_PATH, new TemplateHandler(new TracesHandlerDelegate(buildTraces)));
contextPathToHandler.put(TRACE_DATA_CONTEXT_PATH, new TraceDataHandler(buildTraces));
contextPathToHandler.put(ARTIFACTS_CONTEXT_PATH, artifactCacheHandler);
ImmutableList.Builder<ContextHandler> handlers = ImmutableList.builder();
for (Map.Entry<String, Handler> entry : contextPathToHandler.entrySet()) {
String contextPath = entry.getKey();
Handler handler = entry.getValue();
ContextHandler contextHandler = new ContextHandler(contextPath);
contextHandler.setHandler(handler);
handlers.add(contextHandler);
}
// Create a handler that acts as a WebSocket server.
ServletContextHandler servletContextHandler = new ServletContextHandler(/* parent */
server, /* contextPath */
"/ws", /* sessions */
true, /* security */
false);
servletContextHandler.addServlet(new ServletHolder(streamingWebSocketServlet), "/build");
handlers.add(servletContextHandler);
return handlers.build();
}
use of org.eclipse.jetty.server.Handler in project buck by facebook.
the class WebServer method updateAndStartIfNeeded.
/**
* Update state and start the server if necessary.
*
* @param artifactCache cache to serve.
* @throws WebServerException
*/
public synchronized void updateAndStartIfNeeded(Optional<ArtifactCache> artifactCache) throws WebServerException {
artifactCacheHandler.setArtifactCache(artifactCache);
if (server.isStarted()) {
return;
}
// Package up all of the handlers into a ContextHandlerCollection to serve as the handler for
// the server.
ImmutableList<? extends Handler> handlers = createHandlers();
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(handlers.toArray(new Handler[0]));
server.setHandler(contexts);
try {
server.start();
} catch (Exception e) {
throw new WebServerException("Cannot start Websocket server.", e);
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class SecurityHandler method handle.
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.Handler#handle(java.lang.String,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void handle(String pathInContext, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final Response base_response = baseRequest.getResponse();
final Handler handler = getHandler();
if (handler == null)
return;
final Authenticator authenticator = _authenticator;
if (checkSecurity(baseRequest)) {
//See Servlet Spec 3.1 sec 13.6.3
if (authenticator != null)
authenticator.prepareRequest(baseRequest);
RoleInfo roleInfo = prepareConstraintInfo(pathInContext, baseRequest);
// Check data constraints
if (!checkUserDataPermissions(pathInContext, baseRequest, base_response, roleInfo)) {
if (!baseRequest.isHandled()) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
baseRequest.setHandled(true);
}
return;
}
// is Auth mandatory?
boolean isAuthMandatory = isAuthMandatory(baseRequest, base_response, roleInfo);
if (isAuthMandatory && authenticator == null) {
LOG.warn("No authenticator for: " + roleInfo);
if (!baseRequest.isHandled()) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
baseRequest.setHandled(true);
}
return;
}
// check authentication
Object previousIdentity = null;
try {
Authentication authentication = baseRequest.getAuthentication();
if (authentication == null || authentication == Authentication.NOT_CHECKED)
authentication = authenticator == null ? Authentication.UNAUTHENTICATED : authenticator.validateRequest(request, response, isAuthMandatory);
if (authentication instanceof Authentication.Wrapped) {
request = ((Authentication.Wrapped) authentication).getHttpServletRequest();
response = ((Authentication.Wrapped) authentication).getHttpServletResponse();
}
if (authentication instanceof Authentication.ResponseSent) {
baseRequest.setHandled(true);
} else if (authentication instanceof Authentication.User) {
Authentication.User userAuth = (Authentication.User) authentication;
baseRequest.setAuthentication(authentication);
if (_identityService != null)
previousIdentity = _identityService.associate(userAuth.getUserIdentity());
if (isAuthMandatory) {
boolean authorized = checkWebResourcePermissions(pathInContext, baseRequest, base_response, roleInfo, userAuth.getUserIdentity());
if (!authorized) {
response.sendError(HttpServletResponse.SC_FORBIDDEN, "!role");
baseRequest.setHandled(true);
return;
}
}
handler.handle(pathInContext, baseRequest, request, response);
if (authenticator != null)
authenticator.secureResponse(request, response, isAuthMandatory, userAuth);
} else if (authentication instanceof Authentication.Deferred) {
DeferredAuthentication deferred = (DeferredAuthentication) authentication;
baseRequest.setAuthentication(authentication);
try {
handler.handle(pathInContext, baseRequest, request, response);
} finally {
previousIdentity = deferred.getPreviousAssociation();
}
if (authenticator != null) {
Authentication auth = baseRequest.getAuthentication();
if (auth instanceof Authentication.User) {
Authentication.User userAuth = (Authentication.User) auth;
authenticator.secureResponse(request, response, isAuthMandatory, userAuth);
} else
authenticator.secureResponse(request, response, isAuthMandatory, null);
}
} else {
baseRequest.setAuthentication(authentication);
if (_identityService != null)
previousIdentity = _identityService.associate(null);
handler.handle(pathInContext, baseRequest, request, response);
if (authenticator != null)
authenticator.secureResponse(request, response, isAuthMandatory, null);
}
} catch (ServerAuthException e) {
// jaspi 3.8.3 send HTTP 500 internal server error, with message
// from AuthException
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} finally {
if (_identityService != null)
_identityService.disassociate(previousIdentity);
}
} else
handler.handle(pathInContext, baseRequest, request, response);
}
use of org.eclipse.jetty.server.Handler in project camel by apache.
the class WebsocketComponent method createContext.
protected ServletContextHandler createContext(Server server, Connector connector, List<Handler> handlers) throws Exception {
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
server.addConnector(connector);
if (handlers != null && !handlers.isEmpty()) {
for (Handler handler : handlers) {
if (handler instanceof HandlerWrapper) {
((HandlerWrapper) handler).setHandler(server.getHandler());
server.setHandler(handler);
} else {
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.addHandler(server.getHandler());
handlerCollection.addHandler(handler);
server.setHandler(handlerCollection);
}
}
}
return context;
}
Aggregations