Search in sources :

Example 1 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method read.

@Override
public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, long position, int bytes) {
    try {
        Get req = storage.objects().get(from.getBucket(), from.getName()).setGeneration(from.getGeneration()).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options));
        checkArgument(position >= 0, "Position should be non-negative, is %d", position);
        StringBuilder range = new StringBuilder();
        range.append("bytes=").append(position).append("-").append(position + bytes - 1);
        HttpHeaders requestHeaders = req.getRequestHeaders();
        requestHeaders.setRange(range.toString());
        setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, options);
        ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
        HttpResponse httpResponse = req.executeMedia();
        // todo(mziccard) remove when
        // https://github.com/GoogleCloudPlatform/google-cloud-java/issues/982 is fixed
        String contentEncoding = httpResponse.getContentEncoding();
        if (contentEncoding != null && contentEncoding.contains("gzip")) {
            try {
                Field responseField = httpResponse.getClass().getDeclaredField("response");
                responseField.setAccessible(true);
                LowLevelHttpResponse lowLevelHttpResponse = (LowLevelHttpResponse) responseField.get(httpResponse);
                IOUtils.copy(lowLevelHttpResponse.getContent(), output);
            } catch (IllegalAccessException | NoSuchFieldException ex) {
                throw new StorageException(BaseServiceException.UNKNOWN_CODE, "Error parsing gzip response", ex);
            }
        } else {
            httpResponse.download(output);
        }
        String etag = req.getLastResponseHeaders().getETag();
        return Tuple.of(etag, output.toByteArray());
    } catch (IOException ex) {
        StorageException serviceException = translate(ex);
        if (serviceException.getCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            return Tuple.of(null, new byte[0]);
        }
        throw serviceException;
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) Get(com.google.api.services.storage.Storage.Objects.Get) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) StorageException(com.google.cloud.storage.StorageException)

Example 2 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project copybara by google.

the class GithubApiTest method getTransport.

@Override
public GitHubApiTransport getTransport() throws Exception {
    credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
    true).init().withCredentialHelper("store --file=" + credentialsFile);
    requestToResponse = new HashMap<>();
    requestValidators = new HashMap<>();
    httpTransport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            String requestString = method + " " + url;
            MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    Predicate<String> validator = requestValidators.get(method + " " + url);
                    if (validator != null) {
                        assertWithMessage("Request content did not match expected values.").that(validator.test(getContentAsString())).isTrue();
                    }
                    return super.execute();
                }
            };
            byte[] content = requestToResponse.get(requestString);
            MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
            if (content == null) {
                response.setContent(String.format("{ 'message' : 'This is not the repo you are looking for! %s %s'," + " 'documentation_url' : 'http://github.com/some_url'}", method, url));
                response.setStatusCode(404);
            } else {
                response.setContent(content);
            }
            request.setResponse(response);
            return request;
        }
    };
    return new GitHubApiTransportImpl(repo, httpTransport, "some_storage_file", new TestingConsole());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Predicate(java.util.function.Predicate) GitRepository(com.google.copybara.git.GitRepository) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse)

Example 3 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method firebaseDelete.

@Test
public void firebaseDelete() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    firebaseChannel.firebaseDelete(FIREBASE_DB_URL + "/my/path");
    verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/my/path");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 4 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method firebaseGet.

@Test
public void firebaseGet() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    firebaseChannel.firebaseGet(FIREBASE_DB_URL + "/my/path");
    verify(mockHttpTransport, times(1)).buildRequest("GET", FIREBASE_DB_URL + "/my/path");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 5 with LowLevelHttpResponse

use of com.google.api.client.http.LowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.

the class FirebaseChannelTest method firebasePatch.

@Test
public void firebasePatch() throws Exception {
    // Mock out the firebase response. See
    // http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    return response;
                }
            };
        }
    });
    FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
    Game game = new Game();
    firebaseChannel.firebasePatch(FIREBASE_DB_URL + "/my/path", game);
    verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/my/path");
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Aggregations

LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)24 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)21 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)20 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)18 IOException (java.io.IOException)18 Test (org.junit.Test)16 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)13 GenericJson (com.google.api.client.json.GenericJson)6 Objectify (com.googlecode.objectify.Objectify)5 JsonFactory (com.google.api.client.json.JsonFactory)3 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)3 Storage (com.google.api.services.storage.Storage)3 HttpResponse (com.google.api.client.http.HttpResponse)2 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)2 Mockito.anyString (org.mockito.Mockito.anyString)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 JsonWebSignature (com.google.api.client.json.webtoken.JsonWebSignature)1 Get (com.google.api.services.storage.Storage.Objects.Get)1