Search in sources :

Example 26 with Response

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

the class TerminalClientRequestFilter method filter.

@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
    // Obtain entity - from request or create new.
    final ByteArrayInputStream entity = new ByteArrayInputStream(requestContext.hasEntity() ? requestContext.getEntity().toString().getBytes() : "NO-ENTITY".getBytes());
    final int responseStatus = requestContext.getHeaders().getFirst("Response-Status") != null ? (int) requestContext.getHeaders().getFirst("Response-Status") : 200;
    Response.ResponseBuilder response = Response.status(responseStatus).entity(entity).type("text/plain").header("Test-Thread", Thread.currentThread().getName()).header("Test-Uri", requestContext.getUri().toString()).header("Test-Method", requestContext.getMethod());
    // Request headers -> Response headers (<header> -> Test-Header-<header>)
    for (final MultivaluedMap.Entry<String, List<String>> entry : requestContext.getStringHeaders().entrySet()) {
        response = response.header("Test-Header-" + entry.getKey(), entry.getValue());
    }
    // Request properties -> Response headers (<header> -> Test-Property-<header>)
    for (final String property : requestContext.getPropertyNames()) {
        response = response.header("Test-Property-" + property, requestContext.getProperty(property));
    }
    requestContext.abortWith(response.build());
}
Also used : Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 27 with Response

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

the class ModelEntityOnListTest method myBeanAndPut.

@Test
public void myBeanAndPut() {
    WebTarget target = target("empty/mybean");
    MyBean mb = new MyBean();
    mb.setValue("Hello");
    MyBean ah = new MyBean();
    ah.setValue("Ahoy");
    GenericEntity<List<MyBean>> ge = new GenericEntity<List<MyBean>>(Arrays.asList(mb, ah)) {
    };
    final Response response = target.request().post(Entity.entity(ge, MediaType.APPLICATION_JSON_TYPE));
    assertEquals(200, response.getStatus());
    assertEquals("PASSED", response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) GenericEntity(javax.ws.rs.core.GenericEntity) ArrayList(java.util.ArrayList) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 28 with Response

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

the class EventListenerTest method testSimpleProcessing.

@Test
public void testSimpleProcessing() {
    final Response response = target().path("resource").request().get();
    assertEquals(200, response.getStatus());
    assertEquals("get", response.readEntity(String.class));
    int i = 1;
    System.out.println(response.getHeaders());
    assertEquals("[" + i++ + "]", response.getHeaderString("R.REQ_FILTERS_START.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.REQ_FILTERS_FINISHED.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESOURCE_METHOD_START.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESOURCE_METHOD_FINISHED.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESP_FILTERS_START.order"));
    assertEquals("[" + i + "]", response.getHeaderString("R.RESP_FILTERS_FINISHED.order"));
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 29 with Response

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

the class EventListenerTest method testException.

@Test
public void testException() {
    final Response response = target().path("resource/locator/exception").request().get();
    assertEquals(200, response.getStatus());
    assertEquals("mapped", response.readEntity(String.class));
    assertEquals("[org.glassfish.jersey.tests.e2e.server.monitoring.EventListenerTest$MyMappableException: test-error]", response.getHeaderString("R.EXCEPTION_MAPPER_FOUND.exception"));
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 30 with Response

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

the class EventListenerTest method testLocatorProcessing.

@Test
public void testLocatorProcessing() {
    final Response response = target().path("resource/locator").request().get();
    assertEquals(200, response.getStatus());
    assertEquals("sub", response.readEntity(String.class));
    int i = 1;
    System.out.println(response.getHeaders());
    assertEquals("[" + i++ + "]", response.getHeaderString("R.MATCHED_LOCATOR.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.MATCHED_SUB_RESOURCE.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.REQ_FILTERS_START.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.REQ_FILTERS_FINISHED.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESOURCE_METHOD_START.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESOURCE_METHOD_FINISHED.order"));
    assertEquals("[" + i++ + "]", response.getHeaderString("R.RESP_FILTERS_START.order"));
    assertEquals("[" + i + "]", response.getHeaderString("R.RESP_FILTERS_FINISHED.order"));
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

Response (javax.ws.rs.core.Response)10205 Test (org.junit.Test)5764 Test (org.testng.annotations.Test)1113 JerseyTest (org.glassfish.jersey.test.JerseyTest)870 Test (org.junit.jupiter.api.Test)701 WebTarget (javax.ws.rs.client.WebTarget)668 Builder (javax.ws.rs.client.Invocation.Builder)638 WebClient (org.apache.cxf.jaxrs.client.WebClient)620 DBUnitTest (org.orcid.test.DBUnitTest)611 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)577 Parameters (org.testng.annotations.Parameters)537 URI (java.net.URI)515 HashMap (java.util.HashMap)508 Path (javax.ws.rs.Path)506 List (java.util.List)465 ArrayList (java.util.ArrayList)419 Produces (javax.ws.rs.Produces)396 IOException (java.io.IOException)393 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)354 GET (javax.ws.rs.GET)348