Example usage for org.apache.http.util Args notNull

List of usage examples for org.apache.http.util Args notNull

Introduction

In this page you can find the example usage for org.apache.http.util Args notNull.

Prototype

public static <T> T notNull(T t, String str) 

Source Link

Usage

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public Map<String, Header> getChallenges(final HttpHost authhost, final HttpResponse response,
        final HttpContext context) throws MalformedChallengeException {
    Args.notNull(response, "HTTP response");
    final Header[] headers = response.getHeaders(this.headerName);
    final Map<String, Header> map = new HashMap<String, Header>(headers.length);
    for (final Header header : headers) {
        final CharArrayBuffer buffer;
        int pos;//from www.ja va 2  s . c om
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            pos = ((FormattedHeader) header).getValuePos();
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedChallengeException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            pos = 0;
        }
        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int beginIndex = pos;
        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int endIndex = pos;
        final String s = buffer.substring(beginIndex, endIndex);
        map.put(s.toLowerCase(Locale.US), header);
    }
    return map;
}

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public Queue<AuthOption> select(final Map<String, Header> challenges, final HttpHost authhost,
        final HttpResponse response, final HttpContext context) throws MalformedChallengeException {
    Args.notNull(challenges, "Map of auth challenges");
    Args.notNull(authhost, "Host");
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");
    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final Queue<AuthOption> options = new LinkedList<AuthOption>();
    final Lookup<AuthSchemeProvider> registry = clientContext.getAuthSchemeRegistry();
    if (registry == null) {
        this.log.debug("Auth scheme registry not set in the context");
        return options;
    }/*w ww  .  j  ava  2 s .c o  m*/
    final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
    if (credsProvider == null) {
        this.log.debug("Credentials provider not set in the context");
        return options;
    }
    final RequestConfig config = clientContext.getRequestConfig();
    Collection<String> authPrefs = getPreferredAuthSchemes(config);
    if (authPrefs == null) {
        authPrefs = DEFAULT_SCHEME_PRIORITY;
    }
    if (this.log.isDebugEnabled()) {
        this.log.debug("Authentication schemes in the order of preference: " + authPrefs);
    }

    for (final String id : authPrefs) {
        final Header challenge = challenges.get(id.toLowerCase(Locale.US));
        if (challenge != null) {
            final AuthSchemeProvider authSchemeProvider = registry.lookup(id);
            if (authSchemeProvider == null) {
                if (this.log.isWarnEnabled()) {
                    this.log.warn("Authentication scheme " + id + " not supported");
                    // Try again
                }
                continue;
            }
            final AuthScheme authScheme = authSchemeProvider.create(context);
            authScheme.processChallenge(challenge);

            final AuthScope authScope = new AuthScope(authhost.getHostName(), authhost.getPort(),
                    authScheme.getRealm(), authScheme.getSchemeName());

            final Credentials credentials = credsProvider.getCredentials(authScope);
            if (credentials != null) {
                options.add(new AuthOption(authScheme, credentials));
            }
        } else {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Challenge for " + id + " authentication scheme not available");
                // Try again
            }
        }
    }
    return options;
}

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
        AuthCache authCache = clientContext.getAuthCache();
        if (authCache == null) {
            authCache = new BasicAuthCache();
            clientContext.setAuthCache(authCache);
        }/*from w  ww . j av  a 2 s . com*/
        if (this.log.isDebugEnabled()) {
            this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost);
        }
        authCache.put(authhost, authScheme);
    }
}

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public void authFailed(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Clearing cached auth scheme for " + authhost);
        }/*from   w  w  w.j  ava2s .c  o m*/
        authCache.remove(authhost);
    }
}

From source file:org.apache.http.impl.client.AutoRetryHttpClient.java

public AutoRetryHttpClient(final HttpClient client, final ServiceUnavailableRetryStrategy retryStrategy) {
    super();//  w ww  .  j  a  va  2  s. co  m
    Args.notNull(client, "HttpClient");
    Args.notNull(retryStrategy, "ServiceUnavailableRetryStrategy");
    this.backend = client;
    this.retryStrategy = retryStrategy;
}

From source file:org.apache.http.impl.client.BasicAuthCache.java

