List of usage examples for org.apache.http.impl.auth BasicScheme processChallenge
@Override public void processChallenge(final Header header) throws MalformedChallengeException
From source file:org.siddhiesb.transport.http.conn.ProxyAuthenticator.java
public void authenticatePreemptively(final HttpRequest request, final HttpContext context) throws ProtocolException { BasicScheme basicScheme = new BasicScheme(); basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=\"proxy\"")); Header authresp = basicScheme.authenticate(proxycreds, request, context); request.addHeader(authresp);/* w w w . j a v a 2 s. com*/ }
From source file:org.ops4j.pax.url.mvn.internal.wagon.ConfigurableHttpWagon.java
@Override protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException { setHeaders(httpMethod);/* ww w . j a v a 2 s . c o m*/ String userAgent = getUserAgent(httpMethod); if (userAgent != null) { httpMethod.setHeader(HTTP.USER_AGENT, userAgent); } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); // WAGON-273: default the cookie-policy to browser compatible requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); Repository repo = getRepository(); ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost()); if (proxyInfo != null) { HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); requestConfigBuilder.setProxy(proxy); } HttpMethodConfiguration config = getHttpConfiguration() == null ? null : getHttpConfiguration().getMethodConfiguration(httpMethod); if (config != null) { copyConfig(config, requestConfigBuilder); } else { requestConfigBuilder.setSocketTimeout(getReadTimeout()); requestConfigBuilder.setConnectTimeout(getTimeout()); } getLocalContext().setRequestConfig(requestConfigBuilder.build()); if (config != null && config.isUsePreemptive()) { HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (getCredentialsProvider().getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); getAuthCache().put(targetHost, targetAuth); } } if (proxyInfo != null) { if (proxyInfo.getHost() != null) { HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost); String proxyUsername = proxyInfo.getUserName(); String proxyPassword = proxyInfo.getPassword(); String proxyNtlmHost = proxyInfo.getNtlmHost(); String proxyNtlmDomain = proxyInfo.getNtlmDomain(); if (proxyUsername != null && proxyPassword != null) { Credentials creds; if (proxyNtlmHost != null || proxyNtlmDomain != null) { creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain); } else { creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); } getCredentialsProvider().setCredentials(proxyScope, creds); BasicScheme proxyAuth = new BasicScheme(); proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive")); getAuthCache().put(proxyHost, proxyAuth); } } } return client.execute(httpMethod, getLocalContext()); }
From source file:org.glowroot.central.SyntheticMonitorService.java
private HttpClientContext getHttpClientContext() throws Exception { HttpProxyConfig httpProxyConfig = configRepository.getHttpProxyConfig(); if (httpProxyConfig.host().isEmpty() || httpProxyConfig.username().isEmpty()) { return HttpClientContext.create(); }//from w ww . j a v a 2 s . co m // perform preemptive proxy authentication int proxyPort = MoreObjects.firstNonNull(httpProxyConfig.port(), 80); HttpHost proxyHost = new HttpHost(httpProxyConfig.host(), proxyPort); BasicScheme basicScheme = new BasicScheme(); basicScheme.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=")); BasicAuthCache authCache = new BasicAuthCache(); authCache.put(proxyHost, basicScheme); String password = httpProxyConfig.encryptedPassword(); if (!password.isEmpty()) { password = Encryption.decrypt(password, configRepository.getLazySecretKey()); } CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyHost), new UsernamePasswordCredentials(httpProxyConfig.username(), password)); HttpClientContext context = HttpClientContext.create(); context.setAuthCache(authCache); context.setCredentialsProvider(credentialsProvider); return context; }
From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagonFixed.java
private void put(int wait, Resource resource, File source, HttpEntity httpEntity, String url) throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException { //Parent directories need to be created before posting try {/*w ww. j a v a2 s . co m*/ mkdirs(PathUtils.dirname(resource.getName())); } catch (HttpException he) { fireTransferError(resource, he, TransferEvent.REQUEST_GET); } catch (IOException e) { fireTransferError(resource, e, TransferEvent.REQUEST_GET); } // preemptive for put // TODO: is it a good idea, though? 'Expect-continue' handshake would serve much better Repository repo = getRepository(); HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (credentialsProvider.getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); try { targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); authCache.put(targetHost, targetAuth); } catch (MalformedChallengeException ignore) { // ignore } } HttpPut putMethod = new HttpPut(url); firePutStarted(resource, source); try { putMethod.setEntity(httpEntity); CloseableHttpResponse response = execute(putMethod); try { int statusCode = response.getStatusLine().getStatusCode(); String reasonPhrase = ", ReasonPhrase: " + response.getStatusLine().getReasonPhrase() + "."; fireTransferDebug(url + " - Status code: " + statusCode + reasonPhrase); // Check that we didn't run out of retries. switch (statusCode) { // Success Codes case HttpStatus.SC_OK: // 200 case HttpStatus.SC_CREATED: // 201 case HttpStatus.SC_ACCEPTED: // 202 case HttpStatus.SC_NO_CONTENT: // 204 break; // handle all redirect even if http specs says " the user agent MUST NOT automatically redirect // the request unless it can be confirmed by the user" case HttpStatus.SC_MOVED_PERMANENTLY: // 301 case HttpStatus.SC_MOVED_TEMPORARILY: // 302 case HttpStatus.SC_SEE_OTHER: // 303 put(resource, source, httpEntity, calculateRelocatedUrl(response)); return; case HttpStatus.SC_FORBIDDEN: fireSessionConnectionRefused(); throw new AuthorizationException("Access denied to: " + url + reasonPhrase); case HttpStatus.SC_NOT_FOUND: throw new ResourceDoesNotExistException("File: " + url + " does not exist" + reasonPhrase); case SC_TOO_MANY_REQUESTS: put(backoff(wait, url), resource, source, httpEntity, url); break; //add more entries here default: TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url + ". Return code is: " + statusCode + reasonPhrase); fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw e; } firePutCompleted(resource, source); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } catch (IOException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } catch (HttpException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } catch (InterruptedException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } }
From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.java
protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException { setHeaders(httpMethod);/*from ww w. j a v a2 s .c o m*/ String userAgent = getUserAgent(httpMethod); if (userAgent != null) { httpMethod.setHeader(HTTP.USER_AGENT, userAgent); } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); // WAGON-273: default the cookie-policy to browser compatible requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); Repository repo = getRepository(); ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost()); if (proxyInfo != null) { HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); requestConfigBuilder.setProxy(proxy); } HttpMethodConfiguration config = httpConfiguration == null ? null : httpConfiguration.getMethodConfiguration(httpMethod); if (config != null) { ConfigurationUtils.copyConfig(config, requestConfigBuilder); } else { requestConfigBuilder.setSocketTimeout(getReadTimeout()); } localContext.setRequestConfig(requestConfigBuilder.build()); if (config != null && config.isUsePreemptive()) { HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (credentialsProvider.getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); authCache.put(targetHost, targetAuth); } } if (proxyInfo != null) { if (proxyInfo.getHost() != null) { HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost); String proxyUsername = proxyInfo.getUserName(); String proxyPassword = proxyInfo.getPassword(); String proxyNtlmHost = proxyInfo.getNtlmHost(); String proxyNtlmDomain = proxyInfo.getNtlmDomain(); if (proxyUsername != null && proxyPassword != null) { Credentials creds; if (proxyNtlmHost != null || proxyNtlmDomain != null) { creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain); } else { creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); } credentialsProvider.setCredentials(proxyScope, creds); BasicScheme proxyAuth = new BasicScheme(); proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive")); authCache.put(proxyHost, proxyAuth); } } } return CLIENT.execute(httpMethod, localContext); }
From source file:org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.java
private void put(int wait, Resource resource, File source, HttpEntity httpEntity, String url) throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException { // preemptive for put // TODO: is it a good idea, though? 'Expect-continue' handshake would serve much better Repository repo = getRepository();// w w w . j a v a 2 s . co m HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (credentialsProvider.getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); try { targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); authCache.put(targetHost, targetAuth); } catch (MalformedChallengeException ignore) { } } //Parent directories need to be created before posting try { mkdirs(PathUtils.dirname(resource.getName())); } catch (HttpException he) { fireTransferError(resource, he, TransferEvent.REQUEST_GET); } HttpPut putMethod = new HttpPut(url); firePutStarted(resource, source); try { putMethod.setEntity(httpEntity); CloseableHttpResponse response = execute(putMethod); try { int statusCode = response.getStatusLine().getStatusCode(); String reasonPhrase = ", ReasonPhrase: " + response.getStatusLine().getReasonPhrase() + "."; fireTransferDebug(url + " - Status code: " + statusCode + reasonPhrase); // Check that we didn't run out of retries. switch (statusCode) { // Success Codes case HttpStatus.SC_OK: // 200 case HttpStatus.SC_CREATED: // 201 case HttpStatus.SC_ACCEPTED: // 202 case HttpStatus.SC_NO_CONTENT: // 204 break; // handle all redirect even if http specs says " the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user" case HttpStatus.SC_MOVED_PERMANENTLY: // 301 case HttpStatus.SC_MOVED_TEMPORARILY: // 302 case HttpStatus.SC_SEE_OTHER: // 303 put(resource, source, httpEntity, calculateRelocatedUrl(response)); return; case HttpStatus.SC_FORBIDDEN: fireSessionConnectionRefused(); throw new AuthorizationException("Access denied to: " + url + reasonPhrase); case HttpStatus.SC_NOT_FOUND: throw new ResourceDoesNotExistException("File: " + url + " does not exist" + reasonPhrase); case SC_TOO_MANY_REQUESTS: put(backoff(wait, url), resource, source, httpEntity, url); break; //add more entries here default: { TransferFailedException e = new TransferFailedException( "Failed to transfer file: " + url + ". Return code is: " + statusCode + reasonPhrase); fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw e; } } firePutCompleted(resource, source); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } catch (IOException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } catch (HttpException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } catch (InterruptedException e) { fireTransferError(resource, e, TransferEvent.REQUEST_PUT); throw new TransferFailedException(e.getMessage(), e); } }
From source file:org.apache.http.impl.auth.TestBasicScheme.java
@Test public void testBasicAuthentication() throws Exception { final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""); final BasicScheme authscheme = new BasicScheme(); authscheme.processChallenge(challenge); final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpContext context = new BasicHttpContext(); final Header authResponse = authscheme.authenticate(creds, request, context); final String expected = "Basic " + EncodingUtils .getAsciiString(Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass"))); Assert.assertEquals(AUTH.WWW_AUTH_RESP, authResponse.getName()); Assert.assertEquals(expected, authResponse.getValue()); Assert.assertEquals("test", authscheme.getRealm()); Assert.assertTrue(authscheme.isComplete()); Assert.assertFalse(authscheme.isConnectionBased()); }
From source file:org.apache.http.impl.auth.TestBasicScheme.java
@Test public void testBasicProxyAuthentication() throws Exception { final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); final Header challenge = new BasicHeader(AUTH.PROXY_AUTH, "Basic realm=\"test\""); final BasicScheme authscheme = new BasicScheme(); authscheme.processChallenge(challenge); final HttpRequest request = new BasicHttpRequest("GET", "/"); final HttpContext context = new BasicHttpContext(); final Header authResponse = authscheme.authenticate(creds, request, context); final String expected = "Basic " + EncodingUtils .getAsciiString(Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass"))); Assert.assertEquals(AUTH.PROXY_AUTH_RESP, authResponse.getName()); Assert.assertEquals(expected, authResponse.getValue()); Assert.assertEquals("test", authscheme.getRealm()); Assert.assertTrue(authscheme.isComplete()); Assert.assertFalse(authscheme.isConnectionBased()); }