Example usage for org.apache.http.auth AuthScope ANY

List of usage examples for org.apache.http.auth AuthScope ANY

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope ANY.

Prototype

AuthScope ANY

To view the source code for org.apache.http.auth AuthScope ANY.

Click Source Link

Document

Default scope matching any host, port, realm and authentication scheme.

Usage

From source file:aajavafx.DevicesController.java

@FXML
private void handleRemoveButton(ActionEvent event) {
    //remove is annotated with @DELETE on server so we use a HttpDelete object
    try {//from w  w w.  j a va2  s . co  m
        //......for ssl handshake....
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("EMPLOYEE", "password");
        provider.setCredentials(AuthScope.ANY, credentials);
        //........
        HttpClient httpClient = HttpClientBuilder.create().build();
        String idToDelete = deviceID.getText();
        //add the id to the end of the URL so this will call the method at MainServerREST/api/visitors/id
        HttpDelete delete = new HttpDelete(DevicesCustomerRootURL + idToDelete);
        HttpResponse response = httpClient.execute(delete);
        System.out.println("response from server " + response.getStatusLine());
    } catch (Exception ex) {
        System.out.println(ex);
    }
    try {
        //refresh table
        tableCustomer.setItems(getDevicesCustomer());
    } catch (IOException ex) {
        Logger.getLogger(VisitorController.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(VisitorController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.sonatype.nexus.client.rest.jersey.NexusClientFactoryImpl.java

protected void applyAuthenticationIfAny(final ConnectionInfo connectionInfo, ApacheHttpClient4Config config) {
    if (connectionInfo.getAuthenticationInfo() != null) {
        if (connectionInfo.getAuthenticationInfo() instanceof UsernamePasswordAuthenticationInfo) {
            final UsernamePasswordAuthenticationInfo upinfo = (UsernamePasswordAuthenticationInfo) connectionInfo
                    .getAuthenticationInfo();
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(upinfo.getUsername(), upinfo.getPassword()));
            config.getProperties().put(PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider);
            config.getProperties().put(PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true);
        } else {/*w  w  w. j a  va 2 s.c  o m*/
            throw new IllegalArgumentException(String.format("AuthenticationInfo of type %s is not supported!",
                    connectionInfo.getAuthenticationInfo().getClass().getName()));
        }
    }
}

From source file:aajavafx.EmployeeController.java

public static void validate(Employees emp) {
    try {//from   www .  ja va 2  s . c  o  m
        Gson gson = new Gson();
        String jsonString = new String(gson.toJson(emp));
        System.out.println("json string: " + jsonString);
        StringEntity postString = new StringEntity(jsonString);
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("EMPLOYEE", "password");
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
        HttpPost post = new HttpPost(postEmployeesURL);
        post.setEntity(postString);
        post.setHeader("Content-type", "application/json");
        HttpResponse response = httpClient.execute(post);

    } catch (UnsupportedEncodingException ex) {
        System.out.println(ex);
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

/**
 * @see DATASOLR-189/*from w ww.  ja  v  a2  s  .c o m*/
 */
@Test
public void cloningLBHttpSolrClientShouldCopyCredentialsProviderCorrectly() {

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("foo", "bar"));

    DefaultHttpClient client = new DefaultHttpClient();
    client.setCredentialsProvider(credentialsProvider);

    LBHttpSolrClient lbSolrClient = new LBHttpSolrClient(client, BASE_URL, ALTERNATE_BASE_URL);

    LBHttpSolrClient cloned = SolrClientUtils.clone(lbSolrClient, CORE_NAME);
    Assert.assertThat(((AbstractHttpClient) cloned.getHttpClient()).getCredentialsProvider(),
            IsEqual.<CredentialsProvider>equalTo(credentialsProvider));
}

From source file:com.ctctlabs.ctctwsjavalib.CTCTConnection.java

/**
 * Validates credentials with the web service and saves authentication information
 * @param apiKey//from   www  .j av  a  2 s  .  c o  m
 * @param username
 * @param password
 * @return True if the credentials are valid
 */
public boolean authenticate(String apiKey, String username, String password)
        throws ClientProtocolException, IOException {
    String loginUsername = apiKey + "%" + username;
    httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(loginUsername, password));
    InputStream stream = doGetRequest(API_BASE + "/ws/customers/" + username + "/");
    if (stream != null) {
        this.username = username;
        return true;
    } else {
        return false;
    }
}

From source file:imageLines.ImageHelpers.java

/**Read a jpeg image from a url into a BufferedImage*/

public static BufferedImage readAsBufferedImage(String imageURLString, String user, String pass) {
    InputStream i = null;//from www  .j av a  2  s  . com
    try {
        /*
        URLConnection conn=imageURL.openConnection();
        String userPassword=user+":"+ pass;
        String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
        conn.setRequestProperty ("Authorization", "Basic " + encoding);*/
        DefaultHttpClient httpclient = new DefaultHttpClient();

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY),
                new UsernamePasswordCredentials(user, pass));

        HttpGet httpget = new HttpGet(imageURLString);
        System.out.println("executing request" + httpget.getRequestLine());

        HttpResponse response = httpclient.execute(httpget);

        HttpEntity en = response.getEntity();
        Header[] h = response.getAllHeaders();
        for (int c = 0; c < h.length; c++)
            System.out.print(h[c].getName() + ":" + h[c].getValue() + "\n");

        i = response.getEntity().getContent();
        //JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(i);
        BufferedImage bi = ImageIO.read(i);//decoder.decodeAsBufferedImage();
        return bi;
    } catch (Exception e) {
        System.out.println(e);
        return null;
    } finally {
        try {
            if (i != null)
                i.close();
        } catch (IOException ex) {

        }
    }
}

From source file:org.apache.jena.jdbc.remote.RemoteEndpointDriver.java

protected HttpClient configureClient(Properties props) throws SQLException {
    // Try to get credentials to use
    String user = props.getProperty(PARAM_USERNAME, null);
    if (user != null && user.trim().isEmpty())
        user = null;//from  ww w.  j  a  v a2s.c  om
    String password = props.getProperty(PARAM_PASSWORD, null);
    if (password != null && password.trim().isEmpty())
        password = null;

    // If credentials then we use them
    if (user != null && password != null) {
        BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
        credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
        return HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
    }
    // else use a supplied or default client
    Object client = props.get(PARAM_CLIENT);
    if (client != null) {
        if (!(client instanceof HttpClient))
            throw new SQLException("The " + PARAM_CLIENT
                    + " parameter is specified but the value is not an object implementing the required HttpClient interface");
        return (HttpClient) client;
    }
    return null;
}

From source file:org.apache.hadoop.hbase.thrift.TestThriftSpnegoHttpServer.java

private CloseableHttpClient createHttpClient() throws Exception {
    final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(clientPrincipal, clientKeytab);
    final Set<Principal> clientPrincipals = clientSubject.getPrincipals();
    // Make sure the subject has a principal
    assertFalse(clientPrincipals.isEmpty());

    // Get a TGT for the subject (might have many, different encryption types). The first should
    // be the default encryption type.
    Set<KerberosTicket> privateCredentials = clientSubject.getPrivateCredentials(KerberosTicket.class);
    assertFalse(privateCredentials.isEmpty());
    KerberosTicket tgt = privateCredentials.iterator().next();
    assertNotNull(tgt);//w  w w.ja  va2 s . co  m

    // The name of the principal
    final String clientPrincipalName = clientPrincipals.iterator().next().getName();

    return Subject.doAs(clientSubject, new PrivilegedExceptionAction<CloseableHttpClient>() {
        @Override
        public CloseableHttpClient run() throws Exception {
            // Logs in with Kerberos via GSS
            GSSManager gssManager = GSSManager.getInstance();
            // jGSS Kerberos login constant
            Oid oid = new Oid("1.2.840.113554.1.2.2");
            GSSName gssClient = gssManager.createName(clientPrincipalName, GSSName.NT_USER_NAME);
            GSSCredential credential = gssManager.createCredential(gssClient, GSSCredential.DEFAULT_LIFETIME,
                    oid, GSSCredential.INITIATE_ONLY);

            Lookup<AuthSchemeProvider> authRegistry = RegistryBuilder.<AuthSchemeProvider>create()
                    .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)).build();

            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential));

            return HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry)
                    .setDefaultCredentialsProvider(credentialsProvider).build();
        }
    });
}

From source file:com.google.resting.RestingBuilder.java

/**
 * Enables basic authentication with username and password
 * /*  w w  w .ja v a  2  s.co m*/
 * @param user
 * @param password
 * @return a reference to this {@code RestingBuilder} object to fulfill the "Builder" pattern
 */
public RestingBuilder enableBasicAuthentication(String user, String password) {
    httpContext.setCredentials(user, password).setAuthScope(AuthScope.ANY);
    return this;
}