Charles
Charles

Reputation: 11758

Jersey - Modify Query Parameter inside a Resource

I'm calling ResourceB Inside ResourceA.

Both uses the same query param "param1".

How can I change the value of "param1" inside ResourceA before calling ResourceB.

Here is the code:

package com.example;

import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sun.jersey.api.core.ResourceContext;

public abstract class MyResource {
    ResponseBuilder response;
    Request request;
    protected String param1;

    public MyResource(@QueryParam("param1") String param1, @Context Request request) {
        this.param1 = param1;
        this.request = request;
        this.response = Response.ok();
    }
}

public class ResourceA extends MyResource {
    public ResourceA(String param1, Request request) {
        super(param1, request);
    }
    @Context private ResourceContext rc;
    public Response postJSON(String postData) {
        JSONObject data = JSONObject.fromObject(postData);
        if (data.has("resourceB")) {
            ResourceB resourceB = rc.getResource(ResourceB.class);
            // resourceB.setQueryParams("param1", "my new param 1");
            resourceB.postJSON(data.getJSONArray("resourceB"));
        }
    }
}

public class ResourceB extends MyResource {
    public ResourceB(String param1, Request request) {
        super(param1, request);
    }

    public Response postJSON(JSONArray data) {
        // this.params1 should not be "my new param 1"
    }
}

Upvotes: 4

Views: 997

Answers (1)

Stepan Vavra
Stepan Vavra

Reputation: 4044

How about using the ResourceContext.initResource(T resource), initialize the resource with the modified values and control what the DI provider injects by declaring JAX-RS annotated fields?
If I understand your question right, you would like to modify the request context after a matching resource method is found or to hook into the injection and change some of the values. I'm not sure if that is possible.

However, as I'm suggesting, if it's ok to do some design modifications, you could initialize the resource programmatically and leverage the fields only injection:

public Response postJSON(String postData) {
    JSONObject data = JSONObject.fromObject(postData);
    if (data.has("resourceB")) {
        ResourceB resourceB = new ResourceB(/* pass here the modified query param */ "my new param 1");
        // let the DI provider inject the JAX-RS annotated fields only
        resourceB = rc.initResource(resourceB);
        // resourceB.setQueryParams("param1", "my new param 1");
        resourceB.postJSON(data.getJSONArray("resourceB"));
    }
}

The ResourceB class itself (and its super class) would declare annotated fields you would like to have injected, while the @QueryParam("param1") (and possibly other JAX-RS aware fields) would be declared in the constructor so that it does not get overridden.

public abstract class MyResource {
    ResponseBuilder response;

    // this gets injected in the ResourceContext.initResource() method
    @Context
    Request request;

    // additional JAX-RS annotated fields you would like to have injected such as
    @PathParam("id")
    private String pathParamId;

    protected String param1;

    // constructor JAX-RS annotated parameters would not get re-initialized  
    public MyResource(@QueryParam("param1") String param1) {
        this.param1 = param1;
        this.request = request;
        this.response = Response.ok();
    }
}

Upvotes: 3

Related Questions