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 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>");
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class HttpAuthenticationFilter method repeatRequest.
/**
* Repeat the {@code request} with provided {@code newAuthorizationHeader}
* and update the {@code response} with newest response data.
*
* @param request Request context.
* @param response Response context (will be updated with the new response data).
* @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
* @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
* {@code false} otherwise).
*/
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
Client client = request.getClient();
String method = request.getMethod();
MediaType mediaType = request.getMediaType();
URI lUri = request.getUri();
WebTarget resourceTarget = client.target(lUri);
Invocation.Builder builder = resourceTarget.request(mediaType);
MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
continue;
}
newHeaders.put(entry.getKey(), entry.getValue());
}
newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
builder.headers(newHeaders);
builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
Invocation invocation;
if (request.getEntity() == null) {
invocation = builder.build(method);
} else {
invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
}
Response nextResponse = invocation.invoke();
if (nextResponse.hasEntity()) {
response.setEntityStream(nextResponse.readEntity(InputStream.class));
}
MultivaluedMap<String, String> headers = response.getHeaders();
headers.clear();
headers.putAll(nextResponse.getStringHeaders());
response.setStatus(nextResponse.getStatus());
return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class JerseyInvocationTest method testClearHeader.
/**
* Regression test for JERSEY-2562.
*/
@Test
public void testClearHeader() {
final Client client = ClientBuilder.newClient();
final Invocation.Builder builder = client.target("http://localhost:8080/mypath").request();
final JerseyInvocation invocation = (JerseyInvocation) builder.header("foo", "bar").header("foo", null).header("bar", "foo").buildGet();
final MultivaluedMap<String, Object> headers = invocation.request().getHeaders();
assertThat(headers.size(), is(1));
assertThat(headers.keySet(), hasItem("bar"));
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class JerseyInvocationTest method failedUnboundGenericCallback.
@Test
public void failedUnboundGenericCallback() throws InterruptedException {
final Invocation invocation = ClientBuilder.newClient().target("http://localhost:888/").request().buildGet();
final CountDownLatch latch = new CountDownLatch(1);
final MyUnboundCallback<String> callback = new MyUnboundCallback<String>(latch);
invocation.submit(callback);
latch.await(1, TimeUnit.SECONDS);
assertThat(callback.getThrowable(), CoreMatchers.instanceOf(ProcessingException.class));
assertThat(callback.getThrowable().getCause(), CoreMatchers.instanceOf(IllegalArgumentException.class));
assertThat(callback.getThrowable().getCause().getMessage(), CoreMatchers.allOf(CoreMatchers.containsString(MyUnboundCallback.class.getName()), CoreMatchers.containsString(InvocationCallback.class.getName())));
}
use of javax.ws.rs.client.Invocation in project jersey by jersey.
the class ClientTest method testContextHeaders.
@Test
public // Inspired by JERSEY-1502
void testContextHeaders() {
final WebTarget target = target().path("headers").path("content");
Invocation.Builder ib;
Invocation i;
Response r;
String reqHeaders;
ib = target.request("*/*");
ib.header("custom-header", "custom-value");
ib.header("content-encoding", "deflate");
i = ib.build("POST", Entity.entity("aaa", MediaType.WILDCARD_TYPE));
r = i.invoke();
reqHeaders = r.readEntity(String.class).toLowerCase();
for (final String expected : new String[] { "custom-header:[custom-value]", "custom-header:custom-value" }) {
assertTrue(String.format("Request headers do not contain expected '%s' entry:\n%s", expected, reqHeaders), reqHeaders.contains(expected));
}
final String unexpected = "content-encoding";
assertFalse(String.format("Request headers contains unexpected '%s' entry:\n%s", unexpected, reqHeaders), reqHeaders.contains(unexpected));
ib = target.request("*/*");
i = ib.build("POST", Entity.entity("aaa", Variant.mediaTypes(MediaType.WILDCARD_TYPE).encodings("deflate").build().get(0)));
r = i.invoke();
final String expected = "content-encoding:[deflate]";
reqHeaders = r.readEntity(String.class).toLowerCase();
assertTrue(String.format("Request headers do not contain expected '%s' entry:\n%s", expected, reqHeaders), reqHeaders.contains(expected));
}
Aggregations