Java Code Examples for org.apache.http.HttpStatus#SC_PROXY_AUTHENTICATION_REQUIRED

The following examples show how to use org.apache.http.HttpStatus#SC_PROXY_AUTHENTICATION_REQUIRED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientHandler.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
private boolean checkStatusCode(final String httpMethod, final URL sourceURL, final HttpResponse response) {
    final int status = response.getStatusLine().getStatusCode();
    if (status == HttpStatus.SC_OK) {
        return true;
    }
    // IVY-1328: some servers return a 204 on a HEAD request
    if (HttpHead.METHOD_NAME.equals(httpMethod) && (status == 204)) {
        return true;
    }

    Message.debug("HTTP response status: " + status + " url=" + sourceURL);
    if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
        Message.warn("Your proxy requires authentication.");
    } else if (String.valueOf(status).startsWith("4")) {
        Message.verbose("CLIENT ERROR: " + response.getStatusLine().getReasonPhrase() + " url=" + sourceURL);
    } else if (String.valueOf(status).startsWith("5")) {
        Message.error("SERVER ERROR: " + response.getStatusLine().getReasonPhrase() + " url=" + sourceURL);
    }
    return false;
}
 
Example 2
Source File: AptProxyFacet.java    From nexus-repository-apt with Eclipse Public License 1.0 5 votes vote down vote up
private void throwProxyExceptionForStatus(HttpResponse httpResponse) {
  final StatusLine status = httpResponse.getStatusLine();
  if (HttpStatus.SC_UNAUTHORIZED == status.getStatusCode() 
      || HttpStatus.SC_PAYMENT_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_INTERNAL_SERVER_ERROR <= status.getStatusCode()) {
    throw new ProxyServiceException(httpResponse);
  }
}
 
Example 3
Source File: AptProxyFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void throwProxyExceptionForStatus(final HttpResponse httpResponse) {
  final StatusLine status = httpResponse.getStatusLine();
  if (HttpStatus.SC_UNAUTHORIZED == status.getStatusCode()
      || HttpStatus.SC_PAYMENT_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_INTERNAL_SERVER_ERROR <= status.getStatusCode()) {
    throw new ProxyServiceException(httpResponse);
  }
}
 
Example 4
Source File: ProxyFacetSupport.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * May throw {@link ProxyServiceException} based on response statuses.
 */
private void mayThrowProxyServiceException(final HttpResponse httpResponse) {
  final StatusLine status = httpResponse.getStatusLine();
  if (HttpStatus.SC_UNAUTHORIZED == status.getStatusCode()
      || HttpStatus.SC_PAYMENT_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED == status.getStatusCode()
      || HttpStatus.SC_INTERNAL_SERVER_ERROR <= status.getStatusCode()) {
    throw new ProxyServiceException(httpResponse);
  }
}
 
Example 5
Source File: ResponseProtocolCompliance.java    From apigee-android-sdk with Apache License 2.0 5 votes vote down vote up
private void authenticationRequiredDidNotHaveAProxyAuthenticationHeader(
		HttpRequest request, HttpResponse response)
		throws ClientProtocolException {
	if (response.getStatusLine().getStatusCode() != HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED)
		return;

	if (response.getFirstHeader(HeaderConstants.PROXY_AUTHENTICATE) == null)
		throw new ClientProtocolException(
				"407 Response did not contain a Proxy-Authentication header");
}
 
