Search in sources :

Example 96 with Client

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

the class ConstrainedToTest method testClientWithProviderInstances.

@Test
public void testClientWithProviderInstances() {
    Client client = ClientBuilder.newClient(new ClientConfig(new ClientFilterConstrainedToServer(), new ClientFilterConstrainedToClient(), new ClientFilter()));
    _testFilters(client);
}
Also used : Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 97 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>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    Client client = ClientBuilder.newClient();
    client.register(ClientLoggingFilter.class).register(MyClientReaderInterceptor.class).register(MyClientWriterInterceptor.class);
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/fruits");
    out.println(target.getUri() + "<br><br>");
    out.println("GET request" + "<br><br>");
    String result = target.request().get(String.class);
    out.println("Received response (GET): " + result + "<br><br>");
    System.out.println("**** POST request");
    result = target.request().post(Entity.text("1"), String.class);
    out.println("Received response (POST): " + result + "<br><br>");
    out.println("Received response: " + result + "<br><br>");
    out.println("<br><br>Check server log for client/server filter output, prints headers and entity body.");
    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 98 with Client

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

the class MyResourceTest method setUp.

@Before
public void setUp() throws Exception {
    // start the server
    server = Main.startServer();
    // create the client
    Client c = ClientBuilder.newClient();
    // uncomment the following line if you want to enable
    // support for JSON in the client (you also have to uncomment
    // dependency on jersey-media-json module in pom.xml and Main.startServer())
    // --
    // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
    target = c.target(Main.BASE_URI);
}
Also used : Client(javax.ws.rs.client.Client) Before(org.junit.Before)

Example 99 with Client

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

the class AuthTest method testAuthPostWithClientFilter.

@Test
public void testAuthPostWithClientFilter() {
    ClientConfig cc = new ClientConfig();
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    client.register(HttpAuthenticationFeature.basic("name", "password"));
    WebTarget r = client.target(getBaseUri()).path("test/filter");
    assertEquals("POST", r.request().post(Entity.text("POST"), String.class));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 100 with Client

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

the class HelloWorldTest method testLoggingFilterClientClass.

@Test
public void testLoggingFilterClientClass() {
    Client client = client();
    client.register(CustomLoggingFilter.class).property("foo", "bar");
    CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
    String s = target().path(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)

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