use of javax.ws.rs.core.GenericType in project Payara by payara.
the class EjbHttpProxyHandler method doRemoteInvoke.
public Object doRemoteInvoke(Object proxy, Method method, Object[] argValues) throws Throwable {
// HTTP method name; we're always using POST to invoke remote EJBs
String httpMethod = POST;
// The bare payload being sent
Map<String, Object> payload = new HashMap<>();
payload.put("lookup", lookup);
payload.put("method", method.getName());
payload.put("argTypes", Arrays.stream(method.getParameterTypes()).map(Class::getName).toArray());
payload.put("argValues", argValues == null ? new Object[0] : argValues);
if (jndiOptions.containsKey(SECURITY_PRINCIPAL)) {
payload.put(SECURITY_PRINCIPAL, Lookup.base64Encode(jndiOptions.get(SECURITY_PRINCIPAL)));
}
if (jndiOptions.containsKey(SECURITY_CREDENTIALS)) {
payload.put(SECURITY_CREDENTIALS, Lookup.base64Encode(jndiOptions.get(SECURITY_CREDENTIALS)));
}
// Payload wrapped as entity so it'll be encoded in JSON
Entity<?> entity = Entity.entity(payload, APPLICATION_JSON);
// Response type
Class<?> responseType = method.getReturnType();
GenericType<?> responseGenericType = new GenericType<>(method.getGenericReturnType());
// Create a new UriBuilder appending the name from the method
WebTarget newTarget = addPathFromMethod(method, target);
// Start request
Invocation.Builder builder = newTarget.request();
// Set optional headers and cookies
builder.headers(new MultivaluedHashMap<String, Object>(this.headers));
for (Cookie cookie : new LinkedList<>(this.cookies)) {
builder = builder.cookie(cookie);
}
if (responseType.isAssignableFrom(CompletionStage.class)) {
// Reactive call - the actual response type is T from CompletionStage<T>
return builder.rx().method(httpMethod, entity, getResponseParameterizedType(method, responseGenericType));
} else if (responseType.isAssignableFrom(Future.class)) {
// Asynchronous call - the actual response type is T from Future<T>
return builder.async().method(httpMethod, entity, getResponseParameterizedType(method, responseGenericType));
}
// Synchronous call
return builder.method(httpMethod, entity, responseGenericType);
}
use of javax.ws.rs.core.GenericType in project jersey by jersey.
the class JerseyInvocation method submit.
/**
* Submit the request for an asynchronous invocation and register an
* {@link InvocationCallback} to process the future result of the invocation.
* <p>
* Response type in this case is taken from {@code responseType} param (if not {@code null}) rather
* than from {@code callback}. This allows to pass callbacks like {@code new InvocationCallback<>() {...}}.
* </p>
*
* @param <T> response type
* @param responseType response type that is used instead of obtaining types from {@code callback}.
* @param callback invocation callback for asynchronous processing of the
* request invocation result.
* @return future response object of the specified type as a result of the
* request invocation.
*/
public <T> Future<T> submit(final GenericType<T> responseType, final InvocationCallback<T> callback) {
final CompletableFuture<T> responseFuture = new CompletableFuture<>();
try {
final ReflectionHelper.DeclaringClassInterfacePair pair = ReflectionHelper.getClass(callback.getClass(), InvocationCallback.class);
final Type callbackParamType;
final Class<T> callbackParamClass;
if (responseType == null) {
// If we don't have response use callback to obtain param types.
final Type[] typeArguments = ReflectionHelper.getParameterizedTypeArguments(pair);
if (typeArguments == null || typeArguments.length == 0) {
callbackParamType = Object.class;
} else {
callbackParamType = typeArguments[0];
}
callbackParamClass = ReflectionHelper.erasure(callbackParamType);
} else {
callbackParamType = responseType.getType();
callbackParamClass = ReflectionHelper.erasure(responseType.getRawType());
}
final ResponseCallback responseCallback = new ResponseCallback() {
@Override
public void completed(final ClientResponse response, final RequestScope scope) {
if (responseFuture.isCancelled()) {
response.close();
failed(new ProcessingException(new CancellationException(LocalizationMessages.ERROR_REQUEST_CANCELLED())));
return;
}
final T result;
if (callbackParamClass == Response.class) {
result = callbackParamClass.cast(new InboundJaxrsResponse(response, scope));
responseFuture.complete(result);
callback.completed(result);
} else if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
result = response.readEntity(new GenericType<T>(callbackParamType));
responseFuture.complete(result);
callback.completed(result);
} else {
failed(convertToException(new InboundJaxrsResponse(response, scope)));
}
}
@Override
public void failed(final ProcessingException error) {
try {
if (error.getCause() instanceof WebApplicationException) {
responseFuture.completeExceptionally(error.getCause());
} else if (!responseFuture.isCancelled()) {
responseFuture.completeExceptionally(error);
}
} finally {
callback.failed(error.getCause() instanceof CancellationException ? error.getCause() : error);
}
}
};
request().getClientRuntime().submit(requestForCall(requestContext), responseCallback);
} catch (final Throwable error) {
final ProcessingException ce;
//noinspection ChainOfInstanceofChecks
if (error instanceof ProcessingException) {
ce = (ProcessingException) error;
responseFuture.completeExceptionally(ce);
} else if (error instanceof WebApplicationException) {
ce = new ProcessingException(error);
responseFuture.completeExceptionally(error);
} else {
ce = new ProcessingException(error);
responseFuture.completeExceptionally(ce);
}
callback.failed(ce);
}
return responseFuture;
}
use of javax.ws.rs.core.GenericType in project jersey by jersey.
the class ModelEntityOnListTest method myBeanAndGet.
@Test
public void myBeanAndGet() {
WebTarget target = target("empty/getbean");
final Response response = target.request(MediaType.APPLICATION_JSON).get();
assertEquals(200, response.getStatus());
GenericType<List<MyBean>> ge = new GenericType<List<MyBean>>() {
};
List<MyBean> teb = response.readEntity(ge);
assertEquals("one element in list: " + teb, 1, teb.size());
assertEquals("value", "hello", teb.get(0).getValue());
}
use of javax.ws.rs.core.GenericType in project jersey by jersey.
the class RxObservableTest method testReadEntityViaGenericType.
@Test
public void testReadEntityViaGenericType() throws Throwable {
final String response = client.target("http://jersey.java.net").request().rx(RxObservableInvoker.class).get(new GenericType<String>() {
}).toBlocking().toFuture().get();
assertThat(response, is("NO-ENTITY"));
}
use of javax.ws.rs.core.GenericType in project jersey by jersey.
the class RxFlowableTest method testReadEntityViaGenericType.
@Test
public void testReadEntityViaGenericType() throws Throwable {
final String response = client.target("http://jersey.java.net").request().rx(RxFlowableInvoker.class).get(new GenericType<String>() {
}).blockingFirst();
assertThat(response, is("NO-ENTITY"));
}
Aggregations