Search in sources :

Example 76 with Client

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

the class MainTest method _testWithoutSSLAuthentication.

/**
     * Test to see that SSLHandshakeException is thrown when client don't have
     * trusted key.
     */
private void _testWithoutSSLAuthentication(ClientConfig clientConfig) {
    SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD);
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
    System.out.println("Client: GET " + Server.BASE_URI);
    WebTarget target = client.target(Server.BASE_URI);
    target.register(LoggingFeature.class);
    boolean caught = false;
    try {
        target.path("/").request().get(String.class);
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
// solaris throws java.net.SocketException instead of SSLHandshakeException
// assertTrue(msg.contains("SSLHandshakeException"));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 77 with Client

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

the class App method main.

/**
     * Main method that creates a {@link Client client} and initializes the OAuth support with
     * configuration needed to connect to the Twitter and retrieve statuses.
     * <p/>
     * Execute this method to demo
     *
     * @param args Command line arguments.
     * @throws Exception Thrown when error occurs.
     */
public static void main(final String[] args) throws Exception {
    // retrieve consumer key/secret and token/secret from the property file
    // or system properties
    loadSettings();
    final ConsumerCredentials consumerCredentials = new ConsumerCredentials(PROPERTIES.getProperty(PROPERTY_CONSUMER_KEY), PROPERTIES.getProperty(PROPERTY_CONSUMER_SECRET));
    final Feature filterFeature;
    if (PROPERTIES.getProperty(PROPERTY_TOKEN) == null) {
        // we do not have Access Token yet. Let's perfom the Authorization Flow first,
        // let the user approve our app and get Access Token.
        final OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(consumerCredentials).authorizationFlow("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize").build();
        final String authorizationUri = authFlow.start();
        System.out.println("Enter the following URI into a web browser and authorize me:");
        System.out.println(authorizationUri);
        System.out.print("Enter the authorization code: ");
        final String verifier;
        try {
            verifier = IN.readLine();
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
        final AccessToken accessToken = authFlow.finish(verifier);
        // store access token for next application execution
        PROPERTIES.setProperty(PROPERTY_TOKEN, accessToken.getToken());
        PROPERTIES.setProperty(PROPERTY_TOKEN_SECRET, accessToken.getAccessTokenSecret());
        // get the feature that will configure the client with consumer credentials and
        // received access token
        filterFeature = authFlow.getOAuth1Feature();
    } else {
        final AccessToken storedToken = new AccessToken(PROPERTIES.getProperty(PROPERTY_TOKEN), PROPERTIES.getProperty(PROPERTY_TOKEN_SECRET));
        // build a new feature from the stored consumer credentials and access token
        filterFeature = OAuth1ClientSupport.builder(consumerCredentials).feature().accessToken(storedToken).build();
    }
    // create a new Jersey client and register filter feature that will add OAuth signatures and
    // JacksonFeature that will process returned JSON data.
    final Client client = ClientBuilder.newBuilder().register(filterFeature).register(JacksonFeature.class).build();
    // make requests to protected resources
    // (no need to care about the OAuth signatures)
    final Response response = client.target(FRIENDS_TIMELINE_URI).request().get();
    if (response.getStatus() != 200) {
        String errorEntity = null;
        if (response.hasEntity()) {
            errorEntity = response.readEntity(String.class);
        }
        throw new RuntimeException("Request to Twitter was not successful. Response code: " + response.getStatus() + ", reason: " + response.getStatusInfo().getReasonPhrase() + ", entity: " + errorEntity);
    }
    // persist the current consumer key/secret and token/secret for future use
    storeSettings();
    final List<Status> statuses = response.readEntity(new GenericType<List<Status>>() {
    });
    System.out.println("Tweets:\n");
    for (final Status s : statuses) {
        System.out.println(s.getText());
        System.out.println("[posted by " + s.getUser().getName() + " at " + s.getCreatedAt() + "]");
    }
}
Also used : OAuth1AuthorizationFlow(org.glassfish.jersey.client.oauth1.OAuth1AuthorizationFlow) ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) IOException(java.io.IOException) Feature(javax.ws.rs.core.Feature) JacksonFeature(org.glassfish.jersey.jackson.JacksonFeature) Response(javax.ws.rs.core.Response) JacksonFeature(org.glassfish.jersey.jackson.JacksonFeature) AccessToken(org.glassfish.jersey.client.oauth1.AccessToken) List(java.util.List) Client(javax.ws.rs.client.Client)

Example 78 with Client

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

the class HelloWorldTest method testLoggingFilterClientInstance.

@Test
@RunSeparately
public void testLoggingFilterClientInstance() {
    Client client = client();
    client.register(new CustomLoggingFilter()).property("foo", "bar");
    CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
    String s = target().path(App.ROOT_PATH).request().get(String.class);
    assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
    assertEquals(1, CustomLoggingFilter.preFilterCalled);
    assertEquals(1, CustomLoggingFilter.postFilterCalled);
}
Also used : Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest) RunSeparately(org.glassfish.jersey.test.util.runner.RunSeparately)

Example 79 with Client

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

the class HelloWorldTest method testConfigurationUpdate.

@Test
@RunSeparately
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(App.ROOT_PATH).request().get(String.class);
    assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
    assertEquals(1, CustomLoggingFilter.preFilterCalled);
    assertEquals(1, CustomLoggingFilter.postFilterCalled);
}
Also used : Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest) RunSeparately(org.glassfish.jersey.test.util.runner.RunSeparately)

Example 80 with Client

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

the class BasicClientTest method testCustomExecutorsSync.

@Test
public void testCustomExecutorsSync() throws ExecutionException, InterruptedException {
    ClientConfig jerseyConfig = new ClientConfig();
    jerseyConfig.register(CustomExecutorProvider.class).register(ThreadInterceptor.class);
    Client client = ClientBuilder.newClient(jerseyConfig);
    runCustomExecutorTestSync(client);
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

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