Search in sources :

Example 76 with Handler

use of org.eclipse.jetty.server.Handler in project blade by biezhi.

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);
}
Also used : Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) DeferredAuthentication(org.eclipse.jetty.security.authentication.DeferredAuthentication) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeferredAuthentication(org.eclipse.jetty.security.authentication.DeferredAuthentication) Authentication(org.eclipse.jetty.server.Authentication)

Example 77 with Handler

use of org.eclipse.jetty.server.Handler in project sonarqube by SonarSource.

the class MockHttpServer method getMockHandler.

/**
   * Creates an {@link org.mortbay.jetty.handler.AbstractHandler handler} returning an arbitrary String as a response.
   *
   * @return never <code>null</code>.
   */
public Handler getMockHandler() {
    Handler handler = new AbstractHandler() {

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            targets.add(target);
            setResponseBody(getMockResponseData());
            setRequestBody(IOUtils.toString(baseRequest.getInputStream()));
            response.setStatus(mockResponseStatus);
            response.setContentType("text/xml;charset=utf-8");
            write(getResponseBody(), response.getOutputStream());
            baseRequest.setHandled(true);
        }
    };
    return handler;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler)

Example 78 with Handler

use of org.eclipse.jetty.server.Handler in project hadoop by apache.

the class HttpServer2 method start.

/**
   * Start the server. Does not wait for the server to start.
   */
public void start() throws IOException {
    try {
        try {
            openListeners();
            webServer.start();
        } catch (IOException ex) {
            LOG.info("HttpServer.start() threw a non Bind IOException", ex);
            throw ex;
        } catch (MultiException ex) {
            LOG.info("HttpServer.start() threw a MultiException", ex);
            throw ex;
        }
        // Make sure there is no handler failures.
        Handler[] hs = webServer.getHandlers();
        for (Handler handler : hs) {
            if (handler.isFailed()) {
                throw new IOException("Problem in starting http server. Server handlers failed");
            }
        }
        // Make sure there are no errors initializing the context.
        Throwable unavailableException = webAppContext.getUnavailableException();
        if (unavailableException != null) {
            // Have to stop the webserver, or else its non-daemon threads
            // will hang forever.
            webServer.stop();
            throw new IOException("Unable to initialize WebAppContext", unavailableException);
        }
    } catch (IOException e) {
        throw e;
    } catch (InterruptedException e) {
        throw (IOException) new InterruptedIOException("Interrupted while starting HTTP server").initCause(e);
    } catch (Exception e) {
        throw new IOException("Problem starting http server", e);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Handler(org.eclipse.jetty.server.Handler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) MultiException(org.eclipse.jetty.util.MultiException) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) FileNotFoundException(java.io.FileNotFoundException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) IOException(java.io.IOException)

Example 79 with Handler

use of org.eclipse.jetty.server.Handler in project pulsar by yahoo.

the class WebService method start.

public void start() throws PulsarServerException {
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone(WebService.HANDLER_REQUEST_LOG_TZ);
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
        log.info("Web Service started at {}", pulsar.getWebServiceAddress());
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) GeneralSecurityException(java.security.GeneralSecurityException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 80 with Handler

use of org.eclipse.jetty.server.Handler in project hadoop by apache.

the class SLSWebApp method start.

public void start() throws Exception {
    // static files
    final ResourceHandler staticHandler = new ResourceHandler();
    staticHandler.setMimeTypes(new MimeTypes());
    staticHandler.setResourceBase("html");
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                // timeunit
                // second, divide millionsecond / 1000
                int timeunit = 1000;
                String timeunitLabel = "second";
                if (request.getParameter("u") != null && request.getParameter("u").equalsIgnoreCase("m")) {
                    timeunit = 1000 * 60;
                    timeunitLabel = "minute";
                }
                // http request
                if (target.equals("/")) {
                    printPageIndex(request, response);
                } else if (target.equals("/simulate")) {
                    printPageSimulate(request, response, timeunit, timeunitLabel);
                } else if (target.equals("/track")) {
                    printPageTrack(request, response, timeunit, timeunitLabel);
                } else // js/css request
                if (target.startsWith("/js") || target.startsWith("/css")) {
                    response.setCharacterEncoding("utf-8");
                    staticHandler.handle(target, baseRequest, request, response);
                } else // json request
                if (target.equals("/simulateMetrics")) {
                    printJsonMetrics(request, response);
                } else if (target.equals("/trackMetrics")) {
                    printJsonTrack(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    server = new Server(port);
    server.setHandler(handler);
    server.start();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) MimeTypes(org.eclipse.jetty.http.MimeTypes) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Handler (org.eclipse.jetty.server.Handler)119 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)37 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)37 Server (org.eclipse.jetty.server.Server)35 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)26 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)25 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)23 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)22 ArrayList (java.util.ArrayList)21 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)18 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)18 IOException (java.io.IOException)15 Test (org.junit.Test)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 ServerConnector (org.eclipse.jetty.server.ServerConnector)13 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Request (org.eclipse.jetty.server.Request)12 URI (java.net.URI)11 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)11