@Override
public void put(final HttpHost host, final AuthScheme authScheme) {
    Args.notNull(host, "HTTP host");
    if (authScheme == null) {
        return;//w  ww  . j  av  a2s  .  c om
    }
    if (authScheme instanceof Serializable) {
        try {
            final ByteArrayOutputStream buf = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(buf);
            out.writeObject(authScheme);
            out.close();
            this.map.put(getKey(host), buf.toByteArray());
        } catch (IOException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Unexpected I/O error while serializing auth scheme", ex);
            }
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Auth scheme " + authScheme.getClass() + " is not serializable");
        }
    }
}

From source file:org.apache.http.impl.client.BasicAuthCache.java

@Override
public AuthScheme get(final HttpHost host) {
    Args.notNull(host, "HTTP host");
    final byte[] bytes = this.map.get(getKey(host));
    if (bytes != null) {
        try {//from  ww  w .j  av a2  s  .  c o m
            final ByteArrayInputStream buf = new ByteArrayInputStream(bytes);
            final ObjectInputStream in = new ObjectInputStream(buf);
            final AuthScheme authScheme = (AuthScheme) in.readObject();
            in.close();
            return authScheme;
        } catch (IOException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Unexpected I/O error while de-serializing auth scheme", ex);
            }
            return null;
        } catch (ClassNotFoundException ex) {
            if (log.isWarnEnabled()) {
                log.warn("Unexpected error while de-serializing auth scheme", ex);
            }
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.apache.http.impl.client.BasicAuthCache.java

@Override
public void remove(final HttpHost host) {
    Args.notNull(host, "HTTP host");
    this.map.remove(getKey(host));
}

From source file:org.apache.http.impl.client.cache.CachingExec.java

public CachingExec(final ClientExecChain backend, final HttpCache cache, final CacheConfig config,
        final AsynchronousValidator asynchRevalidator) {
    super();/*from  www  .  j  av a 2s .c  o  m*/
    Args.notNull(backend, "HTTP backend");
    Args.notNull(cache, "HttpCache");
    this.cacheConfig = config != null ? config : CacheConfig.DEFAULT;
    this.backend = backend;
    this.responseCache = cache;
    this.validityPolicy = new CacheValidityPolicy();
    this.responseGenerator = new CachedHttpResponseGenerator(this.validityPolicy);
    this.cacheableRequestPolicy = new CacheableRequestPolicy();
    this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy, config);
    this.conditionalRequestBuilder = new ConditionalRequestBuilder();
    this.responseCompliance = new ResponseProtocolCompliance();
    this.requestCompliance = new RequestProtocolCompliance(config.isWeakETagOnPutDeleteAllowed());
    this.responseCachingPolicy = new ResponseCachingPolicy(this.cacheConfig.getMaxObjectSize(),
            this.cacheConfig.isSharedCache(), this.cacheConfig.isNeverCacheHTTP10ResponsesWithQuery(),
            this.cacheConfig.is303CachingEnabled());
    this.asynchRevalidator = asynchRevalidator;
}

From source file:org.apache.http.impl.client.cache.CachingHttpAsyncClient.java

CachingHttpAsyncClient(final HttpAsyncClient client, final HttpCache cache, final CacheConfig config) {
    super();/*from  w  w  w.  j  a  v a  2  s  .c o  m*/
    Args.notNull(client, "HttpClient");
    Args.notNull(cache, "HttpCache");
    Args.notNull(config, "CacheConfig");
    this.maxObjectSizeBytes = config.getMaxObjectSize();
    this.sharedCache = config.isSharedCache();
    this.backend = client;
    this.responseCache = cache;
    this.validityPolicy = new CacheValidityPolicy();
    this.responseCachingPolicy = new ResponseCachingPolicy(this.maxObjectSizeBytes, this.sharedCache, false,
            config.is303CachingEnabled());
    this.responseGenerator = new CachedHttpResponseGenerator(this.validityPolicy);
    this.cacheableRequestPolicy = new CacheableRequestPolicy();
    this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy, config);
    this.conditionalRequestBuilder = new ConditionalRequestBuilder();

    this.responseCompliance = new ResponseProtocolCompliance();
    this.requestCompliance = new RequestProtocolCompliance(config.isWeakETagOnPutDeleteAllowed());

    this.asynchAsyncRevalidator = makeAsynchronousValidator(config);
}