Robin Wieruch
Robin Wieruch

Reputation: 15898

REST - Get Resource URI out of Response (NOT ClientResponse)

How to get a resource URI path/location out of the Response.class? When I invoke my restful service with Apache CXF client API like this:

 Response res = resource.post(object);

I get back the JAX-RS Response type. CXF doesnt an own implementation of Response like Jersey or RestEasy do. So how to get the URI, where I created my object, out of Response.class?

In Jersey I am dealing with a ClientResponse.class. There I can handle this with:

 res.getLocation(); 

RestEasy has a ClientResponse.class as well and I can handle the problem like jersey does.

Upvotes: 1

Views: 3510

Answers (1)

Joe23
Joe23

Reputation: 5782

The Jersey ClientResponse gets the Location from the headers:

/**
 * Get the location.
 *
 * @return the location, otherwise <code>null</code> if not present.
 */
public URI getLocation() {
    String l = getHeaders().getFirst("Location");
    return (l != null) ? URI.create(l) : null;
}

The JAX-RS Response provides header information via getMetadata():

public MultivaluedMap<String, Object> getMetadata() {
    if (headers != null)
        return headers;

    headers = new OutBoundHeaders();

    for (int i = 0; i < values.length; i++)
        if (values[i] != null)
            headers.putSingle(ResponseBuilderHeaders.getNameFromId(i), values[i]);

    Iterator i = nameValuePairs.iterator();
    while (i.hasNext()) {
        headers.add((String)i.next(), i.next());
    }

    return headers;
}

So what I would try is:

response.getMetadata().getFirst("Location");

(If that doesn't work print the Metadata content. Maybe the key has another name.)

Upvotes: 4

Related Questions