Search in sources :

Example 61 with GenericType

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);
}
Also used : Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) Invocation(javax.ws.rs.client.Invocation) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) LinkedList(java.util.LinkedList) Future(java.util.concurrent.Future) WebTarget(javax.ws.rs.client.WebTarget)

Example 62 with GenericType

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&lt;&gt() {...}}.
     * </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;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) RequestScope(org.glassfish.jersey.process.internal.RequestScope) CompletableFuture(java.util.concurrent.CompletableFuture) MediaType(javax.ws.rs.core.MediaType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) CancellationException(java.util.concurrent.CancellationException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 63 with GenericType

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());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) ArrayList(java.util.ArrayList) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 64 with GenericType

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"));
}
Also used : GenericType(javax.ws.rs.core.GenericType) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 65 with GenericType

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"));
}
Also used : GenericType(javax.ws.rs.core.GenericType) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

GenericType (javax.ws.rs.core.GenericType)126 List (java.util.List)64 WebTarget (javax.ws.rs.client.WebTarget)64 Response (javax.ws.rs.core.Response)60 Test (org.junit.Test)51 Client (javax.ws.rs.client.Client)24 ArrayList (java.util.ArrayList)17 Message (com.remswork.project.alice.model.support.Message)16 WebClient (org.apache.cxf.jaxrs.client.WebClient)16 GenericEntity (javax.ws.rs.core.GenericEntity)15 MediaType (javax.ws.rs.core.MediaType)15 Collection (java.util.Collection)14 Set (java.util.Set)14 LinkedList (java.util.LinkedList)12 HashSet (java.util.HashSet)11 Test (org.junit.jupiter.api.Test)11 JerseyTest (org.glassfish.jersey.test.JerseyTest)10 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)9 Collections (java.util.Collections)9 Entity (javax.ws.rs.client.Entity)9