Search in sources :

Example 6 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project pentaho-kettle by pentaho.

the class Wadl2JavaRestConnectivetyCheck method initRestService1.

/**
 * Test stand alone generated code web service connectivity
 */
private void initRestService1() throws InitializationException {
    // get information about the remote connection
    String username = "admin";
    String password = "password";
    ClientConfig clientConfig = new DefaultClientConfig();
    client = Client.create(clientConfig);
    client.addFilter(new HTTPBasicAuthFilter(username, password));
    URI baseUri = null;
    try {
        baseUri = new URI(uri + "/plugin/");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    PentahoDiPlugin.PurRepositoryPluginApiRevision revisionService = PentahoDiPlugin.purRepositoryPluginApiRevision(client, baseUri);
    PentahoDiPlugin.PurRepositoryPluginApiRevision.PathIdVersioningConfiguration versioningConfigurationMethod = revisionService.pathIdVersioningConfiguration("foo.ktr");
    FileVersioningConfiguration fileVersioningConfiguration = versioningConfigurationMethod.getAsXml(FileVersioningConfiguration.class);
    System.out.println("Test1: " + fileVersioningConfiguration.isVersioningEnabled());
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) URISyntaxException(java.net.URISyntaxException) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) URI(java.net.URI)

Example 7 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project pentaho-kettle by pentaho.

the class RepositoryCleanupUtil method authenticateLoginCredentials.

/**
 * Use REST API to authenticate provided credentials
 *
 * @throws Exception
 */
private void authenticateLoginCredentials() throws Exception {
    if (client == null) {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        client = Client.create(clientConfig);
        client.addFilter(new HTTPBasicAuthFilter(username, password));
    }
    WebResource resource = client.resource(url + AUTHENTICATION + AdministerSecurityAction.NAME);
    String response = resource.get(String.class);
    if (!response.equals("true")) {
        throw new Exception(Messages.getInstance().getString("REPOSITORY_CLEANUP_UTIL.ERROR_0012.ACCESS_DENIED"));
    }
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) WebResource(com.sun.jersey.api.client.WebResource) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) ParseException(java.text.ParseException)

Example 8 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter 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
 */
