Search in sources :

Example 1 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter 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 2 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter 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 3 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project coprhd-controller by CoprHD.

the class IsilonApiFactory method getRESTClient.

/**
 * Create Isilon API client
 *
 * @param endpoint isilon endpoint
 * @return
 */
public IsilonApi getRESTClient(URI endpoint, String username, String password) {
    IsilonApi isilonApi = _clientMap.get(endpoint.toString() + ":" + username + ":" + password);
    if (isilonApi == null) {
        Client jerseyClient = new ApacheHttpClient(_clientHandler);
        jerseyClient.addFilter(new HTTPBasicAuthFilter(username, password));
        RESTClient restClient = new RESTClient(jerseyClient);
        isilonApi = new IsilonApi(endpoint, restClient);
        _clientMap.putIfAbsent(endpoint.toString() + ":" + username + ":" + password, isilonApi);
    }
    return isilonApi;
}
Also used : ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 4 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project ORCID-Source by ORCID.

the class OrcidClientHelper method addBasicAuth.

public void addBasicAuth(String username, String password) {
    basicAuthFilter = new HTTPBasicAuthFilter(username, password);
    jerseyClient.addFilter(basicAuthFilter);
}
Also used : HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 5 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project microservices by pwillhan.

the class ComponentFinder method main.

public static void main(String[] args) {
    Scanner scanner = null;
    try {
        scanner = new Scanner(System.in);
        System.out.println("Enter component type in plural form (e.g. queues, exchanges) ");
        String type = scanner.nextLine();
        System.out.println("Enter vhost (leave empty for default vhost) ");
        String vhost = scanner.nextLine();
        System.out.println("Enter name pattern (leave empty for match-all pattern)");
        String pattern = scanner.nextLine();
        Client client = Client.create();
        String path;
        if (vhost.trim().isEmpty()) {
            path = API_ROOT + "/" + type + "?columns=name";
        } else {
            path = API_ROOT + "/" + type + "/" + vhost + "?columns=name";
        }
        WebResource resource = client.resource(path);
        resource.header("Content-Type", "application/json;charset=UTF-8");
        resource.addFilter(new HTTPBasicAuthFilter("guest", "guest".getBytes()));
        String result = resource.get(String.class);
        JSONArray jsonResult = new JSONArray(result);
        LOGGER.debug("Result: \n" + jsonResult.toString(4));
        filterResult(jsonResult, pattern);
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}
Also used : Scanner(java.util.Scanner) JSONArray(org.json.JSONArray) WebResource(com.sun.jersey.api.client.WebResource) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Aggregations

HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)46 Client (com.sun.jersey.api.client.Client)32 WebResource (com.sun.jersey.api.client.WebResource)19 ClientResponse (com.sun.jersey.api.client.ClientResponse)15 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)14 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)14 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)7 ArrayList (java.util.ArrayList)7 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)6 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)5 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 SSLSession (javax.net.ssl.SSLSession)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 ClientRequest (com.sun.jersey.api.client.ClientRequest)3 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)3 FormDataMultiPart (com.sun.jersey.multipart.FormDataMultiPart)3 FileInputStream (java.io.FileInputStream)3 URI (java.net.URI)3 KeyStore (java.security.KeyStore)3