Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jersey HttpServletResponse Injection #62

Closed
scottwinkler opened this issue Aug 25, 2017 · 2 comments
Closed

Jersey HttpServletResponse Injection #62

scottwinkler opened this issue Aug 25, 2017 · 2 comments
Assignees
Milestone

Comments

@scottwinkler
Copy link

From the docs, I can inject an HttpServletRequest using the following:

ResourceConfig app = new ResourceConfig()
    .packages("com.amazonaws.serverless.proxy.test.jersey")
    .register(new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(AwsProxyServletRequestFactory.class)
                .to(HttpServletRequest.class)
                .in(RequestScoped.class);
            bindFactory(AwsProxyServletContextFactory.class)
                .to(ServletContext.class)
                .in(RequestScoped.class);
        }
    });

And I can access the HttpServletRequest using:

@Path("/my-servlet") @GET
public String echoServletHeaders(@Context HttpServletRequest context) {
    Enumeration<String> headerNames = context.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
    }
    return "servlet";
}

However, I have not found a way to inject the HttpServletResponse. It would be nice to bind a factory method like:

bindFactory(AwsProxyServletResponseFactory.class)
                .to(HttpServletResponse.class)
                .in(RequestScoped.class);

So that I can access the HttpServletResponse using the @context object, e.g. something like:

@Path("/my-servlet") @GET
public String echoServletHeaders(@Context HttpServletRequest request, @Context HttpServletResponse) {
//stuff
}

If this is not possible, is there another way to inject the HttpServletResponse? I tried using the @context above the @path, but the HttpServletResponse always returns null. Here is a snippet of what I tried:

@Context private ServletContext myContext;
    @Context private HttpServletResponse httpResponse;
    @Path("/saml")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response processSAML(@Context HttpServletRequest httpRequest) {
//httpResponse is null
@sapessi
Copy link
Collaborator

sapessi commented Aug 25, 2017

Hi @scottwinkler, at the moment we only have a Factory implementation for the request and context objects. I can definitely look at adding one for the servlet response. I'll mark this issue as an enhancement and look into it in the next few days.

@sapessi sapessi self-assigned this Aug 25, 2017
@sapessi sapessi added this to the Release 0.9 milestone Jan 12, 2018
sapessi added a commit that referenced this issue Jan 13, 2018
…sts and responses as underlying objects. This solves #62 and simplifies writers and readers by condensing everything in the servlet implementation
@sapessi
Copy link
Collaborator

sapessi commented Jan 22, 2018

This is merged into master and have added a unit test for it.

@sapessi sapessi closed this as completed Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants