use of org.jboss.resteasy.client.jaxrs.ResteasyClient 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));
}
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project openremote by openremote.
the class WebsocketIOClient method doConnect.
@Override
protected Future<Boolean> doConnect() {
if (oAuthGrant != null) {
LOG.fine("Retrieving OAuth access token: " + getClientUri());
ResteasyClient client = createClient(executorService, 1, CONNECTION_TIMEOUT_MILLISECONDS, null);
try {
OAuthFilter oAuthFilter = new OAuthFilter(client, oAuthGrant);
authHeaderValue = oAuthFilter.getAuthHeader();
if (TextUtil.isNullOrEmpty(authHeaderValue)) {
throw new RuntimeException("Returned access token is null");
}
LOG.fine("Retrieved access token via OAuth: " + getClientUri());
} catch (SocketException | ProcessingException e) {
LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token for '" + getClientUri() + "': Connection error");
return CompletableFuture.completedFuture(false);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token: " + getClientUri());
return CompletableFuture.completedFuture(false);
} finally {
if (client != null) {
client.close();
}
}
}
return super.doConnect();
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project java by wavefrontHQ.
the class APIContainer method createService.
/**
* Create RESTeasy proxies for remote calls via HTTP.
*/
private <T> T createService(String serverEndpointUrl, Class<T> apiClass) {
ResteasyClient client = new ResteasyClientBuilder().httpEngine(clientHttpEngine).providerFactory(resteasyProviderFactory).build();
ResteasyWebTarget target = client.target(serverEndpointUrl);
return target.proxy(apiClass);
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project dubbo by alibaba.
the class RestProtocol method doRefer.
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).build();
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
}).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build();
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
return target.proxy(serviceType);
}
use of org.jboss.resteasy.client.jaxrs.ResteasyClient in project java by wavefrontHQ.
the class AbstractAgent method createAgentService.
/**
* Create RESTeasy proxies for remote calls via HTTP.
*/
protected WavefrontAPI createAgentService() {
ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
factory.registerProvider(JsonNodeWriter.class);
if (!factory.getClasses().contains(ResteasyJackson2Provider.class)) {
factory.registerProvider(ResteasyJackson2Provider.class);
}
if (httpUserAgent == null) {
httpUserAgent = "Wavefront-Proxy/" + props.getString("build.version");
}
ClientHttpEngine httpEngine;
if (javaNetConnection) {
httpEngine = new JavaNetConnectionEngine() {
@Override
protected HttpURLConnection createConnection(ClientInvocation request) throws IOException {
HttpURLConnection connection = (HttpURLConnection) request.getUri().toURL().openConnection();
connection.setRequestProperty("User-Agent", httpUserAgent);
connection.setRequestMethod(request.getMethod());
// 5s
connection.setConnectTimeout(httpConnectTimeout);
// 60s
connection.setReadTimeout(httpRequestTimeout);
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection secureConnection = (HttpsURLConnection) connection;
secureConnection.setSSLSocketFactory(new SSLSocketFactoryImpl(HttpsURLConnection.getDefaultSSLSocketFactory(), httpRequestTimeout));
}
return connection;
}
};
} else {
HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().setUserAgent(httpUserAgent).setMaxConnTotal(httpMaxConnTotal).setMaxConnPerRoute(httpMaxConnPerRoute).setConnectionTimeToLive(1, TimeUnit.MINUTES).setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(httpRequestTimeout).build()).setSSLSocketFactory(new SSLConnectionSocketFactoryImpl(SSLConnectionSocketFactory.getSystemSocketFactory(), httpRequestTimeout)).setRetryHandler(new DefaultHttpRequestRetryHandler(httpAutoRetries, true)).setDefaultRequestConfig(RequestConfig.custom().setContentCompressionEnabled(true).setRedirectsEnabled(true).setConnectTimeout(httpConnectTimeout).setConnectionRequestTimeout(httpConnectTimeout).setSocketTimeout(httpRequestTimeout).build()).build();
final ApacheHttpClient4Engine apacheHttpClient4Engine = new ApacheHttpClient4Engine(httpClient, true);
// avoid using disk at all
apacheHttpClient4Engine.setFileUploadInMemoryThresholdLimit(100);
apacheHttpClient4Engine.setFileUploadMemoryUnit(ApacheHttpClient4Engine.MemoryUnit.MB);
httpEngine = apacheHttpClient4Engine;
}
ResteasyClient client = new ResteasyClientBuilder().httpEngine(httpEngine).providerFactory(factory).register(GZIPDecodingInterceptor.class).register(gzipCompression ? GZIPEncodingInterceptor.class : DisableGZIPEncodingInterceptor.class).register(AcceptEncodingGZIPFilter.class).build();
ResteasyWebTarget target = client.target(server);
return target.proxy(WavefrontAPI.class);
}
Aggregations