Search in sources :

Example 31 with Client

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

the class MyResourceTest method setUp.

@Before
public void setUp() throws MalformedURLException {
    Client client = ClientBuilder.newClient();
    target = client.target(URI.create(new URL(base, "webresources/order").toExternalForm()));
}
Also used : Client(javax.ws.rs.client.Client) URL(java.net.URL) Before(org.junit.Before)

Example 32 with Client

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

the class TestServlet method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>JAX-RS Reader/Writer w/ JSON</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>JAX-RS Reader/Writer w/ JSON</h1>");
    Client client = ClientBuilder.newClient();
    client.register(MyReader.class).register(MyWriter.class);
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/endpoint");
    out.println("POST request");
    MyObject mo = target.request().post(Entity.entity(new MyObject("Duke", 18), MediaType.APPLICATION_JSON), MyObject.class);
    out.println("Received response: " + mo.getName() + ", " + mo.getAge() + "<br><br>");
    out.println("Message exchanged using application/json type.");
    out.println("</body>");
    out.println("</html>");
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) PrintWriter(java.io.PrintWriter)

Example 33 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 34 with Client

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

the class LifecycleListenerTest method testReload.

@Test
public void testReload() {
    final ResourceConfig rc = new ResourceConfig(One.class);
    Reloader reloader = new Reloader();
    rc.registerInstances(reloader);
    startServer(rc);
    Client client = ClientBuilder.newClient();
    WebTarget r = client.target(getUri().path("/").build());
    assertEquals("one", r.path("one").request().get(String.class));
    assertEquals(404, r.path("two").request().get(Response.class).getStatus());
    // add Two resource
    reloader.reload(new ResourceConfig(One.class, Two.class));
    assertEquals("one", r.path("one").request().get(String.class));
    assertEquals("two", r.path("two").request().get(String.class));
}
Also used : ResourceConfig(org.glassfish.jersey.server.ResourceConfig) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 35 with Client

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

the class LifecycleListenerTest method testStartupShutdownHooks.

@Test
public void testStartupShutdownHooks() {
    final StartStopListener listener = new StartStopListener();
    startServer(new ResourceConfig(One.class).register(listener));
    Client client = ClientBuilder.newClient();
    WebTarget r = client.target(getUri().path("/").build());
    assertThat(r.path("one").request().get(String.class), equalTo("one"));
    assertThat(r.path("two").request().get(Response.class).getStatus(), equalTo(404));
    stopServer();
    assertTrue("ContainerLifecycleListener.onStartup has not been called.", listener.started);
    assertTrue("ContainerLifecycleListener.onShutdown has not been called.", listener.stopped);
}
Also used : ResourceConfig(org.glassfish.jersey.server.ResourceConfig) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) 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