Search in sources :

Example 26 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 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 27 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/persons").toExternalForm()));
    target.register(Person.class);
}
Also used : Client(javax.ws.rs.client.Client) URL(java.net.URL) Before(org.junit.Before)

Example 28 with Client

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

the class MyResourceTest method setupClass.

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

Example 29 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>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 30 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/persons").toExternalForm()));
}
Also used : Client(javax.ws.rs.client.Client) URL(java.net.URL) Before(org.junit.Before)

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