mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/AuthSchemeBase.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/AuthSchemeBase.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.auth;
    1.31 +
    1.32 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.33 +
    1.34 +import ch.boye.httpclientandroidlib.FormattedHeader;
    1.35 +import ch.boye.httpclientandroidlib.Header;
    1.36 +import ch.boye.httpclientandroidlib.HttpRequest;
    1.37 +import ch.boye.httpclientandroidlib.auth.AUTH;
    1.38 +import ch.boye.httpclientandroidlib.auth.AuthenticationException;
    1.39 +import ch.boye.httpclientandroidlib.auth.ContextAwareAuthScheme;
    1.40 +import ch.boye.httpclientandroidlib.auth.Credentials;
    1.41 +import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
    1.42 +import ch.boye.httpclientandroidlib.protocol.HTTP;
    1.43 +import ch.boye.httpclientandroidlib.protocol.HttpContext;
    1.44 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.45 +
    1.46 +/**
    1.47 + * Abstract authentication scheme class that serves as a basis
    1.48 + * for all authentication schemes supported by HttpClient. This class
    1.49 + * defines the generic way of parsing an authentication challenge. It
    1.50 + * does not make any assumptions regarding the format of the challenge
    1.51 + * nor does it impose any specific way of responding to that challenge.
    1.52 + *
    1.53 + *
    1.54 + * @since 4.0
    1.55 + */
    1.56 +@NotThreadSafe // proxy
    1.57 +public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
    1.58 +
    1.59 +    /**
    1.60 +     * Flag whether authenticating against a proxy.
    1.61 +     */
    1.62 +    private boolean proxy;
    1.63 +
    1.64 +    public AuthSchemeBase() {
    1.65 +        super();
    1.66 +    }
    1.67 +
    1.68 +    /**
    1.69 +     * Processes the given challenge token. Some authentication schemes
    1.70 +     * may involve multiple challenge-response exchanges. Such schemes must be able
    1.71 +     * to maintain the state information when dealing with sequential challenges
    1.72 +     *
    1.73 +     * @param header the challenge header
    1.74 +     *
    1.75 +     * @throws MalformedChallengeException is thrown if the authentication challenge
    1.76 +     * is malformed
    1.77 +     */
    1.78 +    public void processChallenge(final Header header) throws MalformedChallengeException {
    1.79 +        if (header == null) {
    1.80 +            throw new IllegalArgumentException("Header may not be null");
    1.81 +        }
    1.82 +        String authheader = header.getName();
    1.83 +        if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
    1.84 +            this.proxy = false;
    1.85 +        } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
    1.86 +            this.proxy = true;
    1.87 +        } else {
    1.88 +            throw new MalformedChallengeException("Unexpected header name: " + authheader);
    1.89 +        }
    1.90 +
    1.91 +        CharArrayBuffer buffer;
    1.92 +        int pos;
    1.93 +        if (header instanceof FormattedHeader) {
    1.94 +            buffer = ((FormattedHeader) header).getBuffer();
    1.95 +            pos = ((FormattedHeader) header).getValuePos();
    1.96 +        } else {
    1.97 +            String s = header.getValue();
    1.98 +            if (s == null) {
    1.99 +                throw new MalformedChallengeException("Header value is null");
   1.100 +            }
   1.101 +            buffer = new CharArrayBuffer(s.length());
   1.102 +            buffer.append(s);
   1.103 +            pos = 0;
   1.104 +        }
   1.105 +        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
   1.106 +            pos++;
   1.107 +        }
   1.108 +        int beginIndex = pos;
   1.109 +        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
   1.110 +            pos++;
   1.111 +        }
   1.112 +        int endIndex = pos;
   1.113 +        String s = buffer.substring(beginIndex, endIndex);
   1.114 +        if (!s.equalsIgnoreCase(getSchemeName())) {
   1.115 +            throw new MalformedChallengeException("Invalid scheme identifier: " + s);
   1.116 +        }
   1.117 +
   1.118 +        parseChallenge(buffer, pos, buffer.length());
   1.119 +    }
   1.120 +
   1.121 +
   1.122 +    @SuppressWarnings("deprecation")
   1.123 +    public Header authenticate(
   1.124 +            final Credentials credentials,
   1.125 +            final HttpRequest request,
   1.126 +            final HttpContext context) throws AuthenticationException {
   1.127 +        return authenticate(credentials, request);
   1.128 +    }
   1.129 +
   1.130 +    protected abstract void parseChallenge(
   1.131 +            CharArrayBuffer buffer, int beginIndex, int endIndex) throws MalformedChallengeException;
   1.132 +
   1.133 +    /**
   1.134 +     * Returns <code>true</code> if authenticating against a proxy, <code>false</code>
   1.135 +     * otherwise.
   1.136 +     *
   1.137 +     * @return <code>true</code> if authenticating against a proxy, <code>false</code>
   1.138 +     * otherwise
   1.139 +     */
   1.140 +    public boolean isProxy() {
   1.141 +        return this.proxy;
   1.142 +    }
   1.143 +
   1.144 +    @Override
   1.145 +    public String toString() {
   1.146 +        return getSchemeName();
   1.147 +    }
   1.148 +
   1.149 +}

mercurial