Search in sources :

Example 16 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class TestREST method configureClient.

public static ClientConfig configureClient() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }, ctx));
    } catch (Exception e) {
    }
    return config;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) JAXBException(javax.xml.bind.JAXBException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) DavException(org.apache.jackrabbit.webdav.DavException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 17 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class TestWebWAVFS method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    String propBasePath = "etc" + File.separator + "test.properties";
    prop = TestSettings.getTestProperties(propBasePath);
    String testURL = prop.getProperty("webdav.test.url");
    // Some problem with the pom.xml. The properties are set but System.getProperty gets null
    if (testURL == null) {
        testURL = "http://localhost:8080/lobcder-1.0-SNAPSHOT/";
    }
    assertTrue(testURL != null);
    if (!testURL.endsWith("/")) {
        testURL = testURL + "/";
    }
    uri = URI.create(testURL);
    root = uri.toASCIIString();
    if (!root.endsWith("/")) {
        root += "/";
    }
    username1 = prop.getProperty(("webdav.test.username1"), "");
    if (username1 == null) {
        username1 = "user1";
    }
    assertTrue(username1 != null);
    password1 = prop.getProperty(("webdav.test.password1"), "");
    if (password1 == null) {
        password1 = "passwd1";
    }
    assertTrue(password1 != null);
    username2 = prop.getProperty(("webdav.test.username2"), "user2");
    assertTrue(username2 != null);
    password2 = prop.getProperty(("webdav.test.password2"), "passwd2");
    assertTrue(password2 != null);
    quckTest = Boolean.valueOf(prop.getProperty(("test.quick"), "true"));
    client1 = new HttpClient();
    HttpClientParams params = new HttpClientParams();
    params.setParameter("http.protocol.handle-redirects", false);
    client1.setParams(params);
    assertNotNull(uri.getHost());
    assertNotNull(uri.getPort());
    assertNotNull(client1);
    int port = uri.getPort();
    if (port == -1) {
        port = 443;
    }
    ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
    Protocol https = new Protocol("https", socketFactory, port);
    Protocol.registerProtocol("https", https);
    client1.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username1, password1));
    httpclient = new DefaultHttpClient();
    org.apache.http.auth.Credentials defaultcreds = new org.apache.http.auth.UsernamePasswordCredentials(username1, password1);
    httpclient.getCredentialsProvider().setCredentials(org.apache.http.auth.AuthScope.ANY, (org.apache.http.auth.Credentials) defaultcreds);
    // httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    client2 = new HttpClient();
    assertNotNull(uri.getHost());
    assertNotNull(uri.getPort());
    assertNotNull(client2);
    client2.getState().setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username2, password2));
    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    restClient = Client.create(clientConfig);
    restClient.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username1, password1));
    restURL = prop.getProperty(("rest.test.url"), "http://localhost:8080/lobcder-2.0-SNAPSHOT/rest/");
    utils = new Utils(client1);
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) BeforeClass(org.junit.BeforeClass)

Example 18 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project lobcder by skoulouzis.

the class VPDRI method configureClient.

public static ClientConfig configureClient() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (java.security.GeneralSecurityException ex) {
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
    ClientConfig config = new DefaultClientConfig();
    try {
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }, ctx));
    } catch (Exception e) {
    }
    return config;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) ResourceNotFoundException(nl.uva.vlet.exception.ResourceNotFoundException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) JAXBException(javax.xml.bind.JAXBException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) VlException(nl.uva.vlet.exception.VlException) VRLSyntaxException(nl.uva.vlet.exception.VRLSyntaxException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) BadPaddingException(javax.crypto.BadPaddingException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 19 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project atlas by apache.

the class AtlasBaseClient method getClient.

@VisibleForTesting
protected Client getClient(Configuration configuration, UserGroupInformation ugi, String doAsUser) {
    DefaultClientConfig config = new DefaultClientConfig();
    // Enable POJO mapping feature
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    int readTimeout = configuration.getInt("atlas.client.readTimeoutMSecs", 60000);
    int connectTimeout = configuration.getInt("atlas.client.connectTimeoutMSecs", 60000);
    if (configuration.getBoolean(TLS_ENABLED, false)) {
        // configuration object, persist it, then subsequently pass in an empty configuration to SSLFactory
        try {
            SecureClientUtils.persistSSLClientConfiguration(configuration);
        } catch (Exception e) {
            LOG.info("Error processing client configuration.", e);
        }
    }
    final URLConnectionClientHandler handler;
    if ((AuthenticationUtil.isKerberosAuthenticationEnabled())) {
        handler = SecureClientUtils.getClientConnectionHandler(config, configuration, doAsUser, ugi);
    } else {
        if (configuration.getBoolean(TLS_ENABLED, false)) {
            handler = SecureClientUtils.getUrlConnectionClientHandler();
        } else {
            handler = new URLConnectionClientHandler();
        }
    }
    Client client = new Client(handler, config);
    client.setReadTimeout(readTimeout);
    client.setConnectTimeout(connectTimeout);
    return client;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) URLConnectionClientHandler(com.sun.jersey.client.urlconnection.URLConnectionClientHandler) Client(com.sun.jersey.api.client.Client) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project cloudbreak by hortonworks.

the class YarnHttpClient method deleteApplication.

@Override
public void deleteApplication(DeleteApplicationRequest deleteApplicationRequest) throws CloudbreakOrchestratorFailedException, MalformedURLException {
    // Add the application name to the URL
    YarnEndpoint dashEndpoint = new YarnEndpoint(apiEndpoint, YarnResourceConstants.APPLICATIONS_PATH + '/' + deleteApplicationRequest.getName());
    ClientConfig clientConfig = new DefaultClientConfig();
    Client client = Client.create(clientConfig);
    // Delete the application
    WebResource webResource = client.resource(dashEndpoint.getFullEndpointUrl().toString());
    ClientResponse response = webResource.accept("application/json").type("application/json").delete(ClientResponse.class);
    // Validate HTTP 204 return
    String msg;
    switch(response.getStatus()) {
        case YarnResourceConstants.HTTP_NO_CONTENT:
            msg = String.format("Successfully deleted application %s", deleteApplicationRequest.getName());
            LOGGER.debug(msg);
            break;
        case YarnResourceConstants.HTTP_NOT_FOUND:
            msg = String.format("Application %s not found, already deleted?", deleteApplicationRequest.getName());
            LOGGER.debug(msg);
            break;
        default:
            msg = String.format("Received %d status code from url %s, reason: %s", response.getStatus(), dashEndpoint.getFullEndpointUrl().toString(), response.getEntity(String.class));
            LOGGER.debug(msg);
            throw new CloudbreakOrchestratorFailedException(msg);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) CloudbreakOrchestratorFailedException(com.sequenceiq.cloudbreak.orchestrator.exception.CloudbreakOrchestratorFailedException) WebResource(com.sun.jersey.api.client.WebResource) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) Client(com.sun.jersey.api.client.Client) YarnEndpoint(com.sequenceiq.cloudbreak.orchestrator.yarn.api.YarnEndpoint)

Aggregations

DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)65 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)55 Client (com.sun.jersey.api.client.Client)42 WebResource (com.sun.jersey.api.client.WebResource)21 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)15 ClientResponse (com.sun.jersey.api.client.ClientResponse)14 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)9 HostnameVerifier (javax.net.ssl.HostnameVerifier)7 SSLSession (javax.net.ssl.SSLSession)7 TrustManager (javax.net.ssl.TrustManager)7 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)6 IOException (java.io.IOException)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)5 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)5 SecureRandom (java.security.SecureRandom)5 SSLContext (javax.net.ssl.SSLContext)5 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4