Search in sources :

Example 21 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder 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);
}
Also used : ResteasyJackson2Provider(org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) GZIPDecodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) ClientInvocation(org.jboss.resteasy.client.jaxrs.internal.ClientInvocation) IOException(java.io.IOException) AcceptEncodingGZIPFilter(org.jboss.resteasy.plugins.interceptors.encoding.AcceptEncodingGZIPFilter) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) HttpURLConnection(java.net.HttpURLConnection) GZIPEncodingInterceptor(org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor) HttpClient(org.apache.http.client.HttpClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 22 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project java by wavefrontHQ.

the class HttpClientTest method httpClientTimeoutsWork.

@Test(expected = ProcessingException.class)
public void httpClientTimeoutsWork() throws Exception {
    ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance();
    factory.registerProvider(JsonNodeWriter.class);
    factory.registerProvider(ResteasyJackson2Provider.class);
    HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().setMaxConnTotal(200).setMaxConnPerRoute(100).setConnectionTimeToLive(1, TimeUnit.MINUTES).setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(100).build()).setDefaultRequestConfig(RequestConfig.custom().setContentCompressionEnabled(true).setRedirectsEnabled(true).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(60000).build()).setSSLSocketFactory(new LayeredConnectionSocketFactory() {

        @Override
        public Socket createLayeredSocket(Socket socket, String target, int port, HttpContext context) throws IOException, UnknownHostException {
            return SSLConnectionSocketFactory.getSystemSocketFactory().createLayeredSocket(socket, target, port, context);
        }

        @Override
        public Socket createSocket(HttpContext context) throws IOException {
            return SSLConnectionSocketFactory.getSystemSocketFactory().createSocket(context);
        }

        @Override
        public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException {
            assertTrue("Non-zero timeout passed to connect socket is expected", connectTimeout > 0);
            throw new ProcessingException("OK");
        }
    }).build();
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(new ApacheHttpClient4Engine(httpClient, true)).providerFactory(factory).build();
    SocketServerRunnable sr = new SocketServerRunnable();
    Thread serverThread = new Thread(sr);
    serverThread.start();
    ResteasyWebTarget target = client.target("https://localhost:" + sr.getPort());
    SimpleRESTEasyAPI proxy = target.proxy(SimpleRESTEasyAPI.class);
    proxy.search("resteasy");
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) InetSocketAddress(java.net.InetSocketAddress) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) HttpContext(org.apache.http.protocol.HttpContext) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 23 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project microservice_framework by CJSCommonPlatform.

the class CakeShopIT method before.

@Before
public void before() throws Exception {
    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    final ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
    client = new ResteasyClientBuilder().httpEngine(engine).build();
    httpResponsePoller = new HttpResponsePoller();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) HttpResponsePoller(uk.gov.justice.services.test.utils.core.http.HttpResponsePoller) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Before(org.junit.Before)

Example 24 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project scheduling by ow2-proactive.

the class DataSpaceClient method delete.

@Override
public boolean delete(IRemoteSource source) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Trying to delete " + source);
    }
    StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(source.getDataspace().value());
    ResteasyClient client = new ResteasyClientBuilder().providerFactory(providerFactory).httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(source.getPath());
    List<String> includes = source.getIncludes();
    if (includes != null && !includes.isEmpty()) {
        target = target.queryParam("includes", includes.toArray(new Object[includes.size()]));
    }
    List<String> excludes = source.getExcludes();
    if (excludes != null && !excludes.isEmpty()) {
        target = target.queryParam("excludes", excludes.toArray(new Object[excludes.size()]));
    }
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).delete();
        boolean noContent = false;
        if (response.getStatus() != HttpURLConnection.HTTP_NO_CONTENT) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedException("User not authenticated or session timeout.");
            } else {
                throw new RuntimeException("Cannot delete file(s). Status :" + response.getStatusInfo() + " Entity : " + response.getEntity());
            }
        } else {
            noContent = true;
            log.debug("No action performed for deletion since source " + source + " was not found remotely");
        }
        if (!noContent && log.isDebugEnabled()) {
            log.debug("Removal of " + source + " performed with success");
        }
        return true;
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 25 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project scheduling by ow2-proactive.

the class DataSpaceClient method download.

@Override
public boolean download(IRemoteSource source, ILocalDestination destination) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Downloading from " + source + " to " + destination);
    }
    StringBuffer uriTmpl = (new StringBuffer()).append(restDataspaceUrl).append(source.getDataspace().value());
    ResteasyClient client = new ResteasyClientBuilder().providerFactory(providerFactory).httpEngine(httpEngine).build();
    ResteasyWebTarget target = client.target(uriTmpl.toString()).path(source.getPath());
    List<String> includes = source.getIncludes();
    if (includes != null && !includes.isEmpty()) {
        target = target.queryParam("includes", includes.toArray(new Object[includes.size()]));
    }
    List<String> excludes = source.getExcludes();
    if (excludes != null && !excludes.isEmpty()) {
        target = target.queryParam("excludes", excludes.toArray(new Object[excludes.size()]));
    }
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).acceptEncoding("*", "gzip", "zip").get();
        if (response.getStatus() != HttpURLConnection.HTTP_OK) {
            if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
                throw new NotConnectedException("User not authenticated or session timeout.");
            } else {
                throw new RuntimeException(String.format("Cannot retrieve the file. Status code: %s", response.getStatus()));
            }
        }
        if (response.hasEntity()) {
            InputStream is = response.readEntity(InputStream.class);
            destination.readFrom(is, response.getHeaderString(HttpHeaders.CONTENT_ENCODING));
        } else {
            throw new RuntimeException(String.format("%s in %s is empty.", source.getDataspace(), source.getPath()));
        }
        if (log.isDebugEnabled()) {
            log.debug("Download from " + source + " to " + destination + " performed with success");
        }
        return true;
    } catch (IOException ioe) {
        throw Throwables.propagate(ioe);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) InputStream(java.io.InputStream) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) IOException(java.io.IOException)

Aggregations

ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)40 ResteasyClient (org.jboss.resteasy.client.jaxrs.ResteasyClient)22 ResteasyWebTarget (org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)18 Response (javax.ws.rs.core.Response)11 Test (org.junit.Test)9 ApacheHttpClient4Engine (org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 ServicesInterface (com.baeldung.client.ServicesInterface)6 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)6 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)6 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)6 RequestConfig (org.apache.http.client.config.RequestConfig)5 ApacheHttpClient43Engine (org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine)5 IOException (java.io.IOException)4 Client (javax.ws.rs.client.Client)4 HttpClient (org.apache.http.client.HttpClient)4 ProcessingException (javax.ws.rs.ProcessingException)3 ClientHttpEngine (org.jboss.resteasy.client.jaxrs.ClientHttpEngine)3 Movie (com.baeldung.model.Movie)2 JacksonJaxbJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)2