Java Code Examples for javax.ws.rs.client.Invocation.Builder#headers()

The following examples show how to use javax.ws.rs.client.Invocation.Builder#headers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractRestClient.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
private Pair<Builder, Entity<?>> buildRequest(
                                 String path, String id, Object object,
                                 MultivaluedMap<String, Object> headers,
                                 Map<String, Object> params) {
    WebTarget target = this.target;
    if (params != null && !params.isEmpty()) {
        for (Map.Entry<String, Object> param : params.entrySet()) {
            target = target.queryParam(param.getKey(), param.getValue());
        }
    }

    Builder builder = id == null ? target.path(path).request() :
                      target.path(path).path(encode(id)).request();

    String encoding = null;
    if (headers != null && !headers.isEmpty()) {
        // Add headers
        builder = builder.headers(headers);
        encoding = (String) headers.getFirst("Content-Encoding");
    }

    /*
     * We should specify the encoding of the entity object manually,
     * because Entity.json() method will reset "content encoding =
     * null" that has been set up by headers before.
     */
    Entity<?> entity;
    if (encoding == null) {
        entity = Entity.json(object);
    } else {
        Variant variant = new Variant(MediaType.APPLICATION_JSON_TYPE,
                                      (String) null, encoding);
        entity = Entity.entity(object, variant);
    }
    return Pair.of(builder, entity);
}
 
Example 2
Source File: InvocationBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeadersMethod() {
    // the javadoc for the Invocation.Builder.headers(MultivaluedMap) method says that
    // invoking this method should remove all previously existing headers
    Client client = ClientBuilder.newClient().register(TestFilter.class);
    Builder builder = client.target("http://localhost:8080/notReal").request();
    builder.header("Header1", "a");
    builder.header("UnexpectedHeader", "should be removed");
    MultivaluedMap<String, Object> map = new MultivaluedHashMap<>();
    map.putSingle("Header1", "b");
    builder.headers(map);

    Response response = builder.get();
    String sentHeaders = response.readEntity(String.class);
    assertTrue(sentHeaders.contains("Header1=b"));
    assertFalse(sentHeaders.contains("UnexpectedHeader"));

    // If value is null then all current headers of the same name 
    // should be removed.
    builder.header("Header1", null);
    builder.header("Header2", "b");
    response = builder.get();
    sentHeaders = response.readEntity(String.class);
    assertTrue(sentHeaders.contains("Header2=b"));
    assertFalse(sentHeaders.contains("Header1"));
    
    // null headers map should clear all headers
    builder.headers(null);
    response = builder.get();
    assertEquals("", response.readEntity(String.class));
}