Search in sources :

Example 11 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project robozonky by RoboZonky.

the class ProxyFactory method newResteasyClient.

public static ResteasyClient newResteasyClient() {
    final ResteasyClientBuilder builder = new ResteasyClientBuilder().readTimeout(Settings.INSTANCE.getSocketTimeout().get(ChronoUnit.SECONDS), TimeUnit.SECONDS).connectTimeout(Settings.INSTANCE.getConnectionTimeout().get(ChronoUnit.SECONDS), TimeUnit.SECONDS);
    /*
         * In order to enable redirects, we need to configure the HTTP Client to support that. Unfortunately, if we then
         * provide such client to the RESTEasy client builder, it will take the client as is and don't do other things
         * that it would have otherwise done to a client that it itself created. Therefore, we do these things
         * ourselves, represented by calling the HTTP engine builder.
         */
    final ApacheHttpClient43Engine engine = (ApacheHttpClient43Engine) new ClientHttpEngineBuilder43().resteasyClientBuilder(builder).build();
    engine.setFollowRedirects(true);
    /*
         * Supply the provider factory singleton, as otherwise RESTEasy would create a new instance every time.
         */
    return new ResteasyClientBuilder().providerFactory(ResteasyProviderFactory.getInstance()).httpEngine(engine).build();
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ClientHttpEngineBuilder43(org.jboss.resteasy.client.jaxrs.ClientHttpEngineBuilder43) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine)

Example 12 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project sandbox by irof.

the class MySpringBootClient method main.

public static void main(String[] args) throws Exception {
    // RESTEasyのBuilderでやっちゃえば #disableTrustManager で手っ取り早い感ある。
    // javadoc に "this is a security hole" って書いてるけど。
    Client client = new ResteasyClientBuilder().disableTrustManager().build();
    Response response = client.target("https://localhost:9000").path("hello").request().get();
    System.out.println(response);
    System.out.println(response.getStatusInfo());
    System.out.println(response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Client(javax.ws.rs.client.Client)

Example 13 with ResteasyClientBuilder

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

the class ResteasyClientTracingRegistrarProvider method configure.

@Override
public ClientBuilder configure(ClientBuilder clientBuilder, ExecutorService executorService) {
    ResteasyClientBuilder resteasyClientBuilder = (ResteasyClientBuilder) clientBuilder;
    Tracer tracer = CDI.current().select(Tracer.class).get();
    return resteasyClientBuilder.executorService(new TracedExecutorService(executorService, tracer)).register(new SmallRyeClientTracingFeature(tracer));
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Tracer(io.opentracing.Tracer) TracedExecutorService(io.opentracing.contrib.concurrent.TracedExecutorService)

Example 14 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project dubbo by alibaba.

the class RestProtocol method doRefer.

@Override
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
    // TODO more configs to add
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    // 20 is the default maxTotal of current PoolingClientConnectionManager
    connectionManager.setMaxTotal(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL));
    connectionManager.setDefaultMaxPerRoute(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE));
    if (connectionMonitor == null) {
        connectionMonitor = new ConnectionMonitor();
        connectionMonitor.start();
    }
    connectionMonitor.addConnectionManager(connectionManager);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(url.getParameter(CONNECT_TIMEOUT_KEY, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT)).build();
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setKeepAliveStrategy((response, 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_KEY)) {
                return Long.parseLong(value) * 1000;
            }
        }
        return HTTPCLIENT_KEEPALIVEDURATION;
    }).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 : COMMA_SPLIT_PATTERN.split(url.getParameter(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);
}
Also used : TIMEOUT_KEY(org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY) DEFAULT_CONNECT_TIMEOUT(org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT) AbstractProxyProtocol(org.apache.dubbo.rpc.protocol.AbstractProxyProtocol) INTERFACE_KEY(org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) SERVER_KEY(org.apache.dubbo.remoting.Constants.SERVER_KEY) SocketConfig(org.apache.http.config.SocketConfig) RequestConfig(org.apache.http.client.config.RequestConfig) COMMA_SPLIT_PATTERN(org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN) BootstrapListener(org.apache.dubbo.remoting.http.servlet.BootstrapListener) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) StringUtils(org.apache.dubbo.common.utils.StringUtils) URL(org.apache.dubbo.common.URL) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) GetRestful(org.jboss.resteasy.util.GetRestful) LinkedList(java.util.LinkedList) HttpBinder(org.apache.dubbo.remoting.http.HttpBinder) HTTP(org.apache.http.protocol.HTTP) HeaderElementIterator(org.apache.http.HeaderElementIterator) DEFAULT_TIMEOUT(org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) EXTENSION_KEY(org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY) ApplicationModel(org.apache.dubbo.rpc.model.ApplicationModel) CONNECTIONS_KEY(org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY) RpcException(org.apache.dubbo.rpc.RpcException) HeaderElement(org.apache.http.HeaderElement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CONNECT_TIMEOUT_KEY(org.apache.dubbo.remoting.Constants.CONNECT_TIMEOUT_KEY) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ServletManager(org.apache.dubbo.remoting.http.servlet.ServletManager) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ServletContext(javax.servlet.ServletContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Collections(java.util.Collections) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) SocketConfig(org.apache.http.config.SocketConfig) HeaderElement(org.apache.http.HeaderElement) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) RpcException(org.apache.dubbo.rpc.RpcException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 15 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project oxTrust by GluuFederation.

the class RecaptchaUtil method verifyGoogleRecaptcha.

public boolean verifyGoogleRecaptcha(String gRecaptchaResponse, String secretKey) {
    boolean result = false;
    try {
        String uriTemplate = "https://www.google.com/recaptcha/api/siteverify";
        ResteasyClient resteasyClient;
        if (ProxyUtil.isProxyRequied()) {
            URLConnectionEngine engine = new URLConnectionEngine();
            resteasyClient = ((ResteasyClientBuilder) ResteasyClientBuilder.newBuilder()).httpEngine(engine).build();
        } else {
            resteasyClient = (ResteasyClient) ResteasyClientBuilder.newClient();
        }
        WebTarget webTarget = resteasyClient.target(uriTemplate);
        Builder clientRequest = webTarget.request();
        clientRequest.accept("application/json");
        Form requestForm = new Form();
        requestForm.param("secret", secretKey);
        requestForm.param("response", gRecaptchaResponse);
        Response response = clientRequest.buildPost(Entity.form(requestForm)).invoke();
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, String> map = mapper.readValue(new ByteArrayInputStream(response.readEntity(String.class).getBytes()), new TypeReference<Map<String, String>>() {
            });
            return Boolean.parseBoolean(map.get("success"));
        } finally {
            response.close();
            if (resteasyClient.httpEngine() != null) {
                resteasyClient.httpEngine().close();
            }
        }
    } catch (Exception e) {
        log.warn("Exception happened while verifying recaptcha, check your internet connection", e);
        return result;
    }
}
Also used : ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Form(javax.ws.rs.core.Form) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) WebTarget(javax.ws.rs.client.WebTarget) Map(java.util.Map) URLConnectionEngine(org.jboss.resteasy.client.jaxrs.engines.URLConnectionEngine) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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