Search in sources :

Example 6 with WebTarget

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

the class MyResourceTest method testRequestWithAllParams.

@Test
public void testRequestWithAllParams() {
    WebTarget t = target.path("/123").path("/abc").queryParam("param1", "foo").queryParam("param2", "bar").queryParam("param3", "baz");
    String r = t.request().get(String.class);
    assertEquals("/123/abc?param1=foo&param2=bar&param3=baz", r);
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 7 with WebTarget

use of javax.ws.rs.client.WebTarget 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 2 Client Invocation API</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>JAX-RS 2 Client Invocation API at " + request.getContextPath() + "</h1>");
    out.println("Initializing client...<br>");
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/resource");
    // GET
    out.print("Building a GET request ...<br>");
    Invocation i1 = target.request().buildGet();
    out.print("GET request ready ...<br>");
    // POST
    out.print("Building a POST request...<br>");
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.add("name", "Name");
    map.add("age", "17");
    Invocation i2 = target.request().buildPost(Entity.form(map));
    out.print("POSTed request ready...<br>");
    Collection<Invocation> is = Arrays.asList(i1, i2);
    for (Invocation i : is) {
        i.invoke();
        System.out.println("Request invoked");
    }
    out.println("... done.<br>");
    out.println("</body>");
    out.println("</html>");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Invocation(javax.ws.rs.client.Invocation) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) PrintWriter(java.io.PrintWriter)

Example 8 with WebTarget

use of javax.ws.rs.client.WebTarget 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>Request Binding</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Request Binding</h1>");
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/persons");
    out.print("GETTing @CookieParam ...<br>");
    out.print(target.request().get(String.class));
    out.print("<br><br>GETTing @MatrixParam ...<br>");
    out.print(target.path("matrix").matrixParam("start", "5").matrixParam("end", "10").request().get(String.class));
    out.print("<br><br>GETTing @Context ...<br>");
    out.print(target.path("context").request().get(String.class));
    out.println("<br>... done.<br>");
    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 9 with WebTarget

use of javax.ws.rs.client.WebTarget 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 10 with WebTarget

use of javax.ws.rs.client.WebTarget 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));
    WebTarget r = ClientBuilder.newClient().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) Test(org.junit.Test)

Aggregations

WebTarget (javax.ws.rs.client.WebTarget)591 Test (org.junit.Test)489 JerseyTest (org.glassfish.jersey.test.JerseyTest)297 Response (javax.ws.rs.core.Response)278 Client (javax.ws.rs.client.Client)98 ClientConfig (org.glassfish.jersey.client.ClientConfig)45 GenericType (javax.ws.rs.core.GenericType)35 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 List (java.util.List)23 InputStream (java.io.InputStream)22 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)21 ProcessingException (javax.ws.rs.ProcessingException)19 URI (java.net.URI)18 Properties (java.util.Properties)18 TestProperties (org.glassfish.jersey.test.TestProperties)18 MediaType (javax.ws.rs.core.MediaType)15 Invocation (javax.ws.rs.client.Invocation)13 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)13