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));
}
}
Aggregations