use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class BasicClientTest method testAbortAsyncRequest.
@Test
public // JERSEY-1412
void testAbortAsyncRequest() throws Exception {
Invocation invocation = abortingTarget().request().buildPost(text("entity"));
Future<String> future = invocation.submit(String.class);
assertEquals("aborted", future.get());
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class ClientInvocationTest method testMultipleCallbackInvocationSubmitsWithEntity.
@Test
public void testMultipleCallbackInvocationSubmitsWithEntity() throws Exception {
final Invocation invocation = target().request().buildPost(Entity.text("OK"));
for (int i = 0; i < INVOCATIONS; i++) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> response = new AtomicReference<>();
invocation.submit(new InvocationCallback<String>() {
@Override
public void completed(final String s) {
response.set(s);
latch.countDown();
}
@Override
public void failed(final Throwable throwable) {
response.set(throwable.getMessage());
latch.countDown();
}
});
latch.await(5, TimeUnit.SECONDS);
assertThat(response.get(), is("OK"));
}
}
use of javax.ws.rs.client.Invocation 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 Async API</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>JAX-RS 2 Client Invocation Async 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>");
Future<Response> f1 = i1.submit();
Future<String> f2 = i2.submit(String.class);
try {
Response r1 = f1.get();
out.println("Response from r1: " + r1.readEntity(String.class) + "<br>");
String r2 = f2.get();
out.println("Response from r2: " + r2 + "<br>");
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
}
out.println("... done.<br>");
out.println("</body>");
out.println("</html>");
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class JerseyInvocationTest method testNullResponseType.
@Test
public void testNullResponseType() throws Exception {
final Client client = ClientBuilder.newClient();
client.register(new ClientRequestFilter() {
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.ok().build());
}
});
final WebTarget target = client.target("http://localhost:8080/mypath");
final Class<Response> responseType = null;
final String[] methods = new String[] { "GET", "PUT", "POST", "DELETE", "OPTIONS" };
for (final String method : methods) {
final Invocation.Builder request = target.request();
try {
request.method(method, responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
final Invocation build = "PUT".equals(method) ? request.build(method, Entity.entity("", MediaType.TEXT_PLAIN_TYPE)) : request.build(method);
try {
build.submit(responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
try {
build.invoke(responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
try {
request.async().method(method, responseType);
fail("IllegalArgumentException expected.");
} catch (final IllegalArgumentException iae) {
// OK.
}
}
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class JerseyInvocationTest method failedCallbackTest.
@Test
public void failedCallbackTest() throws InterruptedException {
final Invocation.Builder builder = ClientBuilder.newClient().target("http://localhost:888/").request();
for (int i = 0; i < 1; i++) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger ai = new AtomicInteger(0);
final InvocationCallback<String> callback = new InvocationCallback<String>() {
@Override
public void completed(final String arg0) {
try {
ai.set(ai.get() + 1);
} finally {
latch.countDown();
}
}
@Override
public void failed(final Throwable throwable) {
try {
int result = 10;
if (throwable instanceof ProcessingException) {
result += 100;
}
final Throwable ioe = throwable.getCause();
if (ioe instanceof IOException) {
result += 1000;
}
ai.set(ai.get() + result);
} finally {
latch.countDown();
}
}
};
final Invocation invocation = builder.buildGet();
final Future<String> future = invocation.submit(callback);
try {
future.get();
fail("future.get() should have failed.");
} catch (final ExecutionException e) {
final Throwable pe = e.getCause();
assertTrue("Execution exception cause is not a ProcessingException: " + pe.toString(), pe instanceof ProcessingException);
final Throwable ioe = pe.getCause();
assertTrue("Execution exception cause is not an IOException: " + ioe.toString(), ioe instanceof IOException);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
latch.await(1, TimeUnit.SECONDS);
assertEquals(1110, ai.get());
}
}
Aggregations