Code example for CharArrayBuffer

Methods: append, indexOf, substringTrimmed, toString

0
 
    public String getRealm() {
        // NTLM does not support the concept of an authentication realm 
        return null; 
    } 
 
    public boolean isConnectionBased() { 
        return true; 
    } 
 
    @Override 
    protected void parseChallenge( 
            final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
        String challenge = buffer.substringTrimmed(pos, len);
        if (challenge.length() == 0) {
            if (this.state == State.UNINITIATED) {
                this.state = State.CHALLENGE_RECEIVED;
            } else { 
                this.state = State.FAILED;
            } 
            this.challenge = null;
        } else { 
            this.state = State.MSG_TYPE2_RECEVIED;
            this.challenge = challenge;
        } 
    } 
 
    public Header authenticate(
            final Credentials credentials, 
            final HttpRequest request) throws AuthenticationException {
        NTCredentials ntcredentials = null;
        try { 
            ntcredentials = (NTCredentials) credentials;
        } catch (ClassCastException e) {
            throw new InvalidCredentialsException(
             "Credentials cannot be used for NTLM authentication: "  
              + credentials.getClass().getName());
        } 
        String response = null;
        if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
            response = this.engine.generateType1Msg(
                    ntcredentials.getDomain(), 
                    ntcredentials.getWorkstation());
            this.state = State.MSG_TYPE1_GENERATED;
        } else if (this.state == State.MSG_TYPE2_RECEVIED) {
            response = this.engine.generateType3Msg(
                    ntcredentials.getUserName(), 
                    ntcredentials.getPassword(), 
                    ntcredentials.getDomain(), 
                    ntcredentials.getWorkstation(),
                    this.challenge);
            this.state = State.MSG_TYPE3_GENERATED;
        } else { 
            throw new AuthenticationException("Unexpected state: " + this.state);
        } 
        CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else { 
            buffer.append(AUTH.WWW_AUTH_RESP);
        } 
        buffer.append(": NTLM ");
        buffer.append(response);
        return new BufferedHeader(buffer);
    } 
 
    public boolean isComplete() { 
        return this.state == State.MSG_TYPE3_GENERATED || this.state == State.FAILED;