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

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

Introduction

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

Prototype

String ANY_REALM

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

Click Source Link

Document

The null value represents any realm.

Usage

From source file:com.unboundid.scim.sdk.PreemptiveAuthInterceptor.java

/**
 * Method to update the AuthState in order to preemptively supply the
 * credentials to the server.//from   w  w w .  j a  va2  s .  co m
 *
 * @param host the HttpHost which we're authenticating to
 * @param authScheme the AuthScheme in use
 * @param authState the AuthState object from the HttpContext
 * @param credsProvider the CredentialsProvider which has the username and
 *                      password
 */
private void doPreemptiveAuth(final HttpHost host, final AuthScheme authScheme, final AuthState authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();

    final AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(schemeName)) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    }
}

From source file:org.apache.manifoldcf.authorities.authorities.generic.GenericAuthority.java

protected DefaultHttpClient getClient() throws ManifoldCFException {
    synchronized (this) {
        if (client != null) {
            return client;
        }//from  w ww.j  a  v a 2  s  .  c om
        DefaultHttpClient cl = new DefaultHttpClient();
        if (genericLogin != null && !genericLogin.isEmpty()) {
            try {
                URL url = new URL(genericEntryPoint);
                Credentials credentials = new UsernamePasswordCredentials(genericLogin, genericPassword);
                cl.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(),
                        url.getPort() > 0 ? url.getPort() : 80, AuthScope.ANY_REALM), credentials);
                cl.addRequestInterceptor(new PreemptiveAuth(credentials), 0);
            } catch (MalformedURLException ex) {
                client = null;
                sessionExpirationTime = -1L;
                throw new ManifoldCFException("getClient exception: " + ex.getMessage(), ex);
            }
        }
        HttpConnectionParams.setConnectionTimeout(cl.getParams(), connectionTimeoutMillis);
        HttpConnectionParams.setSoTimeout(cl.getParams(), socketTimeoutMillis);
        sessionExpirationTime = System.currentTimeMillis() + 300000L;
        client = cl;
        return cl;
    }
}

From source file:org.brutusin.rpc.client.http.HttpEndpoint.java

public static void main(String[] args) throws Exception {

    HttpClientContextFactory ctxFact = new HttpClientContextFactory() {
        public HttpClientContext create() {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM, "basic"),
                    new UsernamePasswordCredentials("user", "password"));
            HttpClientContext context = HttpClientContext.create();
            context.setCredentialsProvider(credsProvider);
            return context;
        }//  w ww .  j a  v a 2  s . com
    };

    HttpEndpoint endpoint = new HttpEndpoint(new URI("http://localhost:8080/rpc/http"), ctxFact);

    HttpResponse resp = endpoint.exec("rpc.http.version", null, null);
    if (resp.isIsBinary()) {
        System.out.println("binary");
        System.out.println(resp.getInputStream().getName());
    } else {
        System.out.println(resp.getRpcResponse().getResult());
    }
}

From source file:net.gromgull.android.bibsonomyposter.BibsonomyPosterActivity.java

public void bookmark(String url, String title) throws ClientProtocolException, IOException {
    CredentialsProvider credProvider = new BasicCredentialsProvider();

    credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST,

            AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, apikey));

    StringWriter sw = new StringWriter();

    XmlSerializer x = Xml.newSerializer();
    x.setOutput(sw);/*w  ww  . j av a 2 s  . com*/
    x.startDocument(null, null);
    x.startTag(null, "bibsonomy");
    x.startTag(null, "post");
    x.attribute(null, "description", "a bookmark");

    x.startTag(null, "user");
    x.attribute(null, "name", username);
    x.endTag(null, "user");

    x.startTag(null, "tag");
    x.attribute(null, "name", "from_android");
    x.endTag(null, "tag");

    x.startTag(null, "group");
    x.attribute(null, "name", "public");
    x.endTag(null, "group");

    x.startTag(null, "bookmark");
    x.attribute(null, "url", url);
    x.attribute(null, "title", title);
    x.endTag(null, "bookmark");

    x.endTag(null, "post");
    x.endTag(null, "bibsonomy");

    x.endDocument();

    Log.v(LOGTAG, "XML: " + sw.toString());

    HttpPost httppost = new HttpPost("http://www.bibsonomy.org/api/users/" + username + "/posts");
    StringEntity e = new StringEntity(sw.toString());
    e.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/xml"));
    httppost.setEntity(e);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.setCredentialsProvider(credProvider);
    HttpResponse response = httpclient.execute(httppost);
    Log.i(LOGTAG, "Bibsonomy said :" + response.getStatusLine());
    if (response.getStatusLine().getStatusCode() != 201) {
        HttpEntity re = response.getEntity();
        byte b[] = new byte[(int) re.getContentLength()];
        re.getContent().read(b);
        Log.v(LOGTAG, "Bibsonomy said: " + new String(b));
        throw new IOException("Bibsonomy said :" + response.getStatusLine());

    }
}

