Example usage for org.apache.commons.httpclient HttpState getCredentials

List of usage examples for org.apache.commons.httpclient HttpState getCredentials

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpState getCredentials.

Prototype

public Credentials getCredentials(AuthScope paramAuthScope) 

Source Link

Usage

From source file:com.legstar.http.client.CicsHttpTest.java

/**
 * Try to create a state./*from  w w w  . j av  a2  s . c o m*/
 */
public void testCreateHttpState() {
    CicsHttp cicsHttp = new CicsHttp(getName(), getEndpoint());
    HttpState state = cicsHttp.createHttpState("mainframe", 3080, "P390", "TRUC", null);
    AuthScope as = new AuthScope("mainframe", 3080, null, AuthScope.ANY_SCHEME);
    assertEquals("P390:TRUC", state.getCredentials(as).toString());
}

From source file:com.twinsoft.convertigo.engine.enums.AuthenticationMode.java

public boolean setCredentials(HttpState httpState, String user, String password, String host, String domain) {
    if (httpState == null)
        return false;
    if (host == null)
        return false;

    AuthScope authScope = new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

    Credentials credentials = null;//w ww  .jav a2s  .  c o  m
    int type = getType();
    try {
        switch (type) {
        case 0: // Anonymous
            credentials = ac;
            break;
        case 1: // Basic
            credentials = new UsernamePasswordCredentials(user, password);
            break;
        case 2: // NTLM
            credentials = new NTCredentials(user, password, host, domain);
            break;
        default: // None
        case -1:
            break;
        }

        Credentials curCred = httpState.getCredentials(authScope);
        int needChange = compare(curCred, credentials);
        switch (needChange) {
        case -1:
            httpState.setCredentials(authScope, null);
            Engine.logEngine.debug("(AuthenticationMode) credentials cleared");
            break;
        case 1:
            httpState.setCredentials(authScope, credentials);
            Engine.logEngine.debug("(AuthenticationMode) " + name() + " credentials: " + user + ": ******");
            break;
        case 0:
            Engine.logEngine.debug("(AuthenticationMode) reusing credentials");
            break;
        }

        return needChange != 0;

    } catch (Exception e) {
        Engine.logEngine.error("Unable to set " + name() + " credentials for user", e);
        return false;
    }
}