List of usage examples for org.apache.http.util Args notNull
public static <T> T notNull(T t, String str)
From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java
/** * @since 4.4//from w w w.j a v a2s .c o m */ public SSLConnectionSocketFactory(final javax.net.ssl.SSLSocketFactory socketfactory, final String[] supportedProtocols, final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier) { this.socketfactory = Args.notNull(socketfactory, "SSL socket factory"); this.supportedProtocols = supportedProtocols; this.supportedCipherSuites = supportedCipherSuites; this.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier : getDefaultHostnameVerifier(); }
From source file:org.apache.http.conn.util.PublicSuffixMatcherLoader.java
public static PublicSuffixMatcher load(final URL url) throws IOException { Args.notNull(url, "URL"); final InputStream in = url.openStream(); try {/* w ww .j a v a 2 s.c o m*/ return load(in); } finally { in.close(); } }
From source file:org.apache.http.conn.util.PublicSuffixMatcherLoader.java
public static PublicSuffixMatcher load(final File file) throws IOException { Args.notNull(file, "File"); final InputStream in = new FileInputStream(file); try {//w ww .j a v a 2 s . c o m return load(in); } finally { in.close(); } }
From source file:org.apache.http.impl.auth.BasicScheme.java
/** * Produces basic authorization header for the given set of {@link Credentials}. * * @param credentials The set of credentials to be used for authentication * @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/* www . j av a2 s . c o m*/ */ @Override public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException { Args.notNull(credentials, "Credentials"); Args.notNull(request, "HTTP request"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final byte[] base64password = base64codec .encode(EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request))); final CharArrayBuffer buffer = new CharArrayBuffer(32); if (isProxy()) { 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); }
From source file:org.apache.http.impl.auth.BasicScheme.java
/** * Returns a basic <tt>Authorization</tt> header value for the given * {@link Credentials} and charset./*from w ww . j av a2 s. c o m*/ * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization header * * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}. */ @Deprecated public static Header authenticate(final Credentials credentials, final String charset, final boolean proxy) { Args.notNull(credentials, "Credentials"); Args.notNull(charset, "charset"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final byte[] base64password = Base64.encodeBase64(EncodingUtils.getBytes(tmp.toString(), charset), false); final 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); }
From source file:org.apache.http.impl.auth.GGSSchemeBase.java
@Override public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException { Args.notNull(request, "HTTP request"); switch (state) { case UNINITIATED: throw new AuthenticationException(getSchemeName() + " authentication has not been initiated"); case FAILED:/*from w w w . ja v a2s.c o m*/ throw new AuthenticationException(getSchemeName() + " authentication has failed"); case CHALLENGE_RECEIVED: try { final HttpRoute route = (HttpRoute) context.getAttribute(HttpClientContext.HTTP_ROUTE); if (route == null) { throw new AuthenticationException("Connection route is not available"); } HttpHost host; if (isProxy()) { host = route.getProxyHost(); if (host == null) { host = route.getTargetHost(); } } else { host = route.getTargetHost(); } final String authServer; if (!this.stripPort && host.getPort() > 0) { authServer = host.toHostString(); } else { authServer = host.getHostName(); } if (log.isDebugEnabled()) { log.debug("init " + authServer); } token = generateToken(token, authServer); state = State.TOKEN_GENERATED; } catch (final GSSException gsse) { state = State.FAILED; if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED) { throw new InvalidCredentialsException(gsse.getMessage(), gsse); } if (gsse.getMajor() == GSSException.NO_CRED) { throw new InvalidCredentialsException(gsse.getMessage(), gsse); } if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN || gsse.getMajor() == GSSException.OLD_TOKEN) { throw new AuthenticationException(gsse.getMessage(), gsse); } // other error throw new AuthenticationException(gsse.getMessage()); } case TOKEN_GENERATED: final String tokenstr = new String(base64codec.encode(token)); if (log.isDebugEnabled()) { log.debug("Sending response '" + tokenstr + "' back to the auth server"); } final CharArrayBuffer buffer = new CharArrayBuffer(32); if (isProxy()) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Negotiate "); buffer.append(tokenstr); return new BufferedHeader(buffer); default: throw new IllegalStateException("Illegal state: " + state); } }
From source file:org.apache.http.impl.auth.NegotiateScheme.java
/** * Returns the authentication parameter with the given name, if available. * * <p>There are no valid parameters for Negotiate authentication so this * method always returns <tt>null</tt>.</p> * * @param name The name of the parameter to be returned * * @return the parameter with the given name */// w w w . j a va 2 s . c o m public String getParameter(final String name) { Args.notNull(name, "Parameter name"); return null; }
From source file:org.apache.http.impl.client.AbstractHttpClient.java
@Override protected final CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request, final HttpContext context) throws IOException, ClientProtocolException { Args.notNull(request, "HTTP request"); // a null target may be acceptable, this depends on the route planner // a null context is acceptable, default context created below HttpContext execContext = null; RequestDirector director = null;/* www. java 2 s . c o m*/ HttpRoutePlanner routePlanner = null; ConnectionBackoffStrategy connectionBackoffStrategy = null; BackoffManager backoffManager = null; // Initialize the request execution context making copies of // all shared objects that are potentially threading unsafe. synchronized (this) { final HttpContext defaultContext = createHttpContext(); if (context == null) { execContext = defaultContext; } else { execContext = new DefaultedHttpContext(context, defaultContext); } final HttpParams params = determineParams(request); final RequestConfig config = HttpClientParamConfig.getRequestConfig(params); execContext.setAttribute(ClientContext.REQUEST_CONFIG, config); // Create a director for this request director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(), getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(), getProtocolProcessor(), getHttpRequestRetryHandler(), getRedirectStrategy(), getTargetAuthenticationStrategy(), getProxyAuthenticationStrategy(), getUserTokenHandler(), params); routePlanner = getRoutePlanner(); connectionBackoffStrategy = getConnectionBackoffStrategy(); backoffManager = getBackoffManager(); } try { if (connectionBackoffStrategy != null && backoffManager != null) { final HttpHost targetForRoute = (target != null) ? target : (HttpHost) determineParams(request).getParameter(ClientPNames.DEFAULT_HOST); final HttpRoute route = routePlanner.determineRoute(targetForRoute, request, execContext); final CloseableHttpResponse out; try { out = CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext)); } catch (final RuntimeException re) { if (connectionBackoffStrategy.shouldBackoff(re)) { backoffManager.backOff(route); } throw re; } catch (final Exception e) { if (connectionBackoffStrategy.shouldBackoff(e)) { backoffManager.backOff(route); } if (e instanceof HttpException) { throw (HttpException) e; } if (e instanceof IOException) { throw (IOException) e; } throw new UndeclaredThrowableException(e); } if (connectionBackoffStrategy.shouldBackoff(out)) { backoffManager.backOff(route); } else { backoffManager.probe(route); } return out; } else { return CloseableHttpResponseProxy.newProxy(director.execute(target, request, execContext)); } } catch (final HttpException httpException) { throw new ClientProtocolException(httpException); } }
From source file:org.apache.http.impl.client.AuthenticationStrategyAdaptor.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 Queue<AuthOption> options = new LinkedList<AuthOption>(); final CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); if (credsProvider == null) { this.log.debug("Credentials provider not set in the context"); return options; }//from w ww .j a v a 2 s. c o m final AuthScheme authScheme; try { authScheme = this.handler.selectScheme(challenges, response, context); } catch (final AuthenticationException ex) { if (this.log.isWarnEnabled()) { this.log.warn(ex.getMessage(), ex); } return options; } final String id = authScheme.getSchemeName(); final Header challenge = challenges.get(id.toLowerCase(Locale.US)); 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)); } return options; }
From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java
public boolean isAuthenticationRequested(final HttpHost authhost, final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); final int status = response.getStatusLine().getStatusCode(); return status == this.challengeCode; }