Search in sources :

Example 6 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class GuicedResteasy method call.

public void call(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NotFoundException {
    final HttpCallContext ctx = HttpCallContext.set(request, response, context);
    if (forceUTF8DefaultCharset && requestCharsetHelper != null)
        requestCharsetHelper.applyDefaultCharset(request);
    Timer.Context timer = null;
    if (httpCalls != null)
        timer = httpCalls.time();
    final String oldThreadName;
    final Thread thread;
    if (ctx.isVerbose() || renameThreads) {
        thread = Thread.currentThread();
        oldThreadName = thread.getName();
    } else {
        thread = null;
        oldThreadName = null;
    }
    try {
        if (thread != null && oldThreadName != null)
            thread.setName(ctx.getLogId() + " " + ctx.getRequestInfo());
        // Share the call id to log4j
        Tracing.start(ctx.getLogId(), ctx.isVerbose());
        // Share the call id to log4j
        MDC.put(TracingConstants.MDC_HTTP_REMOTE_ADDR, ctx.getRequest().getRemoteAddr());
        MDC.put(TracingConstants.MDC_SERVLET_CONTEXT_PATH, ctx.getServletContext().getContextPath());
        MDC.put(TracingConstants.MDC_HTTP_REQUEST_URI, ctx.getRequest().getRequestURI());
        // Add the session id (if present)
        final HttpSession session = ctx.getRequest().getSession(false);
        if (session != null) {
            MDC.put(TracingConstants.MDC_HTTP_SESSION_ID, session.getId());
        }
        try {
            // Optionally log the request
            if (log.isDebugEnabled())
                log.debug(ctx.getRequestInfo());
            // Get or create the dispatcher
            ServletContainerDispatcher dispatcher = getDispatcher();
            dispatcher.service(request.getMethod(), request, response, handleNotFoundException);
        } catch (NotFoundException e) {
            if (httpNotFoundExceptions != null)
                httpNotFoundExceptions.mark();
            // let the caller handle this
            throw e;
        } catch (ServletException | IOException | RuntimeException | Error e) {
            if (httpExceptions != null)
                httpExceptions.mark();
            // try to pretty print, otherwise rethrow
            tryHandleException(ctx, response, e);
        }
    } finally {
        if (timer != null)
            timer.stop();
        HttpCallContext.clear();
        Tracing.stop(ctx.getLogId());
        MDC.clear();
        if (oldThreadName != null && thread != null)
            thread.setName(oldThreadName);
    }
}
Also used : HttpCallContext(com.peterphi.std.guice.web.HttpCallContext) HttpSession(javax.servlet.http.HttpSession) NotFoundException(javax.ws.rs.NotFoundException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Timer(com.codahale.metrics.Timer) ServletContainerDispatcher(org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher)

Example 7 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class RequestScoping method scope.

public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) {
    final String name = key.toString();
    return new Provider<T>() {

        public T get() {
            final HttpCallContext ctx = HttpCallContext.get();
            final HttpServletRequest request = ctx.getRequest();
            synchronized (request) {
                final Object obj = request.getAttribute(name);
                if (NullObject.INSTANCE == obj) {
                    return null;
                } else {
                    @SuppressWarnings("unchecked") T t = (T) obj;
                    if (t == null) {
                        // Construct a new instance
                        t = creator.get();
                        // Cache the instance (unless it's a circular proxy)
                        if (!Scopes.isCircularProxy(t)) {
                            if (t == null)
                                request.setAttribute(name, NullObject.INSTANCE);
                            else
                                request.setAttribute(name, t);
                        }
                    }
                    return t;
                }
            }
        }

        @Override
        public String toString() {
            return String.format("%s[%s]", creator, RequestScoping.INSTANCE);
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext) Provider(com.google.inject.Provider)

Example 8 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class OAuthUser method postConstruct.

@Override
public void postConstruct() {
    // If there's an Authorization: Bearer
    final HttpCallContext ctx = HttpCallContext.peek();
    if (ctx != null) {
        final HttpServletRequest request = ctx.getRequest();
        final String header = request.getHeader("Authorization");
        if (UserManagerBearerToken.isUserManagerBearerAuthorizationHeader(header)) {
            final String token = UserManagerBearerToken.getTokenFromBearerAuthorizationHeader(header);
            this.staticSession = apiSessionRefCacheProvider.get().getOrCreateSessionRef(token);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Example 9 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class RedirectToOAuthAccessRefuser method isGETRequest.

private boolean isGETRequest() {
    final HttpCallContext ctx = HttpCallContext.peek();
    if (ctx == null)
        // Not an HTTP call!
        return false;
    final HttpServletRequest request = ctx.getRequest();
    if (request == null)
        // No request!
        return false;
    else
        return StringUtils.equalsIgnoreCase(request.getMethod(), "GET");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Example 10 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class RedirectToOAuthAccessRefuser method getRequestURI.

private String getRequestURI() {
    final HttpCallContext ctx = HttpCallContext.peek();
    if (ctx == null)
        // not an HTTP request!
        return null;
    final HttpServletRequest request = ctx.getRequest();
    if (request == null)
        // No request info
        return null;
    final String uri = request.getRequestURL().toString();
    final String qs = request.getQueryString();
    if (qs == null)
        return uri;
    else
        return uri + "?" + qs;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Aggregations

HttpCallContext (com.peterphi.std.guice.web.HttpCallContext)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 HttpSession (javax.servlet.http.HttpSession)4 Provider (com.google.inject.Provider)2 Timer (com.codahale.metrics.Timer)1 GuiceBuilder (com.peterphi.std.guice.apploader.impl.GuiceBuilder)1 GuiceRegistry (com.peterphi.std.guice.apploader.impl.GuiceRegistry)1 WebappGuiceRole (com.peterphi.std.guice.web.rest.setup.WebappGuiceRole)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 Cookie (javax.servlet.http.Cookie)1 NotFoundException (javax.ws.rs.NotFoundException)1 ServletContainerDispatcher (org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher)1