Search in sources :

Example 36 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class JdkHttpsServerTest method testHttpsServerNoSslContextDelayedStart.

/**
     * Test, that {@link HttpsServer} can be manually started even with (empty) SSLContext, but will throw an exception
     * on request.
     * @throws Exception
     */
@Test(expected = IOException.class)
public void testHttpsServerNoSslContextDelayedStart() throws Throwable {
    server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
    assertThat(server, instanceOf(HttpsServer.class));
    server.start();
    final Client client = ClientBuilder.newBuilder().newClient();
    try {
        client.target(httpsUri).path("testHttps").request().get(String.class);
    } catch (final ProcessingException e) {
        throw e.getCause();
    }
}
Also used : Client(javax.ws.rs.client.Client) HttpsServer(com.sun.net.httpserver.HttpsServer) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 37 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class HelloWorldTest method testConfigurationUpdate.

@Test
public void testConfigurationUpdate() {
    Client client1 = client();
    client1.register(CustomLoggingFilter.class).property("foo", "bar");
    Client client = ClientBuilder.newClient(client1.getConfiguration());
    CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
    String s = target().path(ROOT_PATH).request().get(String.class);
    assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
    assertEquals(1, CustomLoggingFilter.preFilterCalled);
    assertEquals(1, CustomLoggingFilter.postFilterCalled);
    client.close();
}
Also used : Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 38 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class ExceptionTest method test500StatusCode.

@Test
public void test500StatusCode() {
    startServer(ExceptionResource.class);
    Client client = ClientBuilder.newClient();
    WebTarget r = client.target(getUri().path("500").build());
    assertEquals(500, r.request().get(Response.class).getStatus());
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 39 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class ExceptionTest method test400StatusCode.

@Test
public void test400StatusCode() {
    startServer(ExceptionResource.class);
    Client client = ClientBuilder.newClient();
    WebTarget r = client.target(getUri().path("400").build());
    assertEquals(400, r.request().get(Response.class).getStatus());
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 40 with Client

use of javax.ws.rs.client.Client in project jersey by jersey.

the class HttpAuthenticationFilter method repeatRequest.

/**
     * Repeat the {@code request} with provided {@code newAuthorizationHeader}
     * and update the {@code response} with newest response data.
     *
     * @param request                Request context.
     * @param response               Response context (will be updated with the new response data).
     * @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
     * @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
     * {@code false} otherwise).
     */
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
    Client client = request.getClient();
    String method = request.getMethod();
    MediaType mediaType = request.getMediaType();
    URI lUri = request.getUri();
    WebTarget resourceTarget = client.target(lUri);
    Invocation.Builder builder = resourceTarget.request(mediaType);
    MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
    for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
        if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
            continue;
        }
        newHeaders.put(entry.getKey(), entry.getValue());
    }
    newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
    builder.headers(newHeaders);
    builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
    Invocation invocation;
    if (request.getEntity() == null) {
        invocation = builder.build(method);
    } else {
        invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
    }
    Response nextResponse = invocation.invoke();
    if (nextResponse.hasEntity()) {
        response.setEntityStream(nextResponse.readEntity(InputStream.class));
    }
    MultivaluedMap<String, String> headers = response.getHeaders();
    headers.clear();
    headers.putAll(nextResponse.getStringHeaders());
    response.setStatus(nextResponse.getStatus());
    return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
Also used : Invocation(javax.ws.rs.client.Invocation) InputStream(java.io.InputStream) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

Client (javax.ws.rs.client.Client)227 Test (org.junit.Test)160 WebTarget (javax.ws.rs.client.WebTarget)96 Response (javax.ws.rs.core.Response)87 JerseyTest (org.glassfish.jersey.test.JerseyTest)76 ClientConfig (org.glassfish.jersey.client.ClientConfig)71 URL (java.net.URL)20 ClientResponse (org.glassfish.jersey.client.ClientResponse)19 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)18 Before (org.junit.Before)17 Invocation (javax.ws.rs.client.Invocation)15 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 IOException (java.io.IOException)12 ProcessingException (javax.ws.rs.ProcessingException)12 HttpServer (org.glassfish.grizzly.http.server.HttpServer)10 URI (java.net.URI)9 JerseyClient (org.glassfish.jersey.client.JerseyClient)9 PrintWriter (java.io.PrintWriter)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 SSLContext (javax.net.ssl.SSLContext)8