Search in sources :

Example 41 with GenericType

use of javax.ws.rs.core.GenericType in project opennms by OpenNMS.

the class DefaultRemoteRepository method getOnlineReports.

/**
 * {@inheritDoc}
 */
@Override
public List<BasicReportDefinition> getOnlineReports() {
    List<BasicReportDefinition> resultReports = new ArrayList<>();
    List<RemoteReportSDO> webCallResult = new ArrayList<>();
    if (isConfigOk()) {
        WebTarget target = getTarget(m_remoteRepositoryDefintion.getURI() + "onlineReports" + "/" + m_jasperReportsVersion);
        try {
            webCallResult = getBuilder(target).get(new GenericType<List<RemoteReportSDO>>() {
            });
        } catch (Exception e) {
            logger.error("Error requesting online reports. Error message: '{}' URI was: '{}'", e.getMessage(), target.getUri());
            e.printStackTrace();
        }
        logger.debug("getOnlineReports got '{}' RemoteReportSDOs from uri '{}'", webCallResult.size(), target.getUri());
        resultReports = this.mapSDOListToBasicReportList(webCallResult);
    }
    return resultReports;
}
Also used : RemoteReportSDO(org.opennms.features.reporting.sdo.RemoteReportSDO) GenericType(javax.ws.rs.core.GenericType) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) BasicReportDefinition(org.opennms.features.reporting.model.basicreport.BasicReportDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 42 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 43 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 44 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 45 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)71 WebTarget (javax.ws.rs.client.WebTarget)42 List (java.util.List)35 Test (org.junit.Test)35 Response (javax.ws.rs.core.Response)24 ArrayList (java.util.ArrayList)15 GenericEntity (javax.ws.rs.core.GenericEntity)15 Collection (java.util.Collection)12 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 JerseyTest (org.glassfish.jersey.test.JerseyTest)9 Queue (java.util.Queue)8 GET (javax.ws.rs.GET)7 Produces (javax.ws.rs.Produces)7 MediaType (javax.ws.rs.core.MediaType)7 Type (java.lang.reflect.Type)6 HashSet (java.util.HashSet)6 ExecutionException (java.util.concurrent.ExecutionException)6 Path (javax.ws.rs.Path)6 JAXBElement (javax.xml.bind.JAXBElement)6 Comparator (java.util.Comparator)5