Search in sources :

Example 21 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class RegistryEndpointCallerTest method mockResponseException.

private static ResponseException mockResponseException(int statusCode) {
    ResponseException mock = Mockito.mock(ResponseException.class);
    Mockito.when(mock.getStatusCode()).thenReturn(statusCode);
    return mock;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException)

Example 22 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class RegistryEndpointCallerTest method verifyThrowsRegistryUnauthorizedException.

/**
 * Verifies that a response with {@code httpStatusCode} throws {@link
 * RegistryUnauthorizedException}.
 */
private void verifyThrowsRegistryUnauthorizedException(int httpStatusCode) throws IOException, RegistryException {
    ResponseException responseException = mockResponseException(httpStatusCode);
    setUpRegistryResponse(responseException);
    try {
        endpointCaller.call();
        Assert.fail("Call should have failed");
    } catch (RegistryUnauthorizedException ex) {
        Assert.assertEquals("serverUrl/imageName", ex.getImageReference());
        Assert.assertSame(responseException, ex.getCause());
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException)

Example 23 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException.

@Test
public void testHandleHttpResponseException() throws IOException {
    ResponseException mockResponseException = Mockito.mock(ResponseException.class);
    Mockito.when(mockResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message"));
    Mockito.when(mockResponseException.getContent()).thenReturn(JsonTemplateMapper.toUtf8String(emptyErrorResponseTemplate));
    Assert.assertFalse(testBlobChecker.handleHttpResponseException(mockResponseException).isPresent());
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Test(org.junit.Test)

Example 24 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException_notBlobUnknown.

@Test
public void testHandleHttpResponseException_notBlobUnknown() throws IOException {
    ResponseException mockResponseException = Mockito.mock(ResponseException.class);
    Mockito.when(mockResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate();
    Mockito.when(mockResponseException.getContent()).thenReturn(JsonTemplateMapper.toUtf8String(emptyErrorResponseTemplate));
    try {
        testBlobChecker.handleHttpResponseException(mockResponseException);
        Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
    } catch (ResponseException ex) {
        Assert.assertEquals(mockResponseException, ex);
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) Test(org.junit.Test)

Example 25 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class RegistryEndpointCaller method call.

/**
 * Calls the registry endpoint with a certain {@link URL}.
 *
 * @param url the endpoint URL to call
 * @return an object representing the response
 * @throws IOException for most I/O exceptions when making the request
 * @throws RegistryException for known exceptions when interacting with the registry
 */
private T call(URL url) throws IOException, RegistryException {
    String serverUrl = registryEndpointRequestProperties.getServerUrl();
    String imageName = registryEndpointRequestProperties.getImageName();
    Request.Builder requestBuilder = Request.builder().setUserAgent(userAgent).setHttpTimeout(JibSystemProperties.getHttpTimeout()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).setAuthorization(authorization);
    try (Response response = httpClient.call(registryEndpointProvider.getHttpMethod(), url, requestBuilder.build())) {
        return registryEndpointProvider.handleResponse(response);
    } catch (ResponseException ex) {
        // First, see if the endpoint provider handles an exception as an expected response.
        try {
            return registryEndpointProvider.handleHttpResponseException(ex);
        } catch (ResponseException responseException) {
            if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
                // The name or reference was invalid.
                throw newRegistryErrorException(responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
                throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
            } else if (responseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
                if (responseException.requestAuthorizationCleared()) {
                    throw new RegistryCredentialsNotSentException(serverUrl, imageName);
                } else {
                    // Credentials are either missing or wrong.
                    throw new RegistryUnauthorizedException(serverUrl, imageName, responseException);
                }
            } else {
                // Unknown
                throw responseException;
            }
        }
    } catch (IOException ex) {
        logError("I/O error for image [" + serverUrl + "/" + imageName + "]:");
        logError("    " + ex.getClass().getName());
        logError("    " + (ex.getMessage() == null ? "(null exception message)" : ex.getMessage()));
        logErrorIfBrokenPipe(ex);
        if (ex instanceof SSLException) {
            throw new InsecureRegistryException(url, ex);
        }
        throw ex;
    }
}
Also used : Response(com.google.cloud.tools.jib.http.Response) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) Request(com.google.cloud.tools.jib.http.Request) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Aggregations

ResponseException (com.google.cloud.tools.jib.http.ResponseException)44 Test (org.junit.Test)32 NoHttpResponseException (org.apache.http.NoHttpResponseException)18 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)8 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)6 InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)4 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)4 Request (com.google.cloud.tools.jib.http.Request)4 Response (com.google.cloud.tools.jib.http.Response)4 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)4 IOException (java.io.IOException)4 JibContainer (com.google.cloud.tools.jib.api.JibContainer)2 RegistryException (com.google.cloud.tools.jib.api.RegistryException)2 BlobHttpContent (com.google.cloud.tools.jib.http.BlobHttpContent)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 RegistryCredentialsNotSentException (com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2