Search in sources :

Example 61 with Client

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

the class OauthClientAuthorizationFlowTest method testOAuthClientFlow.

@Test
public void testOAuthClientFlow() throws Exception {
    final String uri = getBaseUri().toString();
    final OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(new ConsumerCredentials("dpf43f3p2l4k3l03", "kd94hf93k423kf44")).timestamp("1191242090").nonce("hsu94j3884jdopsl").signatureMethod("PLAINTEXT").authorizationFlow(uri + "request_token", uri + "access_token", uri + "authorize").enableLogging().build();
    // Check we have correct authorization URI.
    final String authorizationUri = authFlow.start();
    assertThat(authorizationUri, containsString("authorize?oauth_token=hh5s93j4hdidpola"));
    // For the purpose of the test I need parameters (and there is no way how to do it now).
    final Field paramField = authFlow.getClass().getDeclaredField("parameters");
    paramField.setAccessible(true);
    final OAuth1Parameters params = (OAuth1Parameters) paramField.get(authFlow);
    // Update parameters.
    params.timestamp("1191242092").nonce("dji430splmx33448");
    final AccessToken accessToken = authFlow.finish();
    assertThat(accessToken, equalTo(new AccessToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")));
    // Update parameters before creating a feature (i.e. changing signature method).
    params.nonce("kllo9940pd9333jh").signatureMethod("HMAC-SHA1").timestamp("1191242096");
    // Check Authorized Client.
    final Client flowClient = authFlow.getAuthorizedClient().register(LoggingFeature.class);
    String responseEntity = flowClient.target(uri).path("/photos").queryParam("file", "vacation.jpg").queryParam("size", "original").request().get(String.class);
    assertThat("Flow Authorized Client", responseEntity, equalTo("PHOTO"));
    // Check Feature.
    final Client featureClient = ClientBuilder.newClient().register(authFlow.getOAuth1Feature()).register(LoggingFeature.class);
    responseEntity = featureClient.target(uri).path("/photos").queryParam("file", "vacation.jpg").queryParam("size", "original").request().get(String.class);
    assertThat("Feature Client", responseEntity, equalTo("PHOTO"));
}
Also used : OAuth1AuthorizationFlow(org.glassfish.jersey.client.oauth1.OAuth1AuthorizationFlow) Field(java.lang.reflect.Field) OAuth1Parameters(org.glassfish.jersey.oauth1.signature.OAuth1Parameters) ConsumerCredentials(org.glassfish.jersey.client.oauth1.ConsumerCredentials) AccessToken(org.glassfish.jersey.client.oauth1.AccessToken) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 62 with Client

use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.

the class FilterServletTest method filtered_servlet_should_return_enhanced_foobar_text.

@Test
@RunAsClient
public void filtered_servlet_should_return_enhanced_foobar_text() throws MalformedURLException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(URI.create(new URL(base, "filtered/TestServlet").toExternalForm()));
    Response response = target.request().get();
    Assert.assertThat(response.readEntity(String.class), is(equalTo("foo--bar--bar")));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) URL(java.net.URL) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Test(org.junit.Test)

Example 63 with Client

use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.

the class FilterServletTest method standard_servlet_should_return_simple_text.

@Test
@RunAsClient
public void standard_servlet_should_return_simple_text() throws MalformedURLException {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(URI.create(new URL(base, "TestServlet").toExternalForm()));
    Response response = target.request().get();
    Assert.assertThat(response.readEntity(String.class), is(equalTo("bar")));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) URL(java.net.URL) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Test(org.junit.Test)

Example 64 with Client

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

the class UnderlyingCookieStoreAccessTest method testCookieStoreInstanceAccess.

@Test
public void testCookieStoreInstanceAccess() {
    final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new ApacheConnectorProvider()));
    final CookieStore csOnClient = ApacheConnectorProvider.getCookieStore(client);
    // important: the web target instance in this test must be only created AFTER the client has been pre-initialized
    // (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
    // connector provider's static getCookieStore method above.
    final WebTarget target = client.target("http://localhost/");
    final CookieStore csOnTarget = ApacheConnectorProvider.getCookieStore(target);
    assertNotNull("CookieStore instance set on JerseyClient should not be null.", csOnClient);
    assertNotNull("CookieStore instance set on JerseyWebTarget should not be null.", csOnTarget);
    assertSame("CookieStore instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", csOnClient, csOnTarget);
}
Also used : CookieStore(org.apache.http.client.CookieStore) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 65 with Client

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

the class RetryHandlerTest method testRetryGet.

@Test
public void testRetryGet() throws IOException {
    ClientConfig cc = new ClientConfig();
    cc.connectorProvider(new ApacheConnectorProvider());
    cc.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (exception, executionCount, context) -> true);
    cc.property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MS);
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    assertEquals("GET", r.request().get(String.class));
}
Also used : POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) GET(javax.ws.rs.GET) ClientConfig(org.glassfish.jersey.client.ClientConfig) Path(javax.ws.rs.Path) Client(javax.ws.rs.client.Client) IOException(java.io.IOException) Test(org.junit.Test) Application(javax.ws.rs.core.Application) ClientProperties(org.glassfish.jersey.client.ClientProperties) Entity(javax.ws.rs.client.Entity) ClientBuilder(javax.ws.rs.client.ClientBuilder) JerseyTest(org.glassfish.jersey.test.JerseyTest) HttpHeaders(javax.ws.rs.core.HttpHeaders) RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) WebTarget(javax.ws.rs.client.WebTarget) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) Assert.assertEquals(org.junit.Assert.assertEquals) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

Client (javax.ws.rs.client.Client)279 Test (org.junit.Test)192 WebTarget (javax.ws.rs.client.WebTarget)120 Response (javax.ws.rs.core.Response)107 ClientConfig (org.glassfish.jersey.client.ClientConfig)77 JerseyTest (org.glassfish.jersey.test.JerseyTest)76 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)21 URL (java.net.URL)20 ClientResponse (org.glassfish.jersey.client.ClientResponse)19 Invocation (javax.ws.rs.client.Invocation)18 ClientBuilder (javax.ws.rs.client.ClientBuilder)17 Before (org.junit.Before)17 IOException (java.io.IOException)15 ProcessingException (javax.ws.rs.ProcessingException)15 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 URI (java.net.URI)12 WebClient (org.apache.cxf.jaxrs.client.WebClient)11 HttpServer (org.glassfish.grizzly.http.server.HttpServer)10 PrintWriter (java.io.PrintWriter)9 SSLContext (javax.net.ssl.SSLContext)9