Search in sources :

Example 26 with ResteasyClientBuilder

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

the class DataSpaceClient method metadata.

@Override
public Map<String, String> metadata(IRemoteSource source) throws NotConnectedException, PermissionException {
    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());
    Response response = null;
    try {
        response = target.request().header("sessionid", sessionId).head();
        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 get metadata from %s in '%s' dataspace.", source.getPath(), source.getDataspace()));
            }
        }
        MultivaluedMap<String, Object> headers = response.getHeaders();
        Map<String, String> metaMap = Maps.newHashMap();
        if (headers.containsKey(HttpHeaders.LAST_MODIFIED)) {
            metaMap.put(HttpHeaders.LAST_MODIFIED, String.valueOf(headers.getFirst(HttpHeaders.LAST_MODIFIED)));
        }
        return metaMap;
    } 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 27 with ResteasyClientBuilder

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

the class DataSpaceClient method create.

@Override
public boolean create(IRemoteSource source) throws NotConnectedException, PermissionException {
    if (log.isDebugEnabled()) {
        log.debug("Trying to create file " + 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());
    Response response = null;
    try {
        Invocation.Builder request = target.request();
        request.header("sessionid", sessionId);
        Form form = new Form();
        form.param("mimetype", source.getType().getMimeType());
        response = request.post(Entity.form(form));
        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("Cannot create file(s). Status code:" + response.getStatus());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("File creation " + 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) Invocation(javax.ws.rs.client.Invocation) NotConnectedException(org.ow2.proactive.scheduler.common.exception.NotConnectedException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 28 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project eap-additional-testsuite by jboss-set.

the class ApacheHttpClient432TestCase method apacheHttpClient4EngineServletTest.

@Test
@OperateOnDeployment(DEPLOYMENT)
public void apacheHttpClient4EngineServletTest(@ArquillianResource URL url) throws Exception {
    SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoReuseAddress(true).build();
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(100);
    connManager.setDefaultMaxPerRoute(100);
    connManager.setDefaultSocketConfig(socketConfig);
    RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(100).setConnectionRequestTimeout(3000).setStaleConnectionCheckEnabled(true).build();
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).setConnectionManager(connManager).build();
    final ClientHttpEngine executor;
    executor = new ApacheHttpClient43Engine(httpClient);
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(executor).build();
    final ApacheHttpClient43Resource proxy = client.target("http://127.0.0.1:8080/" + ApacheHttpClient432TestCase.class.getSimpleName()).proxy(ApacheHttpClient43Resource.class);
    WebTarget target = client.target("http://127.0.0.1:8080/" + ApacheHttpClient432TestCase.class.getSimpleName() + "/test2");
    Response response = target.request().get();
    Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
    try {
        Response s = proxy.get();
        assertEquals(200, s.getStatus());
    } catch (ProcessingException e) {
        logger.warn("Exception occured." + e);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : 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) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Response(javax.ws.rs.core.Response) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) ApacheHttpClient43Resource(org.jboss.additional.testsuite.jdkall.present.jaxrs.client.resource.ApacheHttpClient43Resource) WebTarget(javax.ws.rs.client.WebTarget) ProcessingException(javax.ws.rs.ProcessingException) OperateOnDeployment(org.jboss.arquillian.container.test.api.OperateOnDeployment) Test(org.junit.Test)

Example 29 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project eap-additional-testsuite by jboss-set.

the class ApacheHttpClient43TestCase method createEngine.

@SuppressWarnings(value = "unchecked")
private ResteasyClient createEngine(Class engine) {
    RequestConfig reqConfig = // apache HttpClient specific
    RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).setConnectionRequestTimeout(5000).build();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(reqConfig).setMaxConnTotal(3).build();
    final ClientHttpEngine executor;
    if (engine.isAssignableFrom(ApacheHttpClient43Engine.class)) {
        executor = new ApacheHttpClient43Engine(httpClient);
    } else {
        executor = new URLConnectionEngine();
    }
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(executor).build();
    return client;
}
Also used : 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) ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) URLConnectionEngine(org.jboss.resteasy.client.jaxrs.engines.URLConnectionEngine)

Example 30 with ResteasyClientBuilder

use of org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder in project eap-additional-testsuite by jboss-set.

the class JaxbProviderDeserializationSecurityCheckTestCase method doRequestAndExpectIllegalTypeMessage.

private void doRequestAndExpectIllegalTypeMessage(String nastyJson) {
    ResteasyClient client = null;
    Response response = null;
    try {
        client = new ResteasyClientBuilder().build();
        Invocation.Builder request = client.target(url + "rest/jaxb/bad").request();
        response = request.post(Entity.entity(nastyJson, MediaType.APPLICATION_JSON_TYPE));
        Assert.assertEquals("The request should fail because of security reasons!", 400, response.getStatus());
        Assert.assertTrue("The response should contain \"Illegal type\"", response.readEntity(String.class).contains("Illegal type"));
    } finally {
        response.close();
        client.close();
    }
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Invocation(javax.ws.rs.client.Invocation)

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