Example 6
Source File: HttpResponseExceptionMappingService.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
public BackgroundException map(final Throwable failure, final StringBuilder buffer, final int statusCode) {
    switch(statusCode) {
        case HttpStatus.SC_UNAUTHORIZED:
            return new LoginFailureException(buffer.toString(), failure);
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
            return new ProxyLoginFailureException(buffer.toString(), failure);
        case HttpStatus.SC_FORBIDDEN:
        case HttpStatus.SC_NOT_ACCEPTABLE:
            return new AccessDeniedException(buffer.toString(), failure);
        case HttpStatus.SC_CONFLICT:
            return new ConflictException(buffer.toString(), failure);
        case HttpStatus.SC_NOT_FOUND:
        case HttpStatus.SC_GONE:
        case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
            return new NotfoundException(buffer.toString(), failure);
        case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
        case HttpStatus.SC_INSUFFICIENT_STORAGE:
        case HttpStatus.SC_PAYMENT_REQUIRED:
            return new QuotaException(buffer.toString(), failure);
        case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_REQUEST_URI_TOO_LONG:
        case HttpStatus.SC_METHOD_NOT_ALLOWED:
        case HttpStatus.SC_NOT_IMPLEMENTED:
            return new InteroperabilityException(buffer.toString(), failure);
        case HttpStatus.SC_REQUEST_TIMEOUT:
        case HttpStatus.SC_GATEWAY_TIMEOUT:
            return new ConnectionTimeoutException(buffer.toString(), failure);
        case HttpStatus.SC_LOCKED:
            return new LockedException(buffer.toString(), failure);
        case HttpStatus.SC_BAD_GATEWAY:
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        case HttpStatus.SC_SERVICE_UNAVAILABLE:
        case 429:
            // Too Many Requests. Rate limiting
        case 509:
            // Bandwidth Limit Exceeded
            return new RetriableAccessDeniedException(buffer.toString(), failure);
        default:
            return new InteroperabilityException(buffer.toString(), failure);
    }
}
 
Example 7
Source File: ExceptionUtils.java    From flink-crawler with Apache License 2.0 4 votes vote down vote up
public static FetchStatus mapHttpStatusToFetchStatus(int httpStatus) {
    switch (httpStatus) {
        case HttpStatus.SC_OK:
            return FetchStatus.FETCHED;

        case HttpStatus.SC_FORBIDDEN:
            return FetchStatus.HTTP_FORBIDDEN;

        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
            return FetchStatus.HTTP_UNAUTHORIZED;

        case HttpStatus.SC_NOT_FOUND:
            return FetchStatus.HTTP_NOT_FOUND;

        case HttpStatus.SC_GONE:
            return FetchStatus.HTTP_GONE;

        case HttpStatus.SC_TEMPORARY_REDIRECT:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
            return FetchStatus.HTTP_TOO_MANY_REDIRECTS;

        case HttpStatus.SC_MOVED_PERMANENTLY:
            return FetchStatus.HTTP_MOVED_PERMANENTLY;

        default:
            if (httpStatus < 300) {
                LOGGER.warn("Invalid HTTP status for exception: " + httpStatus);
                return FetchStatus.HTTP_SERVER_ERROR;
            } else if (httpStatus < 400) {
                return FetchStatus.HTTP_REDIRECTION_ERROR;
            } else if (httpStatus < 500) {
                return FetchStatus.HTTP_CLIENT_ERROR;
            } else if (httpStatus < 600) {
                return FetchStatus.HTTP_SERVER_ERROR;
            } else {
                LOGGER.warn("Unknown status: " + httpStatus);
                return FetchStatus.HTTP_SERVER_ERROR;
            }
    }
}
 
