Search in sources :

Example 16 with GenericType

use of javax.ws.rs.core.GenericType in project jersey by jersey.

the class GenericResponseTest method testPost.

@Test
public void testPost() {
    GenericType<Response> generic = new GenericType<Response>(Response.class);
    Entity entity = Entity.entity("entity", MediaType.WILDCARD_TYPE);
    WebTarget target = target("resource");
    SyncInvoker sync = target.request();
    Response response = sync.post(entity, generic);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("entity", response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) Entity(javax.ws.rs.client.Entity) GenericType(javax.ws.rs.core.GenericType) SyncInvoker(javax.ws.rs.client.SyncInvoker) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 17 with GenericType

use of javax.ws.rs.core.GenericType in project jersey by jersey.

the class GenericResponseTest method testGetGenericString.

@Test
public void testGetGenericString() {
    GenericType<String> generic = new GenericType<String>(String.class);
    WebTarget target = target("resource");
    SyncInvoker sync = target.request();
    final String entity = sync.get(generic);
    Assert.assertEquals("get", entity);
}
Also used : GenericType(javax.ws.rs.core.GenericType) SyncInvoker(javax.ws.rs.client.SyncInvoker) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 18 with GenericType

use of javax.ws.rs.core.GenericType in project jersey by jersey.

the class BasicClientTest method testAsyncClientInvocation.

@Test
public void testAsyncClientInvocation() throws InterruptedException, ExecutionException {
    final WebTarget resource = target().path("resource");
    Future<Response> f1 = resource.request().async().post(text("post1"));
    final Response response = f1.get();
    assertEquals("post1", response.readEntity(String.class));
    Future<String> f2 = resource.request().async().post(text("post2"), String.class);
    assertEquals("post2", f2.get());
    Future<List<JaxbString>> f3 = resource.request().async().get(new GenericType<List<JaxbString>>() {
    });
    assertEquals(Arrays.asList("a", "b", "c").toString(), f3.get().stream().map(input -> input.value).collect(Collectors.toList()).toString());
    CompletableFuture<String> future1 = new CompletableFuture<>();
    final TestCallback<Response> c1 = new TestCallback<Response>(future1) {

        @Override
        protected String process(Response result) {
            return result.readEntity(String.class);
        }
    };
    resource.request().async().post(text("post"), c1);
    assertEquals("post", future1.get());
    CompletableFuture<String> future2 = new CompletableFuture<>();
    final TestCallback<String> c2 = new TestCallback<String>(future2) {

        @Override
        protected String process(String result) {
            return result;
        }
    };
    resource.request().async().post(text("post"), c2);
    assertEquals("post", future2.get());
    CompletableFuture<String> future3 = new CompletableFuture<>();
    final TestCallback<List<JaxbString>> c3 = new TestCallback<List<JaxbString>>(future3) {

        @Override
        protected String process(List<JaxbString> result) {
            return result.stream().map(jaxbString -> jaxbString.value).collect(Collectors.toList()).toString();
        }
    };
    resource.request().async().get(c3);
    assertEquals(Arrays.asList("a", "b", "c").toString(), future3.get());
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ClientConfig(org.glassfish.jersey.client.ClientConfig) Assert.assertNotSame(org.junit.Assert.assertNotSame) Path(javax.ws.rs.Path) Client(javax.ws.rs.client.Client) CompletableFuture(java.util.concurrent.CompletableFuture) Application(javax.ws.rs.core.Application) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) AsyncInvoker(javax.ws.rs.client.AsyncInvoker) ClientBuilder(javax.ws.rs.client.ClientBuilder) Future(java.util.concurrent.Future) MediaType(javax.ws.rs.core.MediaType) JerseyTest(org.glassfish.jersey.test.JerseyTest) InvocationCallback(javax.ws.rs.client.InvocationCallback) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Assert.fail(org.junit.Assert.fail) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) POST(javax.ws.rs.POST) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Invocation(javax.ws.rs.client.Invocation) ThreadPoolExecutorProvider(org.glassfish.jersey.spi.ThreadPoolExecutorProvider) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Collectors(java.util.stream.Collectors) Entity.text(javax.ws.rs.client.Entity.text) NotFoundException(javax.ws.rs.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) WebTarget(javax.ws.rs.client.WebTarget) ClientAsyncExecutor(org.glassfish.jersey.client.ClientAsyncExecutor) Assert.assertEquals(org.junit.Assert.assertEquals) Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 19 with GenericType

