Search in sources :

Example 1 with ErrorResponseTemplate

use of com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException_notBlobUnknown.

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

Example 2 with ErrorResponseTemplate

use of com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException.

@Test
public void testHandleHttpResponseException() throws IOException, RegistryErrorException {
    HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
    Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message"));
    Mockito.when(mockHttpResponseException.getContent()).thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
    BlobDescriptor blobDescriptor = testBlobChecker.handleHttpResponseException(mockHttpResponseException);
    Assert.assertNull(blobDescriptor);
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) HttpResponseException(com.google.api.client.http.HttpResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Test(org.junit.Test)

Example 3 with ErrorResponseTemplate

use of com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate in project jib by google.

the class RegistryClient method callRegistryEndpoint.

/**
 * Calls the registry endpoint with an override URL.
 *
 * @param url the endpoint URL to call, or {@code null} to use default from {@code
 *     registryEndpointProvider}
 * @param registryEndpointProvider the {@link RegistryEndpointProvider} to the endpoint
 */
@Nullable
private <T> T callRegistryEndpoint(@Nullable URL url, RegistryEndpointProvider<T> registryEndpointProvider) throws IOException, RegistryException {
    if (url == null) {
        url = registryEndpointProvider.getApiRoute(getApiRouteBase());
    }
    try (Connection connection = new Connection(url)) {
        Request request = Request.builder().setAuthorization(authorization).setUserAgent(getUserAgent()).setAccept(registryEndpointProvider.getAccept()).setBody(registryEndpointProvider.getContent()).build();
        Response response = connection.send(registryEndpointProvider.getHttpMethod(), request);
        return registryEndpointProvider.handleResponse(response);
    } catch (HttpResponseException ex) {
        // First, see if the endpoint provider handles an exception as an expected response.
        try {
            return registryEndpointProvider.handleHttpResponseException(ex);
        } catch (HttpResponseException httpResponseException) {
            if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_METHOD_NOT_ALLOWED) {
                // The name or reference was invalid.
                ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(httpResponseException.getContent(), ErrorResponseTemplate.class);
                RegistryErrorExceptionBuilder registryErrorExceptionBuilder = new RegistryErrorExceptionBuilder(registryEndpointProvider.getActionDescription(), httpResponseException);
                for (ErrorEntryTemplate errorEntry : errorResponse.getErrors()) {
                    registryErrorExceptionBuilder.addReason(errorEntry);
                }
                throw registryErrorExceptionBuilder.build();
            } else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED || httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_FORBIDDEN) {
                throw new RegistryUnauthorizedException(registryEndpointProperties.getServerUrl(), registryEndpointProperties.getImageName(), httpResponseException);
            } else if (httpResponseException.getStatusCode() == HttpStatusCodes.STATUS_CODE_TEMPORARY_REDIRECT) {
                return callRegistryEndpoint(new URL(httpResponseException.getHeaders().getLocation()), registryEndpointProvider);
            } else {
                // Unknown
                throw httpResponseException;
            }
        }
    } catch (NoHttpResponseException ex) {
        throw new RegistryNoResponseException(ex);
    } catch (SSLPeerUnverifiedException ex) {
        // Fall-back to HTTP
        GenericUrl httpUrl = new GenericUrl(url);
        httpUrl.setScheme("http");
        return callRegistryEndpoint(httpUrl.toURL(), registryEndpointProvider);
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Connection(com.google.cloud.tools.jib.http.Connection) Request(com.google.cloud.tools.jib.http.Request) NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) URL(java.net.URL) Response(com.google.cloud.tools.jib.http.Response) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Nullable(javax.annotation.Nullable)

Example 4 with ErrorResponseTemplate

use of com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate in project jib by google.

the class BlobChecker method handleHttpResponseException.

@Override
@Nullable
public BlobDescriptor handleHttpResponseException(HttpResponseException httpResponseException) throws RegistryErrorException, HttpResponseException {
    if (httpResponseException.getStatusCode() != HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
        throw httpResponseException;
    }
    // Finds a BLOB_UNKNOWN error response code.
    String errorContent = httpResponseException.getContent();
    if (errorContent == null) {
        // be null, even for HEAD requests.
        return null;
    } else {
        try {
            ErrorResponseTemplate errorResponse = JsonTemplateMapper.readJson(errorContent, ErrorResponseTemplate.class);
            List<ErrorEntryTemplate> errors = errorResponse.getErrors();
            if (errors.size() == 1) {
                String errorCodeString = errors.get(0).getCode();
                if (errorCodeString == null) {
                    // Did not get an error code back.
                    throw httpResponseException;
                }
                ErrorCodes errorCode = ErrorCodes.valueOf(errorCodeString);
                if (errorCode.equals(ErrorCodes.BLOB_UNKNOWN)) {
                    return null;
                }
            }
        } catch (IOException ex) {
            throw new RegistryErrorExceptionBuilder(getActionDescription(), ex).addReason("Failed to parse registry error response body").build();
        }
    }
    // BLOB_UNKNOWN was not found as a error response code.
    throw httpResponseException;
}
Also used : IOException(java.io.IOException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Nullable(javax.annotation.Nullable)

Example 5 with ErrorResponseTemplate

use of com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate in project jib by google.

the class BlobCheckerTest method testHandleHttpResponseException_hasOtherErrors.

@Test
public void testHandleHttpResponseException_hasOtherErrors() throws IOException, RegistryErrorException {
    HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
    Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")).addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message"));
    Mockito.when(mockHttpResponseException.getContent()).thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
    try {
        testBlobChecker.handleHttpResponseException(mockHttpResponseException);
        Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
    } catch (HttpResponseException ex) {
        Assert.assertEquals(mockHttpResponseException, ex);
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Test(org.junit.Test)

Aggregations

ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)5 HttpResponseException (com.google.api.client.http.HttpResponseException)4 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)4 Test (org.junit.Test)3 Nullable (javax.annotation.Nullable)2 GenericUrl (com.google.api.client.http.GenericUrl)1 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)1 Connection (com.google.cloud.tools.jib.http.Connection)1 Request (com.google.cloud.tools.jib.http.Request)1 Response (com.google.cloud.tools.jib.http.Response)1 IOException (java.io.IOException)1 URL (java.net.URL)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 NoHttpResponseException (org.apache.http.NoHttpResponseException)1