Code example for CharArrayBuffer

Methods: append, indexOf, substringTrimmed, toString

0
     */ 
    public String getSchemeName() {
        return "basic"; 
    } 
 
    /** 
     * Processes the Basic challenge. 
     *   
     * @param header the challenge header 
     *  
     * @throws MalformedChallengeException is thrown if the authentication challenge 
     * is malformed 
     */ 
    @Override 
    public void processChallenge( 
            final Header header) throws MalformedChallengeException {
        super.processChallenge(header);
        this.complete = true;
    } 
 
    /** 
     * Tests if the Basic authentication process has been completed. 
     *  
     * @return <tt>true</tt> if Basic authorization has been processed, 
     *   <tt>false</tt> otherwise. 
     */ 
    public boolean isComplete() { 
        return this.complete;
    } 
 
    /** 
     * Returns <tt>false</tt>. Basic authentication scheme is request based. 
     *  
     * @return <tt>false</tt>. 
     */ 
    public boolean isConnectionBased() { 
        return false;     
    } 
 
    /** 
     * Produces basic authorization header for the given set of {@link Credentials}. 
     *  
     * @param credentials The set of credentials to be used for athentication 
     * @param request The request being authenticated 
     * @throws org.apache.http.auth.InvalidCredentialsException if authentication credentials 
     *         are not valid or not applicable for this authentication scheme 
     * @throws AuthenticationException if authorization string cannot  
     *   be generated due to an authentication failure 
     *  
     * @return a basic authorization string 
     */ 
    public Header authenticate(
            final Credentials credentials, 
            final HttpRequest request) throws AuthenticationException {
 
        if (credentials == null) {
            throw new IllegalArgumentException("Credentials may not be null");
        } 
        if (request == null) {
            throw new IllegalArgumentException("HTTP request may not be null");
        } 
         
        String charset = AuthParams.getCredentialCharset(request.getParams());
        return authenticate(credentials, charset, isProxy());
    } 
     
    /** 
     * Returns a basic <tt>Authorization</tt> header value for the given  
     * {@link Credentials} and charset. 
     *  
     * @param credentials The credentials to encode. 
     * @param charset The charset to use for encoding the credentials 
     *  
     * @return a basic authorization header 
     */ 
    public static Header authenticate(
            final Credentials credentials, 
            final String charset, 
            boolean proxy) {
        if (credentials == null) {
            throw new IllegalArgumentException("Credentials may not be null"); 
        } 
        if (charset == null) {
            throw new IllegalArgumentException("charset may not be null");
        } 
 
        StringBuilder tmp = new StringBuilder();
        tmp.append(credentials.getUserPrincipal().getName());
        tmp.append(":");
        tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
 
        byte[] base64password = Base64.encodeBase64(
                EncodingUtils.getBytes(tmp.toString(), charset));
         
        CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (proxy) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else { 
            buffer.append(AUTH.WWW_AUTH_RESP);
        } 
        buffer.append(": Basic ");
        buffer.append(base64password, 0, base64password.length);
         
        return new BufferedHeader(buffer);
    } 
     
}