Search in sources :

Example 6 with DefaultClientConfig

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

the class LdapPolicyMgrUserGroupBuilder method getClient.

private synchronized Client getClient() {
    Client ret = null;
    if (policyMgrBaseUrl.startsWith("https://")) {
        ClientConfig config = new DefaultClientConfig();
        if (sslContext == null) {
            try {
                KeyManager[] kmList = null;
                TrustManager[] tmList = null;
                if (keyStoreFile != null && keyStoreFilepwd != null) {
                    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
                    InputStream in = null;
                    try {
                        in = getFileInputStream(keyStoreFile);
                        if (in == null) {
                            LOG.error("Unable to obtain keystore from file [" + keyStoreFile + "]");
                            return ret;
                        }
                        keyStore.load(in, keyStoreFilepwd.toCharArray());
                        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                        keyManagerFactory.init(keyStore, keyStoreFilepwd.toCharArray());
                        kmList = keyManagerFactory.getKeyManagers();
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                if (trustStoreFile != null && trustStoreFilepwd != null) {
                    KeyStore trustStore = KeyStore.getInstance(trustStoreType);
                    InputStream in = null;
                    try {
                        in = getFileInputStream(trustStoreFile);
                        if (in == null) {
                            LOG.error("Unable to obtain keystore from file [" + trustStoreFile + "]");
                            return ret;
                        }
                        trustStore.load(in, trustStoreFilepwd.toCharArray());
                        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                        trustManagerFactory.init(trustStore);
                        tmList = trustManagerFactory.getTrustManagers();
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                sslContext = SSLContext.getInstance("SSL");
                sslContext.init(kmList, tmList, new SecureRandom());
                hv = new HostnameVerifier() {

                    public boolean verify(String urlHostName, SSLSession session) {
                        return session.getPeerHost().equals(urlHostName);
                    }
                };
            } catch (Throwable t) {
                throw new RuntimeException("Unable to create SSLConext for communication to policy manager", t);
            }
        }
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hv, sslContext));
        ret = Client.create(config);
    } else {
        ClientConfig cc = new DefaultClientConfig();
        cc.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
        ret = Client.create(cc);
    }
    if (!(authenticationType != null && AUTH_KERBEROS.equalsIgnoreCase(authenticationType) && SecureClientLogin.isKerberosCredentialExists(principal, keytab))) {
        if (ret != null) {
            String username = config.getPolicyMgrUserName();
            String password = config.getPolicyMgrPassword();
            if (username == null || password == null || username.trim().isEmpty() || password.trim().isEmpty()) {
                username = config.getDefaultPolicyMgrUserName();
                password = config.getDefaultPolicyMgrPassword();
            }
            if (username != null && password != null) {
                ret.addFilter(new HTTPBasicAuthFilter(username, password));
            }
        }
    }
    return ret;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) KeyStore(java.security.KeyStore) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Client(com.sun.jersey.api.client.Client) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) KeyManager(javax.net.ssl.KeyManager) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 7 with DefaultClientConfig

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

the class PolicyMgrUserGroupBuilder method getClient.

private synchronized Client getClient() {
    Client ret = null;
    if (policyMgrBaseUrl.startsWith("https://")) {
        ClientConfig config = new DefaultClientConfig();
        if (sslContext == null) {
            try {
                KeyManager[] kmList = null;
                TrustManager[] tmList = null;
                if (keyStoreFile != null && keyStoreFilepwd != null) {
                    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
                    InputStream in = null;
                    try {
                        in = getFileInputStream(keyStoreFile);
                        if (in == null) {
                            LOG.error("Unable to obtain keystore from file [" + keyStoreFile + "]");
                            return ret;
                        }
                        keyStore.load(in, keyStoreFilepwd.toCharArray());
                        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                        keyManagerFactory.init(keyStore, keyStoreFilepwd.toCharArray());
                        kmList = keyManagerFactory.getKeyManagers();
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                if (trustStoreFile != null && trustStoreFilepwd != null) {
                    KeyStore trustStore = KeyStore.getInstance(trustStoreType);
                    InputStream in = null;
                    try {
                        in = getFileInputStream(trustStoreFile);
                        if (in == null) {
                            LOG.error("Unable to obtain keystore from file [" + trustStoreFile + "]");
                            return ret;
                        }
                        trustStore.load(in, trustStoreFilepwd.toCharArray());
                        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                        trustManagerFactory.init(trustStore);
                        tmList = trustManagerFactory.getTrustManagers();
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                sslContext = SSLContext.getInstance("SSL");
                sslContext.init(kmList, tmList, new SecureRandom());
                hv = new HostnameVerifier() {

                    public boolean verify(String urlHostName, SSLSession session) {
                        return session.getPeerHost().equals(urlHostName);
                    }
                };
            } catch (Throwable t) {
                throw new RuntimeException("Unable to create SSLConext for communication to policy manager", t);
            }
        }
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hv, sslContext));
        ret = Client.create(config);
    } else {
        ClientConfig cc = new DefaultClientConfig();
        cc.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
        ret = Client.create(cc);
    }
    if (!(authenticationType != null && AUTH_KERBEROS.equalsIgnoreCase(authenticationType) && SecureClientLogin.isKerberosCredentialExists(principal, keytab))) {
        if (ret != null) {
            String username = config.getPolicyMgrUserName();
            String password = config.getPolicyMgrPassword();
            if (username == null || password == null || username.trim().isEmpty() || password.trim().isEmpty()) {
                username = config.getDefaultPolicyMgrUserName();
                password = config.getDefaultPolicyMgrPassword();
            }
            if (username != null && password != null) {
                ret.addFilter(new HTTPBasicAuthFilter(username, password));
            }
        }
    }
    return ret;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) KeyStore(java.security.KeyStore) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Client(com.sun.jersey.api.client.Client) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) KeyManager(javax.net.ssl.KeyManager) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 8 with DefaultClientConfig

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

the class NutchClientImpl method createClient.

public void createClient() {
    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    this.client = Client.create(clientConfig);
    this.nutchResource = client.resource(instance.getUrl());
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig)

Example 9 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project pentaho-kettle by pentaho.

the class Carte method callStopCarteRestService.

/**
 * Checks that Carte is running and if so, shuts down the Carte server
 *
 * @param hostname
 * @param port
 * @param username
 * @param password
 * @throws ParseException
 * @throws CarteCommandException
 */
@VisibleForTesting
static void callStopCarteRestService(String hostname, String port, String username, String password) throws ParseException, CarteCommandException {
    // get information about the remote connection
    try {
        KettleClientEnvironment.init();
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        Client client = Client.create(clientConfig);
        client.addFilter(new HTTPBasicAuthFilter(username, Encr.decryptPasswordOptionallyEncrypted(password)));
        // check if the user can access the carte server. Don't really need this call but may want to check it's output at
        // some point
        String contextURL = "http://" + hostname + ":" + port + "/kettle";
        WebResource resource = client.resource(contextURL + "/status/?xml=Y");
        String response = resource.get(String.class);
        if (response == null || !response.contains("<serverstatus>")) {
            throw new Carte.CarteCommandException(BaseMessages.getString(PKG, "Carte.Error.NoServerFound", hostname, "" + port));
        }
        // This is the call that matters
        resource = client.resource(contextURL + "/stopCarte");
        response = resource.get(String.class);
        if (response == null || !response.contains("Shutting Down")) {
            throw new Carte.CarteCommandException(BaseMessages.getString(PKG, "Carte.Error.NoShutdown", hostname, "" + port));
        }
    } catch (Exception e) {
        throw new Carte.CarteCommandException(BaseMessages.getString(PKG, "Carte.Error.NoServerFound", hostname, "" + port), e);
    }
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) WebResource(com.sun.jersey.api.client.WebResource) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) ParseException(org.apache.commons.cli.ParseException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project pentaho-kettle by pentaho.

the class PentahoDiPlugin method createClient.

/**
 * Create a new Client instance
 */
public static com.sun.jersey.api.client.Client createClient() {
    ClientConfig cc = new DefaultClientConfig();
    customizeClientConfiguration(cc);
    return createClientInstance(cc);
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig)

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