Search in sources :

Example 1 with DEFAULT_HTTP_METHOD

use of org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_HTTP_METHOD in project openremote by openremote.

the class WebsocketAgentProtocol method doSubscription.

protected void doSubscription(Map<String, List<String>> headers, WebsocketSubscription subscription) {
    if (subscription instanceof WebsocketHTTPSubscription) {
        WebsocketHTTPSubscription httpSubscription = (WebsocketHTTPSubscription) subscription;
        if (TextUtil.isNullOrEmpty(httpSubscription.uri)) {
            LOG.warning("Websocket subscription missing or empty URI so skipping: " + subscription);
            return;
        }
        URI uri;
        try {
            uri = new URI(httpSubscription.uri);
        } catch (URISyntaxException e) {
            LOG.warning("Websocket subscription invalid URI so skipping: " + subscription);
            return;
        }
        if (httpSubscription.method == null) {
            httpSubscription.method = WebsocketHTTPSubscription.Method.valueOf(DEFAULT_HTTP_METHOD);
        }
        if (TextUtil.isNullOrEmpty(httpSubscription.contentType)) {
            httpSubscription.contentType = DEFAULT_CONTENT_TYPE;
        }
        if (httpSubscription.headers != null) {
            headers = headers != null ? new HashMap<>(headers) : new HashMap<>();
            Map<String, List<String>> finalHeaders = headers;
            httpSubscription.headers.forEach((header, values) -> {
                if (values == null || values.isEmpty()) {
                    finalHeaders.remove(header);
                } else {
                    List<String> vals = new ArrayList<>(finalHeaders.compute(header, (h, l) -> l != null ? l : Collections.emptyList()));
                    vals.addAll(values);
                    finalHeaders.put(header, vals);
                }
            });
        }
        WebTargetBuilder webTargetBuilder = new WebTargetBuilder(resteasyClient, uri);
        if (headers != null) {
            webTargetBuilder.setInjectHeaders(headers);
        }
        LOG.fine("Creating web target client for subscription '" + uri + "'");
        ResteasyWebTarget target = webTargetBuilder.build();
        Invocation invocation;
        if (httpSubscription.body == null) {
            invocation = target.request().build(httpSubscription.method.toString());
        } else {
            invocation = target.request().build(httpSubscription.method.toString(), Entity.entity(httpSubscription.body, httpSubscription.contentType));
        }
        Response response = invocation.invoke();
        response.close();
        if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            LOG.warning("WebsocketHttpSubscription returned an un-successful response code: " + response.getStatus());
        }
    } else {
        client.sendMessage(ValueUtil.convert(subscription.body, String.class));
    }
}
Also used : java.util(java.util) DEFAULT_CONTENT_TYPE(org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_CONTENT_TYPE) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) URISyntaxException(java.net.URISyntaxException) AttributeRef(org.openremote.model.attribute.AttributeRef) ValueUtil(org.openremote.model.util.ValueUtil) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) Supplier(java.util.function.Supplier) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) Attribute(org.openremote.model.attribute.Attribute) AbstractNettyIOClientProtocol(org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol) AttributeEvent(org.openremote.model.attribute.AttributeEvent) SyslogCategory(org.openremote.model.syslog.SyslogCategory) TextUtil(org.openremote.model.util.TextUtil) URI(java.net.URI) HttpHeaders(org.apache.http.HttpHeaders) OAuthGrant(org.openremote.model.auth.OAuthGrant) ValueType(org.openremote.model.value.ValueType) DEFAULT_HTTP_METHOD(org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_HTTP_METHOD) Pair(org.openremote.model.util.Pair) Invocation(javax.ws.rs.client.Invocation) Logger(java.util.logging.Logger) Entity(javax.ws.rs.client.Entity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) BasicAuthHelper(org.jboss.resteasy.util.BasicAuthHelper) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Response(javax.ws.rs.core.Response) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ChannelHandler(io.netty.channel.ChannelHandler) WebTargetBuilder.createClient(org.openremote.container.web.WebTargetBuilder.createClient) UsernamePassword(org.openremote.model.auth.UsernamePassword) ProtocolUtil(org.openremote.model.protocol.ProtocolUtil) AttributeExecuteStatus(org.openremote.model.attribute.AttributeExecuteStatus) Invocation(javax.ws.rs.client.Invocation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Response(javax.ws.rs.core.Response) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 Logger (java.util.logging.Logger)1 Entity (javax.ws.rs.client.Entity)1 Invocation (javax.ws.rs.client.Invocation)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Response (javax.ws.rs.core.Response)1 HttpHeaders (org.apache.http.HttpHeaders)1 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)1 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)1 BasicAuthHelper (org.jboss.resteasy.util.BasicAuthHelper)1 DEFAULT_CONTENT_TYPE (org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_CONTENT_TYPE)1 DEFAULT_HTTP_METHOD (org.openremote.agent.protocol.http.HTTPProtocol.DEFAULT_HTTP_METHOD)1 AbstractNettyIOClientProtocol (org.openremote.agent.protocol.io.AbstractNettyIOClientProtocol)1 WebTargetBuilder (org.openremote.container.web.WebTargetBuilder)1