From source file:com.jfrog.bintray.client.impl.HttpClientConfigurator.java

/**
 * Ignores blank username input/*from  ww  w.j  a va  2 s.  c o m*/
 */
public HttpClientConfigurator authentication(String username, String password) {
    if (StringUtils.isNotBlank(username)) {
        if (StringUtils.isBlank(host)) {
            throw new IllegalStateException("Cannot configure authentication when host is not set.");
        }
        credsProvider.setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(username, password));

        builder.addInterceptorFirst(new PreemptiveAuthInterceptor());
    }
    return this;
}

From source file:org.eclipse.aether.transport.http.HttpTransporter.java

private static CredentialsProvider toCredentialsProvider(String host, int port, AuthenticationContext ctx) {
    DeferredCredentialsProvider provider = new DeferredCredentialsProvider();
    if (ctx != null) {
        AuthScope basicScope = new AuthScope(host, port);
        provider.setCredentials(basicScope, new DeferredCredentialsProvider.BasicFactory(ctx));

        AuthScope ntlmScope = new AuthScope(host, port, AuthScope.ANY_REALM, "ntlm");
        provider.setCredentials(ntlmScope, new DeferredCredentialsProvider.NtlmFactory(ctx));
    }//from   w ww. j  a v a  2  s.  com
    return provider;
}

From source file:org.apache.manifoldcf.crawler.connectors.generic.GenericConnector.java

protected DefaultHttpClient getClient() throws ManifoldCFException {
    DefaultHttpClient cl = new DefaultHttpClient();
    if (genericLogin != null && !genericLogin.isEmpty()) {
        try {//from  ww  w .  jav a  2s .c o m
            URL url = new URL(genericEntryPoint);
            Credentials credentials = new UsernamePasswordCredentials(genericLogin, genericPassword);
            cl.getCredentialsProvider().setCredentials(
                    new AuthScope(url.getHost(), url.getPort() > 0 ? url.getPort() : 80, AuthScope.ANY_REALM),
                    credentials);
            cl.addRequestInterceptor(new PreemptiveAuth(credentials), 0);
        } catch (MalformedURLException ex) {
            throw new ManifoldCFException("getClient exception: " + ex.getMessage(), ex);
        }
    }
    HttpConnectionParams.setConnectionTimeout(cl.getParams(), connectionTimeoutMillis);
    HttpConnectionParams.setSoTimeout(cl.getParams(), socketTimeoutMillis);
    return cl;
}

From source file:org.apache.abdera2.common.protocol.BasicClient.java

/**
 * Add authentication credentials//from   w  w  w  .  java  2  s.c o  m
 */
public <T extends Client> T addCredentials(String target, String realm, String scheme, Credentials credentials)
        throws URISyntaxException {
    String host = AuthScope.ANY_HOST;
    int port = AuthScope.ANY_PORT;
    if (target != null) {
        URI uri = new URI(target);
        host = uri.getHost();
        port = uri.getPort();
    }
    HttpHost targetHost = new HttpHost(host, port, scheme);
    getDefaultHttpClient().getCredentialsProvider()
            .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort(),
                    realm != null ? realm : AuthScope.ANY_REALM,
                    scheme != null ? scheme : AuthScope.ANY_SCHEME), credentials);
    return (T) this;
}

From source file:org.mumod.util.HttpManager.java

public void setCredentials(String username, String password) {

    Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
    String host = AuthScope.ANY_HOST;
    if (mHost != null)
        host = mHost;//  w  w  w  .  j a  va  2  s .c om
    BasicCredentialsProvider cP = new BasicCredentialsProvider();
    cP.setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    mClient.setCredentialsProvider(cP);
    mClient.addRequestInterceptor(preemptiveAuth, 0);

}