@Override public String putBlob(final String containerName, final Blob blob) throws IOException { byte[] payload; HashCode actualHashCode; HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try { payload = ByteStreams.toByteArray(input); long actualSize = payload.length; Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength(); if (expectedSize != null && actualSize != expectedSize) { throw new IOException("Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize); } actualHashCode = input.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } } finally { Closeables2.closeQuietly(input); } Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob, payload, actualHashCode); Map<String, Blob> map = containerToBlobs.get(containerName); String blobName = newBlob.getMetadata().getName(); map.put(blobName, newBlob); containerToBlobAccess.get(containerName).put(blobName, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); }
@Test public void testHead() throws Exception { MockWebServer server = mockWebServer(new MockResponse()); IntegrationTestClient client = client(server.getUrl("/").toString()); try { assertTrue(client.exists("")); } finally { closeQuietly(client); server.shutdown(); } }
@Test public void testUploadBigFile() throws Exception { MockWebServer server = mockWebServer(new MD5CheckDispatcher()); IntegrationTestClient client = client(server.getUrl("/").toString()); String strDigest = base64().encode(digest); payload.getContentMetadata().setContentMD5(digest); payload.getContentMetadata().setContentLength(f.length()); Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload); RecordedRequest request = server.takeRequest(); assertEquals(request.getHeader("Content-MD5"), strDigest); assertEquals(headers.get("x-Content-MD5"), ImmutableList.of(strDigest)); } finally { if (payload != null) { payload.release(); closeQuietly(client); server.shutdown();
@Test public void longContentLengthSupported() throws Exception { long reallyLongContent = TestUtils.isJava6() ? Integer.MAX_VALUE : Long.MAX_VALUE; server.play(); HttpClient client = api(HttpClient.class, server.getUrl("/").toString()); fakePayload.getContentMetadata().setContentLength(reallyLongContent); client.post(server.getUrl("/").toURI(), fakePayload); fail("Should have errored since we didn't sent that much data!"); } catch (HttpResponseException expected) { assertEquals(server.takeRequest().getHeader(CONTENT_LENGTH), String.valueOf(reallyLongContent)); } finally { closeQuietly(client); server.shutdown();
@Test public void testInterruptThrottledGet() throws Exception { long timeoutMillis = 10 * 1000; MockWebServer server = mockWebServer(new MockResponse().setBody(XML).throttleBody(XML.length() / 2, timeoutMillis, TimeUnit.MILLISECONDS)); IntegrationTestClient client = client(server.getUrl("/").toString()); try { HttpResponse response = client.invoke(HttpRequest.builder() .method("GET") .endpoint(server.getUrl("/").toURI()) .build()); InputStream is = response.getPayload().openStream(); long now = System.currentTimeMillis(); is.close(); long diff = System.currentTimeMillis() - now; assertTrue(diff < timeoutMillis / 2, "expected " + diff + " to be less than " + (timeoutMillis / 2)); } finally { closeQuietly(client); try { server.shutdown(); } catch (IOException ex) { // MockWebServer 2.1.0 introduces an active wait for its executor termination. // That active wait is a hardcoded value and throws an IOE if the executor has not // terminated in that timeout. It is safe to ignore this exception (related to how // throttling works internally in MWS), as the functionality has been properly verified. } } } }
@Test(expectedExceptions = HttpResponseException.class, expectedExceptionsMessageRegExp = ".*exhausted connection specs.*") public void testSSLConnectionFailsIfOnlyHttpConfigured() throws Exception { MockWebServer server = mockWebServer(new MockResponse()); server.useHttps(sslContext.getSocketFactory(), false); Module httpConfigModule = new ConnectionSpecModule(ConnectionSpec.CLEARTEXT); PatchApi api = api(PatchApi.class, server.getUrl("/").toString(), httpConfigModule); try { api.patchNothing(""); } finally { closeQuietly(api); server.shutdown(); } }
@Test public void testHeadFailure() throws Exception { MockWebServer server = mockWebServer(new MockResponse().setResponseCode(404)); IntegrationTestClient client = client(server.getUrl("/").toString()); try { assertFalse(client.exists("")); } finally { closeQuietly(client); server.shutdown(); } }
@Test public void testAndExperiment() { ComputeServiceContext context = null; try { context = ContextBuilder .newBuilder(new GleSYSProviderMetadata()) .overrides(setupProperties()) .modules(setupModules()).build(ComputeServiceContext.class); assertEquals(context.getComputeService().listAssignableLocations().size(), 4); } finally { Closeables2.closeQuietly(context); } }
@Override public String putBlob(final String containerName, final Blob blob) throws IOException { String blobKey = blob.getMetadata().getName(); Payload payload = blob.getPayload(); filesystemContainerNameValidator.validate(containerName); filesystemBlobKeyValidator.validate(blobKey); HashCode expectedHashCode = payload.getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); payload.getContentMetadata().setContentMD5(actualHashCode); writeCommonMetadataAttr(view, blob); } catch (IOException e) { logger.debug("xattrs not supported on %s", tmpPath); delete(tmpFile); } catch (IOException e) { logger.debug("Could not delete %s: %s", tmpFile, e); closeQuietly(his); if (payload != null) { payload.release();
private void assertClientCreated(String identity, String credential) { Properties overrides = super.setupProperties(); overrides.setProperty(provider + ".identity", identity); overrides.setProperty(provider + ".credential", credential); ChefApi clientApi = create(overrides, setupModules()); try { Client client = clientApi.getClient(identity); assertNotNull(client, "Client not found: " + identity); } finally { closeQuietly(clientApi); } } }
@Override public void run() { ListenableFuture<byte[]> result; long from; try { for (from = 0; from < contentLength; from = from + partSize) { logger.debug(Thread.currentThread() + " writing to output"); result = results.take(); if (result == null) { output.close(); input.close(); throw new RuntimeException("Error downloading file part to stream"); } output.write(result.get()); } } catch (Exception e) { logger.debug(e.toString()); // Close pipe so client is notified of an exception Closeables2.closeQuietly(input); throw new RuntimeException(e); } finally { // Finished writing results to stream Closeables2.closeQuietly(output); } } });
@Override public T apply(HttpResponse arg0) { if (arg0.getPayload() == null) return nothing(); JsonReader reader = null; try { reader = new JsonReader(new InputStreamReader(arg0.getPayload().getInput())); // in case keys are not in quotes reader.setLenient(true); AtomicReference<String> name = Atomics.newReference(); JsonToken token = reader.peek(); for (; token != JsonToken.END_DOCUMENT && nnn(reader, token, name); token = skipAndPeek(token, reader)) { } if (name.get() == null) { logger.trace("did not object named %s in json from response %s", nameChoices, arg0); return nothing(); } else if (nameChoices.contains(name.get())) { return json.delegate().<T> fromJson(reader, type.getType()); } else { return nothing(); } } catch (IOException e) { throw new RuntimeException(String.format( "error reading from stream, parsing object named %s from http response %s", nameChoices, arg0), e); } finally { Closeables2.closeQuietly(reader); arg0.getPayload().release(); } }
/** * @param payload * payload to write * @param lengthDesc * what to use in error log when an IOException occurs * @param connection * connection to write to */ void writePayloadToConnection(Payload payload, Object lengthDesc, HttpURLConnection connection) throws IOException { connection.setDoOutput(true); CountingOutputStream out = new CountingOutputStream(connection.getOutputStream()); InputStream is = payload.openStream(); try { ByteStreams.copy(is, out); } catch (IOException e) { logger.error(e, "error after writing %d/%s bytes to %s", out.getCount(), lengthDesc, connection.getURL()); throw e; } finally { closeQuietly(is); } }
public T apply(HttpResponse from) { try { checkNotNull(from, "http response"); checkNotNull(from.getPayload(), "payload in " + from); } catch (NullPointerException e) { return addDetailsAndPropagate(from, e); } InputStream is = null; try { // debug is more normally set, so trace is more appropriate for // something heavy like this if (from.getStatusCode() >= 300 || logger.isTraceEnabled()) return convertStreamToStringAndParse(from); is = from.getPayload().getInput(); return parse(new InputSource(is)); } catch (RuntimeException e) { return addDetailsAndPropagate(from, e); } finally { Closeables2.closeQuietly(is); from.getPayload().release(); } }
public static byte[] toByteArrayOrNull(PayloadEnclosing response) { if (response.getPayload() != null) { InputStream input = response.getPayload().getInput(); try { return toByteArray(input); } catch (IOException e) { propagate(e); } finally { closeQuietly(input); } } return null; }
private void output(final File out) { checkNotNull(out, "output"); InputStream in = null; try { in = new FileInputStream(out); wire(">> ", in); } catch (FileNotFoundException e) { logger.error(e, "Error tapping file: %s", out); } finally { closeQuietly(in); } }
@SuppressWarnings("unchecked") @Override public InputSupplier<InputStream> apply(InputStream from) { if (from == null) return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return null; } }; try { return InputSupplier.class.cast(ByteStreams.newInputStreamSupplier(ByteStreams.toByteArray(from))); } catch (Exception e) { logger.warn(e, "ignoring problem retrieving credentials"); return null; } finally { Closeables2.closeQuietly(from); } } }
if (logger.isTraceEnabled()) { DockerInputStream dis = null; try { switch (data.getType()) { case OUT: logger.trace("Container [%s] StdOut: %s", idToLog, payload); break; case ERR: logger.trace("Container [%s] StdErr: %s", idToLog, payload); break; default: logger.trace("Retrieving container log failed", e); } finally { Closeables2.closeQuietly(dis);
/** * if we created the stream, then it is already consumed on close. */ @Override public void release() { closeQuietly(content); }
@Test(dataProvider = "gets") public void testGetStringSynch(String uri) throws Exception { MockWebServer server = mockWebServer(new MockResponse().setBody(XML)); IntegrationTestClient client = client(server.getUrl("/").toString()); try { String result = client.synch(uri); RecordedRequest request = server.takeRequest(); assertTrue(URLDecoder.decode(request.getPath(), "UTF-8").endsWith(uri)); assertEquals(result, XML); } finally { closeQuietly(client); server.shutdown(); } }