Search in sources :

Example 26 with Builder

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));
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZPath(org.apache.zookeeper.server.jersey.jaxb.ZPath) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 27 with Builder

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));
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) ZStat(org.apache.zookeeper.server.jersey.jaxb.ZStat) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Test(org.junit.Test)

Example 28 with Builder

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;
}
Also used : Cookie(javax.ws.rs.core.Cookie) Builder(com.sun.jersey.api.client.WebResource.Builder)

Aggregations

Builder (com.sun.jersey.api.client.WebResource.Builder)28 ClientResponse (com.sun.jersey.api.client.ClientResponse)24 WebResource (com.sun.jersey.api.client.WebResource)20 EurekaHttpResponseBuilder (com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder)8 Test (org.junit.Test)6 Stat (org.apache.zookeeper.data.Stat)4 InstanceInfo (com.netflix.appinfo.InstanceInfo)3 Map (java.util.Map)3 CacheBuilder (com.google.common.cache.CacheBuilder)2 Client (com.sun.jersey.api.client.Client)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ZSession (org.apache.zookeeper.server.jersey.jaxb.ZSession)2 OrcidError (org.orcid.jaxb.model.error_v2.OrcidError)2 DeprecatedRecordException (org.orcid.listener.exception.DeprecatedRecordException)2 LockedRecordException (org.orcid.listener.exception.LockedRecordException)2 AbstractLoadBalancerAwareClient (com.netflix.client.AbstractLoadBalancerAwareClient)1 ClientException (com.netflix.client.ClientException)1 ClientFactory (com.netflix.client.ClientFactory)1 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1