Search in sources :

Example 26 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletRequestAttribute method writeAttribute.

@Override
public void writeAttribute(final HttpServerExchange exchange, final String newValue) throws ReadOnlyAttributeException {
    ServletRequestContext context = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (context != null) {
        context.getServletRequest().setAttribute(attributeName, newValue);
    } else {
        Map<String, String> attrs = exchange.getAttachment(HttpServerExchange.REQUEST_ATTRIBUTES);
        if (attrs == null) {
            exchange.putAttachment(HttpServerExchange.REQUEST_ATTRIBUTES, attrs = new HashMap<>());
        }
        attrs.put(attributeName, newValue);
    }
}
Also used : HashMap(java.util.HashMap) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 27 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletBlockingHttpExchange method close.

@Override
public void close() throws IOException {
    ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (!exchange.isComplete()) {
        try {
            HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
            request.closeAndDrainRequest();
        } finally {
            HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
            response.closeStreamAndWriter();
        }
    } else {
        try {
            HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
            request.freeResources();
        } finally {
            HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
            response.freeResources();
        }
    }
}
Also used : HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl)

Example 28 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletAuthenticationCallHandler method handleRequest.

/**
     * Only allow the request through if successfully authenticated or if authentication is not required.
     *
     * @see io.undertow.server.HttpHandler#handleRequest(io.undertow.server.HttpServerExchange)
     */
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }
    SecurityContext context = exchange.getSecurityContext();
    if (context.authenticate()) {
        if (!exchange.isComplete()) {
            next.handleRequest(exchange);
        }
    } else {
        if (exchange.getStatusCode() >= StatusCodes.BAD_REQUEST && !exchange.isComplete()) {
            ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
            src.getOriginalResponse().sendError(exchange.getStatusCode());
        } else {
            exchange.endExchange();
        }
    }
}
Also used : SecurityContext(io.undertow.security.api.SecurityContext) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 29 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletConfidentialityConstraintHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    final AuthorizationManager authorizationManager = servletRequestContext.getDeployment().getDeploymentInfo().getAuthorizationManager();
    TransportGuaranteeType connectionGuarantee = servletRequestContext.getOriginalRequest().isSecure() ? TransportGuaranteeType.CONFIDENTIAL : TransportGuaranteeType.NONE;
    TransportGuaranteeType transportGuarantee = authorizationManager.transportGuarantee(connectionGuarantee, servletRequestContext.getTransportGuarenteeType(), servletRequestContext.getOriginalRequest());
    servletRequestContext.setTransportGuarenteeType(transportGuarantee);
    if (TransportGuaranteeType.REJECTED == transportGuarantee) {
        HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
        response.sendError(StatusCodes.FORBIDDEN);
        return;
    }
    super.handleRequest(exchange);
}
Also used : TransportGuaranteeType(io.undertow.servlet.api.TransportGuaranteeType) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthorizationManager(io.undertow.servlet.api.AuthorizationManager)

Example 30 with ServletRequestContext

use of io.undertow.servlet.handlers.ServletRequestContext in project undertow by undertow-io.

the class ServletFormAuthenticationMechanism method storeInitialLocation.

@Override
protected void storeInitialLocation(final HttpServerExchange exchange) {
    if (!saveOriginalRequest) {
        return;
    }
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchange, true);
    Session session;
    if (System.getSecurityManager() == null) {
        session = httpSession.getSession();
    } else {
        session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
    }
    SessionManager manager = session.getSessionManager();
    if (seenSessionManagers.add(manager)) {
        manager.registerSessionListener(LISTENER);
    }
    session.setAttribute(SESSION_KEY, RedirectBuilder.redirect(exchange, exchange.getRelativePath()));
    SavedRequest.trySaveRequest(exchange);
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) SessionManager(io.undertow.server.session.SessionManager) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) Session(io.undertow.server.session.Session)

Aggregations

ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)40 IOException (java.io.IOException)8 Session (io.undertow.server.session.Session)5 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)5 ServletException (javax.servlet.ServletException)5 SecurityContext (io.undertow.security.api.SecurityContext)4 Account (io.undertow.security.idm.Account)4 HttpServerExchange (io.undertow.server.HttpServerExchange)4 HttpString (io.undertow.util.HttpString)4 ServletRequest (javax.servlet.ServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Subject (javax.security.auth.Subject)3 GenericMessageInfo (org.jboss.security.auth.message.GenericMessageInfo)3 AuthenticatedSessionManager (io.undertow.security.api.AuthenticatedSessionManager)2 Resource (io.undertow.server.handlers.resource.Resource)2 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)2 AuthorizationManager (io.undertow.servlet.api.AuthorizationManager)2 SingleConstraintMatch (io.undertow.servlet.api.SingleConstraintMatch)2