Closed dehora closed 11 years ago
Thanks for the bug report. I'll look into this tonight.
This is not a dropwizard-scala issue. Same issue occurs if you use java dropwizard or jersey all by itself.
com.sun.jersey.server.impl.model.parameter.HttpContextInjectableProvider
is where @Context
related things are injected from, and demonstrates how you can 1) make a modification and suggest it directly to jersey or 2) create your own provider.
First, note that injecting an instance of javax.ws.rs.core.Request
works properly (but, obviously, loses the fact that it's an HttpRequestContext
. So you could ask for the Request
and do an instance of / scala match on the type:
def sampleRequest(@Context request: Request): Response = { ... }
Or if you really want to avoid type checking the Request
object you can create your own provider. The simplest implementation would look like this:
private final class HttpRequestContextInjectable extends AbstractHttpContextInjectable[HttpRequestContext] {
def getValue(c: HttpContext): HttpRequestContext = c.getRequest
}
class HttpRequestContextProvider extends InjectableProvider[Context, Type] {
private val _httpRequestContextInjectable = new HttpRequestContextInjectable
def getScope: ComponentScope = ComponentScope.PerRequest
def getInjectable(ic: ComponentContext, a: Context, c: Type): Injectable[_] = if (c == classOf[HttpRequestContext]) _httpRequestContextInjectable else null
}
object SampleService extends ScalaService[Configuration] {
def initialize(bootstrap: Bootstrap[Configuration]) {
bootstrap.addBundle(new ScalaBundle)
}
def run(cfg: Configuration, env: Environment) {
env.getJerseyResourceConfig.getClasses.add(classOf[HttpRequestContextProvider])
}
}
Ultimately if you think this belongs in jersey then you need to just add another line to this method and send them a pull request:
public HttpContextInjectableProvider() {
injectables = new HashMap<Type, Injectable>();
HttpContextRequestInjectable re = new HttpContextRequestInjectable();
injectables.put(HttpHeaders.class, re);
injectables.put(Request.class, re);
injectables.put(SecurityContext.class, re);
// Make a pull request to add the following line:
injectables.put(RequestHttpContext.class, re);
injectables.put(HttpContext.class, new HttpContextInjectable());
injectables.put(UriInfo.class, new UriInfoInjectable());
injectables.put(ExtendedUriInfo.class, new UriInfoInjectable());
}
Using 0.6.2 or 0.6.2-1, HttpRequestContext is not available to resources - HttpServletRequest, HttpContext and UriInfo are available.
Example signature