Example 8
Source File: AccessTokenResponseTest.java    From here-aaa-java-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void test_nonJSON_response_from_HA() throws IOException, HttpException {
    OAuth1Signer mockOauth1Signer = new OAuth1Signer(accessKeyId, accessKeySecret);
    String body = createMockHttpResponseBody();

    ClientCredentialsProvider mockClientCredentialsProvider = Mockito.mock(ClientCredentialsProvider.class);
    Mockito.doReturn(Clock.SYSTEM)
            .when(mockClientCredentialsProvider).getClock();
    Mockito.doReturn("https://www.example.com/oauth2/token")
            .when(mockClientCredentialsProvider).getTokenEndpointUrl();
    Mockito.doReturn(mockOauth1Signer)
            .when(mockClientCredentialsProvider).getClientAuthorizer();
    Mockito.doReturn(HttpConstants.HttpMethods.POST)
            .when(mockClientCredentialsProvider).getHttpMethod();

    HttpProvider.HttpRequest mockHttpRequest = Mockito.mock(HttpProvider.HttpRequest.class);
    Mockito.doNothing()
            .when(mockHttpRequest).addHeader(Mockito.anyString(), Mockito.anyString());

    HttpProvider mockHttpProvider = Mockito.mock(HttpProvider.class);
    Mockito.when(mockHttpProvider.getRequest(Mockito.any(HttpProvider.HttpRequestAuthorizer.class), anyString(),
            anyString(), Mockito.any(Map.class)))
            .thenReturn(mockHttpRequest);
    final HttpProvider.HttpResponse mockHttpResponse = new HttpProvider.HttpResponse() {
        @Override
        public int getStatusCode() {
            return HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED;
        }

        @Override
        public long getContentLength() {
            return body.getBytes(StandardCharsets.UTF_8).length;
        }

        @Override
        public Map<String, List<String>> getHeaders() {
            Map<String, List<String>> responseHeader = new HashMap<String, List<String>>();
            List<String> responseTypes = new ArrayList<String>();
            responseTypes.add("text/html");
            responseHeader.put(HttpConstants.CONTENT_TYPE, responseTypes);
            return responseHeader;
        }

        @Override
        public InputStream getResponseBody() {
            byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
            return new ByteArrayInputStream(bytes);
        }
    };
    Mockito.when(mockHttpProvider.execute(Mockito.any())).thenReturn(mockHttpResponse);

    AccessTokenRequest accessTokenRequest = new ClientCredentialsGrantRequest();
    accessTokenRequest.setAdditionalHeaders(Collections.singletonMap("testKey", "testValue"));
    accessTokenRequest.setExpiresIn(1L);

    TokenEndpoint tokenEndpoint = HereAccount.getTokenEndpoint(mockHttpProvider, mockClientCredentialsProvider);

    // expect the request to throw an exception, then validate the exception contents
    try {
        tokenEndpoint.requestToken(accessTokenRequest);
    } catch (AccessTokenException ate) {
        assertEquals("Expected proxyAuthenticationRequired error code",
                HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, ate.getStatusCode());
        assertEquals("Expected proxyAuthenticationRequired error code",
                HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, ate.getErrorResponse().getHttpStatus().intValue());
        assertEquals("Expected text in error response message field", body, ate.getErrorResponse().getMessage());
        return;
    }
    fail("Non-JSON response exception not thrown");
}
 
Example 9
Source File: HttpRequestHandler.java    From Asqatasun with GNU Affero General Public License v3.0 4 votes vote down vote up
private int computeStatus(int status) {
    switch (status) { 
        case HttpStatus.SC_FORBIDDEN:
        case HttpStatus.SC_METHOD_NOT_ALLOWED:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_PAYMENT_REQUIRED:
        case HttpStatus.SC_NOT_FOUND:
        case HttpStatus.SC_NOT_ACCEPTABLE:
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
        case HttpStatus.SC_REQUEST_TIMEOUT:
        case HttpStatus.SC_CONFLICT:
        case HttpStatus.SC_GONE:
        case HttpStatus.SC_LENGTH_REQUIRED:
        case HttpStatus.SC_PRECONDITION_FAILED:
        case HttpStatus.SC_REQUEST_TOO_LONG:
        case HttpStatus.SC_REQUEST_URI_TOO_LONG:
        case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
        case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
        case HttpStatus.SC_EXPECTATION_FAILED:
        case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
        case HttpStatus.SC_METHOD_FAILURE:
        case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        case HttpStatus.SC_LOCKED:
        case HttpStatus.SC_FAILED_DEPENDENCY:
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        case HttpStatus.SC_NOT_IMPLEMENTED:
        case HttpStatus.SC_BAD_GATEWAY:
        case HttpStatus.SC_SERVICE_UNAVAILABLE:
        case HttpStatus.SC_GATEWAY_TIMEOUT:
        case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
        case HttpStatus.SC_INSUFFICIENT_STORAGE:
            return 0;
        case HttpStatus.SC_CONTINUE:
        case HttpStatus.SC_SWITCHING_PROTOCOLS:
        case HttpStatus.SC_PROCESSING:
        case HttpStatus.SC_OK:
        case HttpStatus.SC_CREATED:
        case HttpStatus.SC_ACCEPTED:
        case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
        case HttpStatus.SC_NO_CONTENT:
        case HttpStatus.SC_RESET_CONTENT:
        case HttpStatus.SC_PARTIAL_CONTENT:
        case HttpStatus.SC_MULTI_STATUS:
        case HttpStatus.SC_MULTIPLE_CHOICES:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_NOT_MODIFIED:
        case HttpStatus.SC_USE_PROXY:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return 1;
        default : 
            return 1;
    }
}