Search in sources :

Example 66 with Client

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

the class AuthTest method testAuthDelete.

@Test
public void testAuthDelete() {
    ClientConfig config = new ClientConfig();
    config.property(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, new BasicAuthentication(getBaseUri(), "WallyWorld", "name", "password"));
    config.connectorProvider(new JettyConnectorProvider());
    Client client = ClientBuilder.newClient(config);
    Response response = client.target(getBaseUri()).path(PATH).request().delete();
    assertEquals(response.getStatus(), 204);
    client.close();
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 67 with Client

use of javax.ws.rs.client.Client in project eureka by Netflix.

the class Jersey2ReplicationClient method createReplicationClient.

public static Jersey2ReplicationClient createReplicationClient(EurekaServerConfig config, ServerCodecs serverCodecs, String serviceUrl) {
    String name = Jersey2ReplicationClient.class.getSimpleName() + ": " + serviceUrl + "apps/: ";
    EurekaJersey2Client jerseyClient;
    try {
        String hostname;
        try {
            hostname = new URL(serviceUrl).getHost();
        } catch (MalformedURLException e) {
            hostname = serviceUrl;
        }
        String jerseyClientName = "Discovery-PeerNodeClient-" + hostname;
        EurekaJersey2ClientImpl.EurekaJersey2ClientBuilder clientBuilder = new EurekaJersey2ClientImpl.EurekaJersey2ClientBuilder().withClientName(jerseyClientName).withUserAgent("Java-EurekaClient-Replication").withEncoderWrapper(serverCodecs.getFullJsonCodec()).withDecoderWrapper(serverCodecs.getFullJsonCodec()).withConnectionTimeout(config.getPeerNodeConnectTimeoutMs()).withReadTimeout(config.getPeerNodeReadTimeoutMs()).withMaxConnectionsPerHost(config.getPeerNodeTotalConnectionsPerHost()).withMaxTotalConnections(config.getPeerNodeTotalConnections()).withConnectionIdleTimeout(config.getPeerNodeConnectionIdleTimeoutSeconds());
        if (serviceUrl.startsWith("https://") && "true".equals(System.getProperty("com.netflix.eureka.shouldSSLConnectionsUseSystemSocketFactory"))) {
            clientBuilder.withSystemSSLConfiguration();
        }
        jerseyClient = clientBuilder.build();
    } catch (Throwable e) {
        throw new RuntimeException("Cannot Create new Replica Node :" + name, e);
    }
    String ip = null;
    try {
        ip = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        logger.warn("Cannot find localhost ip", e);
    }
    Client jerseyApacheClient = jerseyClient.getClient();
    jerseyApacheClient.register(new Jersey2DynamicGZIPContentEncodingFilter(config));
    EurekaServerIdentity identity = new EurekaServerIdentity(ip);
    jerseyApacheClient.register(new EurekaIdentityHeaderFilter(identity));
    return new Jersey2ReplicationClient(jerseyClient, serviceUrl);
}
Also used : MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) EurekaJersey2Client(com.netflix.discovery.shared.transport.jersey2.EurekaJersey2Client) URL(java.net.URL) EurekaIdentityHeaderFilter(com.netflix.discovery.shared.transport.jersey2.EurekaIdentityHeaderFilter) EurekaJersey2ClientImpl(com.netflix.discovery.shared.transport.jersey2.EurekaJersey2ClientImpl) EurekaServerIdentity(com.netflix.eureka.EurekaServerIdentity) AbstractJersey2EurekaHttpClient(com.netflix.discovery.shared.transport.jersey2.AbstractJersey2EurekaHttpClient) Client(javax.ws.rs.client.Client) EurekaJersey2Client(com.netflix.discovery.shared.transport.jersey2.EurekaJersey2Client) HttpReplicationClient(com.netflix.eureka.cluster.HttpReplicationClient)

Example 68 with Client

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

the class ExtendedWadlWebappOsgiTest method testWadlOptionsMethod.

