use of com.sun.jersey.api.client.WebResource.Builder in project zookeeper by apache.
the class RootTest method testCreate.
@Test
public void testCreate() throws Exception {
String path = "/";
String name = "roottest-create";
byte[] data = "foo".getBytes();
WebResource wr = znodesr.path(path).queryParam("dataformat", "utf8").queryParam("name", name);
Builder builder = wr.accept(MediaType.APPLICATION_JSON);
ClientResponse cr;
cr = builder.post(ClientResponse.class, data);
Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus());
ZPath zpath = cr.getEntity(ZPath.class);
Assert.assertEquals(new ZPath(path + name), zpath);
Assert.assertEquals(znodesr.path(path).toString(), zpath.uri);
// use out-of-band method to verify
byte[] rdata = zk.getData(zpath.path, false, new Stat());
Assert.assertTrue(new String(rdata) + " == " + new String(data), Arrays.equals(rdata, data));
}
use of com.sun.jersey.api.client.WebResource.Builder in project zookeeper by apache.
the class SetTest method testSet.
@Test
public void testSet() throws Exception {
if (expectedStat != null) {
zk.create(expectedStat.path, "initial".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
WebResource wr = znodesr.path(path).queryParam("dataformat", encoding);
if (data == null) {
wr = wr.queryParam("null", "true");
}
Builder builder = wr.accept(accept).type(MediaType.APPLICATION_OCTET_STREAM);
ClientResponse cr;
if (data == null) {
cr = builder.put(ClientResponse.class);
} else {
// this shouldn't be necessary (wrapping data with string)
// but without it there are problems on the server - ie it
// hangs for 30 seconds and doesn't get the data.
// TODO investigate
cr = builder.put(ClientResponse.class, new String(data));
}
Assert.assertEquals(expectedStatus, cr.getClientResponseStatus());
if (expectedStat == null) {
return;
}
ZStat zstat = cr.getEntity(ZStat.class);
Assert.assertEquals(expectedStat, zstat);
// use out-of-band method to verify
byte[] data = zk.getData(zstat.path, false, new Stat());
if (data == null && this.data == null) {
return;
} else if (data == null || this.data == null) {
Assert.fail((data == null ? null : new String(data)) + " == " + (this.data == null ? null : new String(this.data)));
} else {
Assert.assertTrue(new String(data) + " == " + new String(this.data), Arrays.equals(data, this.data));
}
}
use of com.sun.jersey.api.client.WebResource.Builder in project coprhd-controller by CoprHD.
the class RESTClient method setResourceHeaders.
/**
* Sets required headers into the passed WebResource.
*
* @param resource The resource to which headers are added.
* @param vplexSessionId The VPLEX API session id or null.
* @param jsonFormat the JSON format to expect in the response.
* @param cacheControlMaxAge cache control max age
*/
Builder setResourceHeaders(WebResource resource, String vplexSessionId, String jsonFormat, String cacheControlMaxAge) {
// Set the headers for the username, password, and connection.
Builder resBuilder = resource.header(VPlexApiConstants.USER_NAME_HEADER, _username).header(VPlexApiConstants.PASS_WORD_HEADER, _password).header(VPlexApiConstants.CONNECTION_HEADER, VPlexApiConstants.CONNECTION_HEADER_VALUE_CLOSE);
// Set the session id cookie. Can be null on first request.
if (vplexSessionId != null) {
resBuilder.cookie(new Cookie(VPlexApiConstants.SESSION_COOKIE, vplexSessionId));
}
// will look like this: application/json;format=1 or format=0
resBuilder.accept(MediaType.APPLICATION_JSON + jsonFormat);
// if using JSON response format 1, also set VPLEX API cache-control
if (VPlexApiConstants.ACCEPT_JSON_FORMAT_1.equals(jsonFormat)) {
resBuilder.header(VPlexApiConstants.CACHE_CONTROL_HEADER, VPlexApiConstants.CACHE_CONTROL_MAXAGE_KEY + cacheControlMaxAge);
}
return resBuilder;
}
Aggregations