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);
}
}
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);
}
};
}
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);
}
}
}
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");
}
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;
}
Aggregations