@Test
public void testWadlOptionsMethod() throws Exception {
    // TODO - temporary workaround
    // This is a workaround related to issue JERSEY-2093; grizzly (1.9.5) needs to have the correct context
    // class loader set
    ClassLoader myClassLoader = this.getClass().getClassLoader();
    for (Bundle bundle : bundleContext.getBundles()) {
        if ("webapp".equals(bundle.getSymbolicName())) {
            myClassLoader = bundle.loadClass("org.glassfish.jersey.examples.extendedwadl.resources.MyApplication").getClassLoader();
            break;
        }
    }
    Thread.currentThread().setContextClassLoader(myClassLoader);
    // END of workaround; The entire block can be removed after grizzly is migrated to more recent version
    final ResourceConfig resourceConfig = createResourceConfig();
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    final Client client = ClientBuilder.newClient();
    String wadl = client.target(baseUri).path("items").queryParam(WadlUtils.DETAILED_WADL_QUERY_PARAM, "true").request(MediaTypes.WADL_TYPE).options(String.class);
    assertTrue("Generated wadl is of null length", !wadl.isEmpty());
    assertTrue("Generated wadl doesn't contain the expected text", wadl.contains("This is a paragraph"));
    checkWadl(wadl, baseUri);
    server.shutdownNow();
}
Also used : Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 69 with Client

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

the class ExtendedWadlWebappOsgiTest method testExtendedWadl.

/**
     * Test checks that the WADL generated using the WadlGenerator api doesn't
     * contain the expected text.
     *
     * @throws java.lang.Exception in case of a test error.
     */
@Test
public void testExtendedWadl() throws Exception {
    // TODO - temporary workaround
    // This is a workaround related to issue JERSEY-2093; grizzly (1.9.5) needs to have the correct context
    // class loader set
    ClassLoader myClassLoader = this.getClass().getClassLoader();
    for (Bundle bundle : bundleContext.getBundles()) {
        if ("webapp".equals(bundle.getSymbolicName())) {
            myClassLoader = bundle.loadClass("org.glassfish.jersey.examples.extendedwadl.resources.MyApplication").getClassLoader();
            break;
        }
    }
    Thread.currentThread().setContextClassLoader(myClassLoader);
    // END of workaround - the entire block can be deleted after grizzly is updated to recent version
    // List all the OSGi bundles
    StringBuilder sb = new StringBuilder();
    sb.append("-- Bundle list -- \n");
    for (Bundle b : bundleContext.getBundles()) {
        sb.append(String.format("%1$5s", "[" + b.getBundleId() + "]")).append(" ").append(String.format("%1$-70s", b.getSymbolicName())).append(" | ").append(String.format("%1$-20s", b.getVersion())).append(" |");
        try {
            b.start();
            sb.append(" STARTED  | ");
        } catch (BundleException e) {
            sb.append(" *FAILED* | ").append(e.getMessage());
        }
        sb.append(b.getLocation()).append("\n");
    }
    sb.append("-- \n\n");
    LOGGER.fine(sb.toString());
    final ResourceConfig resourceConfig = createResourceConfig();
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    final Client client = ClientBuilder.newClient();
    final Response response = client.target(baseUri).path("application.wadl").request(MediaTypes.WADL_TYPE).buildGet().invoke();
    String wadl = response.readEntity(String.class);
    LOGGER.info("RESULT = " + wadl);
    assertTrue("Generated wadl is of null length", !wadl.isEmpty());
    assertTrue("Generated wadl doesn't contain the expected text", wadl.contains("This is a paragraph"));
    assertFalse(wadl.contains("application.wadl/xsd0.xsd"));
    server.shutdownNow();
}
Also used : Response(javax.ws.rs.core.Response) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpServer(org.glassfish.grizzly.http.server.HttpServer) BundleException(org.osgi.framework.BundleException) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 70 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) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test) RunSeparately(org.glassfish.jersey.test.util.runner.RunSeparately)

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