Example usage for org.apache.commons.httpclient.auth CredentialsProvider CredentialsProvider

List of usage examples for org.apache.commons.httpclient.auth CredentialsProvider CredentialsProvider

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.auth CredentialsProvider CredentialsProvider.

Prototype

CredentialsProvider

Source Link

Usage

From source file:com.alibaba.antx.config.resource.http.HttpSession.java

public HttpSession(ResourceDriver driver) {
    super(driver);

    client = new HttpClient();

    client.getParams().setAuthenticationPreemptive(true);
    client.getParams().setParameter(CredentialsProvider.PROVIDER, new CredentialsProvider() {
        public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy)
                throws CredentialsNotAvailableException {
            URI uri = ResourceContext.get().getCurrentURI();
            String username = ResourceContext.get().getCurrentUsername();
            Set visitedURIs = ResourceContext.get().getVisitedURIs();
            ResourceKey key = new ResourceKey(new ResourceURI(uri));
            String message;/*from  www  . j  a v a 2  s . c  om*/

            message = "\n";
            message += "Authentication required.\n";
            message += "realm: " + scheme.getRealm() + "\n";
            message += "  uri: " + uri + "\n";

            UsernamePassword up = getResourceManager().getAuthenticationHandler().authenticate(message, uri,
                    username, visitedURIs.contains(key));

            visitedURIs.add(key);

            return new UsernamePasswordCredentials(up.getUsername(), up.getPassword());
        }
    });
}

From source file:sernet.springclient.CommonsExecuter.java

@Override
void init() {// w  w w . j  a v  a  2 s . c o m

    /**
     * Sets the verinice password dialog.
     */
    getHttpClient().getParams().setParameter(CredentialsProvider.PROVIDER, new CredentialsProvider() {

        @Override
        public Credentials getCredentials(AuthScheme authScheme, String host, int port, boolean arg3)
                throws CredentialsNotAvailableException {
            Authentication auth = AuthDialog.getAuthentication(host + ", Port: " + port, authScheme.getRealm());
            return new UsernamePasswordCredentials(auth.getUser(), auth.getPassword());
        }
    });
}