Java Code Examples for javax.ws.rs.core.GenericEntity#getEntity()

The following examples show how to use javax.ws.rs.core.GenericEntity#getEntity() . 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: JAXRS20ClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void doTestPostGetCollectionGenericEntityAndType(WebClient wc, MediaType mediaType) throws Exception {

        wc.accept(mediaType).type(mediaType);
        GenericEntity<List<Book>> collectionEntity = createGenericEntity();
        InvocationCallback<List<Book>> callback = new ListBookInvocationCallback();

        Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity, mediaType),
                                                    callback);

        List<Book> books2 = future.get();
        assertNotNull(books2);

        List<Book> books = collectionEntity.getEntity();
        assertNotSame(books, books2);
        assertEquals(2, books2.size());
        Book b11 = books.get(0);
        assertEquals(123L, b11.getId());
        assertEquals("CXF in Action", b11.getName());
        Book b22 = books.get(1);
        assertEquals(124L, b22.getId());
        assertEquals("CXF Rocks", b22.getName());
        assertEquals(200, wc.getResponse().getStatus());
    }
 
Example 2
Source File: JAXRS20ClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostGetCollectionGenericEntityAndType2() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/bookstore/collections";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml").type("application/xml");
    GenericEntity<List<Book>> collectionEntity = createGenericEntity();
    GenericType<List<Book>> genericResponseType = new GenericType<List<Book>>() {
    };

    Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"),
                                                genericResponseType);

    List<Book> books2 = future.get();
    assertNotNull(books2);

    List<Book> books = collectionEntity.getEntity();
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books.get(0);
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books.get(1);
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
    assertEquals(200, wc.getResponse().getStatus());
}
 
Example 3
Source File: JAXRS20ClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostGetCollectionGenericEntityAndType3() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/bookstore/collections";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml").type("application/xml");
    GenericEntity<List<Book>> collectionEntity = createGenericEntity();
    GenericType<List<Book>> genericResponseType = new GenericType<List<Book>>() {
    };

    Future<Response> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"));

    Response r = future.get();
    List<Book> books2 = r.readEntity(genericResponseType);
    assertNotNull(books2);

    List<Book> books = collectionEntity.getEntity();
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books.get(0);
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books.get(1);
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
    assertEquals(200, wc.getResponse().getStatus());
}
 
Example 4
Source File: WebClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Response doInvoke(String httpMethod,
                            Object body,
                            Class<?> requestClass,
                            Type inGenericType,
                            Class<?> responseClass,
                            Type outGenericType) {
    Annotation[] inAnns = null;
    if (body instanceof Entity) {
        Entity<?> entity = (Entity<?>)body;
        setEntityHeaders(entity);
        body = entity.getEntity();
        requestClass = body.getClass();
        inGenericType = body.getClass();
        inAnns = entity.getAnnotations();
    }
    if (body instanceof GenericEntity) {
        GenericEntity<?> genericEntity = (GenericEntity<?>)body;
        body = genericEntity.getEntity();
        requestClass = genericEntity.getRawType();
        inGenericType = genericEntity.getType();
    }
    MultivaluedMap<String, String> headers = prepareHeaders(responseClass, body);
    resetResponse();
    Response r = null;
    try {
        r = doChainedInvocation(httpMethod, headers, body, requestClass, inGenericType,
                                         inAnns, responseClass, outGenericType, null, null);
    } finally {
        resetResponseStateImmediatelyIfNeeded();
    }

    int status = r.getStatus();
    if (status != 304 && status >= 300 && responseClass != Response.class) {
        throw convertToWebApplicationException(r);
    }
    return r;
}
 
Example 5
Source File: WebClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void prepareAsyncClient(String httpMethod,
                               Object body,
                               Class<?> requestClass,
                               Type inType,
                               Class<?> respClass,
                               Type outType,
                               JaxrsClientCallback<?> cb) {
    Annotation[] inAnns = null;
    if (body instanceof Entity) {
        Entity<?> entity = (Entity<?>)body;
        setEntityHeaders(entity);
        body = entity.getEntity();
        requestClass = body.getClass();
        inType = body.getClass();
        inAnns = entity.getAnnotations();
    }
    if (body instanceof GenericEntity) {
        GenericEntity<?> genericEntity = (GenericEntity<?>)body;
        body = genericEntity.getEntity();
        requestClass = genericEntity.getRawType();
        inType = genericEntity.getType();
    }

    MultivaluedMap<String, String> headers = prepareHeaders(respClass, body);
    resetResponse();

    Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType,
                              inAnns, respClass, outType, null, null);

    m.getExchange().setSynchronous(false);
    setAsyncMessageObserverIfNeeded(m.getExchange());
    m.getExchange().put(JaxrsClientCallback.class, cb);

    doRunInterceptorChain(m);
}
 
Example 6
Source File: JaxRsUtil.java    From SciGraph with Apache License 2.0 3 votes vote down vote up
/***
 * Build a Response, optionally wrapped in a JSONP callback.
 * <p>The response will be wrapped in a JSONP callback if any of the following apply:
 * <ul>
 * <li> The requested media type is <em>application/javascript</em>
 * <li> The callback is not null or empty
 * </ul>
 * @param request The request
 * @param response The response to wrap
 * @param callback The callback
 * @return A Response object, wrapped if necessary.
 */
public static Object wrapJsonp(Request request, GenericEntity<?> response, @Nullable String callback) {
  if (JaxRsUtil.isVariant(request, CustomMediaTypes.APPLICATION_JSONP_TYPE) || !isNullOrEmpty(callback)) {
    callback = Optional.ofNullable(callback).orElse(DEFAULT_JSONP_CALLBACK);
    
    return new JSONWrappedObject(format("%s(", callback), ");", response.getEntity());
  } else {
    return Response.ok(response).build();
  }
}