private static void callStopCarteRestService(String hostname, String port, String username, String password) throws ParseException, CarteCommandException {
    // get information about the remote connection
    try {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        Client client = Client.create(clientConfig);
        client.addFilter(new HTTPBasicAuthFilter(username, 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)

Example 9 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project pentaho-kettle by pentaho.

the class Rest method setConfig.

private void setConfig() throws KettleException {
    if (data.config == null) {
        // Use ApacheHttpClient for supporting proxy authentication.
        data.config = new DefaultApacheHttpClient4Config();
        if (!Utils.isEmpty(data.realProxyHost)) {
            // PROXY CONFIGURATION
            data.config.getProperties().put(ApacheHttpClient4Config.PROPERTY_PROXY_URI, "http://" + data.realProxyHost + ":" + data.realProxyPort);
            if (!Utils.isEmpty(data.realHttpLogin) && !Utils.isEmpty(data.realHttpPassword)) {
                AuthScope authScope = new AuthScope(data.realProxyHost, data.realProxyPort);
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(data.realHttpLogin, data.realHttpPassword);
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(authScope, credentials);
                data.config.getProperties().put(ApacheHttpClient4Config.PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider);
            }
        } else {
            if (!Utils.isEmpty(data.realHttpLogin)) {
                // Basic authentication
                data.basicAuthentication = new HTTPBasicAuthFilter(data.realHttpLogin, data.realHttpPassword);
            }
        }
        if (meta.isPreemptive()) {
            data.config.getProperties().put(ApacheHttpClient4Config.PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true);
        }
        // SSL TRUST STORE CONFIGURATION
        if (!Utils.isEmpty(data.trustStoreFile)) {
            try (FileInputStream trustFileStream = new FileInputStream(data.trustStoreFile)) {
                KeyStore trustStore = KeyStore.getInstance("JKS");
                trustStore.load(trustFileStream, data.trustStorePassword.toCharArray());
                TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
                tmf.init(trustStore);
                SSLContext ctx = SSLContext.getInstance("SSL");
                ctx.init(null, tmf.getTrustManagers(), null);
                HostnameVerifier hv = new HostnameVerifier() {

                    public boolean verify(String hostname, SSLSession session) {
                        if (isDebug()) {
                            logDebug("Warning: URL Host: " + hostname + " vs. " + session.getPeerHost());
                        }
                        return true;
                    }
                };
                data.config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hv, ctx));
            } catch (NoSuchAlgorithmException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.NoSuchAlgorithm"), e);
            } catch (KeyStoreException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.KeyStoreException"), e);
            } catch (CertificateException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.CertificateException"), e);
            } catch (FileNotFoundException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.FileNotFound", data.trustStoreFile), e);
            } catch (IOException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.IOException"), e);
            } catch (KeyManagementException e) {
                throw new KettleException(BaseMessages.getString(PKG, "Rest.Error.KeyManagementException"), e);
            }
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SSLSession(javax.net.ssl.SSLSession) FileNotFoundException(java.io.FileNotFoundException) CertificateException(java.security.cert.CertificateException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagementException(java.security.KeyManagementException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultApacheHttpClient4Config(com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) AuthScope(org.apache.http.auth.AuthScope) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties)

Example 10 with HTTPBasicAuthFilter

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

the class AuthSvcTests method createHttpsClient.

/**
 * Conveninence method to create a Client, and add authentication info
 * if desired. If addAuthFilter is set to true, credentials will be added
 * to a Basic Auth filter, and 302 will be followed manually, adding the auth token
 * on the final redirect to the service location. If addAuthFilter is set to
 * false, a regular 302 follow up will be done, no headers or basic auth will be
 * added.
 *
 * @throws NoSuchAlgorithmException
 */
protected Client createHttpsClient(final String username, final String password, boolean addAuthFilters) throws NoSuchAlgorithmException {
    // Disable server certificate validation as we are using
    // self-signed certificate
    disableCertificateValidation();
    final ClientConfig config = new DefaultClientConfig();
    final Client c = Client.create(config);
    c.addFilter(new LoggingFilter());
    if (addAuthFilters) {
        // do a "modified 302" below with copying the header
        c.setFollowRedirects(false);
        c.addFilter(new HTTPBasicAuthFilter(username, password));
        c.addFilter(new ClientFilter() {

            @Override
            public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
                if (_savedTokens.containsKey(username)) {
                    ArrayList<Object> token = new ArrayList<Object>();
                    token.add(_savedTokens.get(username));
                    request.getHeaders().put(AUTH_TOKEN_HEADER, token);
                }
                ClientResponse response = getNext().handle(request);
                if (response.getHeaders() != null && response.getHeaders().get(AUTH_TOKEN_HEADER) != null) {
                    _savedTokens.put(username, response.getHeaders().getFirst(AUTH_TOKEN_HEADER));
                }
                if (response.getHeaders() != null && response.getHeaders().get(AUTH_PROXY_TOKEN_HEADER) != null) {
                    _savedProxyTokens.put(username, response.getHeaders().getFirst(AUTH_PROXY_TOKEN_HEADER));
                }
                if (response.getStatus() == 302) {
                    WebResource wb = c.resource(response.getLocation());
                    response = wb.header(AUTH_TOKEN_HEADER, _savedTokens.get(username)).get(ClientResponse.class);
                }
                return response;
            }
        });
    } else {
        // no auth filter, and do a regular 302 follow up, don't copy any auth token.
        c.setFollowRedirects(true);
    }
    return c;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) LoggingFilter(com.sun.jersey.api.client.filter.LoggingFilter) ArrayList(java.util.ArrayList) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig)

Aggregations

HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)45 Client (com.sun.jersey.api.client.Client)31 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 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)5 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)4 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