Hi Jan
On 19/11/12 14:16, janb wrote:
Hi,

I'm currently wondering of what would be the best (CXF) way to handle a
response URL that I get, after saving a new resource.

In REST it is best practice to return Status Code 201 and a URL pointing to
the new resource after a POST request containing any kind of resource that
should be stored. My service I working well using JAX-RS annotations and CXF
as a Webservice-Stack. Now I would like to test my service. Therefore I
create a new resource receive the URL for this new resource back and would
like to GET this resource for purposes of unit testing.
Here is what I did:


Response response = roleService.create(roleTO, null);

assertNotNull(response);
assertEquals(org.apache.http.HttpStatus.SC_CREATED, response.getStatus());

Client client = WebClient.client(roleService);
WebClient webClient = WebClient.fromClient(client);
int offset = webClient.getBaseURI().toString().length();
webClient.replacePath(response.getLocation().toString().substring(offset));
RoleTO actual = webClient.get(RoleTO.class);


This code does not look nice to me. Since this should be a common usecase,
I'm wondering if there is a better way to do this?
I would like to reuse the configuration of my proxy object, to reuse my
provider and authentication configuration.


Can you clarify please what the code above does ?

Suppose we have "http://bar/roles";

and we have the role service returning

"http://bar/roles/1";.

One option is indeed do

WebClient roleToService = WebClient.fromClient(roleService);

this will copy the actual original properties

and then do

// false flag will let use locations pointing to URIs not starting from the base URI
roleToService.to(response.getLocation(), true);

This should do it.

Alterntaively you can 'browse' with the the original roleService, though in this case you'll need to do some calculations:


String currentURILen = roleService.getCurrentUri().toString().length();

Response response = roleService.create(roleTO, null);
// advance
roleService.path(response.getLocation().substring(currentURILen));
// test new resource
// back to the URI pointing to "http://bar/roles";
roleService.back();

HTH, Sergey






Best regards
Jan



--
View this message in context: 
http://cxf.547215.n5.nabble.com/Best-way-to-handle-JAX-RS-Creat-Resource-URLs-tp5718738.html
Sent from the cxf-user mailing list archive at Nabble.com.


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to