use of javax.ws.rs.core.GenericType in project jersey by jersey.

the class ContactCardTest method testSearchByName.

@Test
public void testSearchByName() throws Exception {
    final WebTarget target = target().path("contact");
    target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(CARD_1, MediaType.APPLICATION_JSON_TYPE));
    target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(CARD_2, MediaType.APPLICATION_JSON_TYPE));
    Response response = target.path("search/name").queryParam("q", "er").request(MediaType.APPLICATION_JSON_TYPE).get();
    List<ContactCard> contactCards = response.readEntity(new GenericType<List<ContactCard>>() {
    });
    assertEquals(200, response.getStatus());
    assertEquals(2, contactCards.size());
    for (final ContactCard contactCard : contactCards) {
        assertTrue(contactCard.getFullName().contains("er"));
    }
    response = target.path("search/name").queryParam("q", "Foo").request(MediaType.APPLICATION_JSON_TYPE).get();
    contactCards = response.readEntity(new GenericType<List<ContactCard>>() {
    });
    assertEquals(200, response.getStatus());
    assertEquals(1, contactCards.size());
    assertTrue(contactCards.get(0).getFullName().contains("Foo"));
    assertEquals(200, target.request(MediaType.APPLICATION_JSON_TYPE).delete().getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ContactCard(org.glassfish.jersey.examples.beanvalidation.webapp.domain.ContactCard) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 20 with GenericType

use of javax.ws.rs.core.GenericType in project jersey by jersey.

the class AsyncContentAndEntityTypeTest method testAsyncContentType.

@Test
public void testAsyncContentType() throws Exception {
    final ApplicationHandler app = createApplication(AsyncResource.class);
    MediaType foo = MediaType.valueOf("application/foo");
    Future<ContainerResponse> responseFuture = Executors.newFixedThreadPool(1).submit(new Callable<ContainerResponse>() {

        @Override
        public ContainerResponse call() throws Exception {
            return app.apply(RequestContextBuilder.from("/", "GET").accept("*/*").build()).get();
        }
    });
    ContainerResponse response;
    // making sure the JVM optimization does not swap the order of the calls.
    synchronized (this) {
        app.apply(RequestContextBuilder.from("/", "POST").entity("Foo").build());
        response = responseFuture.get();
    }
    assertTrue("Status: " + response.getStatus(), response.getStatus() < 300);
    assertEquals("Foo", response.getEntity());
    assertEquals(foo, response.getMediaType());
    final GenericType stringType = new GenericType(String.class);
    assertEquals(stringType.getRawType(), response.getEntityClass());
    assertEquals(stringType.getType(), response.getEntityType());
}
Also used : GenericType(javax.ws.rs.core.GenericType) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Aggregations

GenericType (javax.ws.rs.core.GenericType)52 WebTarget (javax.ws.rs.client.WebTarget)38 Test (org.junit.Test)26 List (java.util.List)17 GenericEntity (javax.ws.rs.core.GenericEntity)13 Response (javax.ws.rs.core.Response)13 Collection (java.util.Collection)12 JerseyTest (org.glassfish.jersey.test.JerseyTest)9 ArrayList (java.util.ArrayList)8 Queue (java.util.Queue)8 GET (javax.ws.rs.GET)7 Produces (javax.ws.rs.Produces)7 MediaType (javax.ws.rs.core.MediaType)7 Type (java.lang.reflect.Type)6 HashSet (java.util.HashSet)6 Path (javax.ws.rs.Path)6 Comparator (java.util.Comparator)5 Set (java.util.Set)5 Stack (java.util.Stack)5 TreeSet (java.util.TreeSet)5