Search in sources :

Example 81 with GenericType

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

the class RestClient method getServicesForANode.

public List<OnmsMonitoredService> getServicesForANode(String nodeCriteria, String ipAddress) {
    GenericType<List<OnmsMonitoredService>> services = new GenericType<List<OnmsMonitoredService>>() {
    };
    final WebTarget target = getTarget().path("nodes").path(nodeCriteria).path("ipinterfaces").path(ipAddress).path("services");
    return getBuilder(target).get(services);
}
Also used : GenericType(javax.ws.rs.core.GenericType) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService)

Example 82 with GenericType

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

the class RestClient method getAllEvents.

public List<OnmsEvent> getAllEvents() {
    GenericType<List<OnmsEvent>> events = new GenericType<List<OnmsEvent>>() {
    };
    final WebTarget target = getTarget().path("events").queryParam("limit", 0);
    return getBuilder(target).get(events);
}
Also used : GenericType(javax.ws.rs.core.GenericType) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget)

Example 83 with GenericType

use of javax.ws.rs.core.GenericType in project camel by apache.

the class CxfRsProducer method invokeHttpClient.

protected void invokeHttpClient(Exchange exchange) throws Exception {
    Message inMessage = exchange.getIn();
    JAXRSClientFactoryBean cfb = clientFactoryBeanCache.get(CxfEndpointUtils.getEffectiveAddress(exchange, ((CxfRsEndpoint) getEndpoint()).getAddress()));
    Bus bus = ((CxfRsEndpoint) getEndpoint()).getBus();
    // We need to apply the bus setting from the CxfRsEndpoint which is not use the default bus
    if (bus != null) {
        cfb.setBus(bus);
    }
    WebClient client = cfb.createWebClient();
    ((CxfRsEndpoint) getEndpoint()).getChainedCxfRsEndpointConfigurer().configureClient(client);
    String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
    Class<?> responseClass = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Class.class);
    Type genericType = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_GENERIC_TYPE, Type.class);
    Object[] pathValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
    String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class);
    if (LOG.isTraceEnabled()) {
        LOG.trace("HTTP method = {}", httpMethod);
        LOG.trace("path = {}", path);
        LOG.trace("responseClass = {}", responseClass);
    }
    // set the path
    if (path != null) {
        if (ObjectHelper.isNotEmpty(pathValues) && pathValues.length > 0) {
            client.path(path, pathValues);
        } else {
            client.path(path);
        }
    }
    CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
    CxfRsBinding binding = cxfRsEndpoint.getBinding();
    Object body = getBody(exchange, inMessage, httpMethod, cxfRsEndpoint, binding);
    setupClientMatrix(client, exchange);
    setupClientQueryAndHeaders(client, exchange);
    // handle cookies
    CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
    loadCookies(exchange, client, cookieHandler);
    // invoke the client
    Object response = null;
    if (responseClass == null || Response.class.equals(responseClass)) {
        response = client.invoke(httpMethod, body);
    } else {
        if (Collection.class.isAssignableFrom(responseClass)) {
            if (genericType instanceof ParameterizedType) {
                // Get the collection member type first
                Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments();
                response = client.invokeAndGetCollection(httpMethod, body, (Class<?>) actualTypeArguments[0]);
            } else {
                throw new CamelExchangeException("Header " + CxfConstants.CAMEL_CXF_RS_RESPONSE_GENERIC_TYPE + " not found in message", exchange);
            }
        } else {
            response = client.invoke(httpMethod, body, responseClass);
        }
    }
    int statesCode = client.getResponse().getStatus();
    // handle cookies
    saveCookies(exchange, client, cookieHandler);
    //http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if (throwException) {
        if (response instanceof Response) {
            Integer respCode = ((Response) response).getStatus();
            if (respCode > 207) {
                throw populateCxfRsProducerException(exchange, (Response) response, respCode);
            }
        }
    }
    // set response
    if (exchange.getPattern().isOutCapable()) {
        LOG.trace("Response body = {}", response);
        exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
        exchange.getOut().setBody(binding.bindResponseToCamelBody(response, exchange));
        exchange.getOut().getHeaders().putAll(binding.bindResponseHeadersToCamelHeaders(response, exchange));
        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, statesCode);
    } else {
        // just close the input stream of the response object
        if (response instanceof Response) {
            ((Response) response).close();
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) CamelExchangeException(org.apache.camel.CamelExchangeException) Message(org.apache.camel.Message) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebClient(org.apache.cxf.jaxrs.client.WebClient) Response(javax.ws.rs.core.Response) ParameterizedType(java.lang.reflect.ParameterizedType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CookieHandler(org.apache.camel.http.common.cookie.CookieHandler)

Example 84 with GenericType

use of javax.ws.rs.core.GenericType in project camel by apache.

the class CxfRsProducer method invokeAsyncHttpClient.

protected void invokeAsyncHttpClient(Exchange exchange, final AsyncCallback callback) throws Exception {
    Message inMessage = exchange.getIn();
    JAXRSClientFactoryBean cfb = clientFactoryBeanCache.get(CxfEndpointUtils.getEffectiveAddress(exchange, ((CxfRsEndpoint) getEndpoint()).getAddress()));
    Bus bus = ((CxfRsEndpoint) getEndpoint()).getBus();
    // We need to apply the bus setting from the CxfRsEndpoint which is not use the default bus
    if (bus != null) {
        cfb.setBus(bus);
    }
    WebClient client = cfb.createWebClient();
    ((CxfRsEndpoint) getEndpoint()).getChainedCxfRsEndpointConfigurer().configureClient(client);
    String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
    Class<?> responseClass = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Class.class);
    Type genericType = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_GENERIC_TYPE, Type.class);
    Object[] pathValues = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, Object[].class);
    String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class);
    if (LOG.isTraceEnabled()) {
        LOG.trace("HTTP method = {}", httpMethod);
        LOG.trace("path = {}", path);
        LOG.trace("responseClass = {}", responseClass);
    }
    // set the path
    if (path != null) {
        if (ObjectHelper.isNotEmpty(pathValues) && pathValues.length > 0) {
            client.path(path, pathValues);
        } else {
            client.path(path);
        }
    }
    CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
    CxfRsBinding binding = cxfRsEndpoint.getBinding();
    Object body = getBody(exchange, inMessage, httpMethod, cxfRsEndpoint, binding);
    setupClientMatrix(client, exchange);
    setupClientQueryAndHeaders(client, exchange);
    //Build message entity
    Entity<Object> entity = binding.bindCamelMessageToRequestEntity(body, inMessage, exchange);
    // handle cookies
    CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
    loadCookies(exchange, client, cookieHandler);
    // invoke the client
    client.async().method(httpMethod, entity, new CxfInvocationCallback(client, exchange, cxfRsEndpoint, responseClass, callback, genericType));
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.camel.Message) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebClient(org.apache.cxf.jaxrs.client.WebClient) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CookieHandler(org.apache.camel.http.common.cookie.CookieHandler)

Example 85 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)

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