@VisibleForTesting String hashBody(Payload payload) { if (payload == null) return emptyStringHash; payload = useTheFilePartIfForm(payload); checkArgument(payload != null, "payload was null"); checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload); try { return base64().encode(ByteSources.asByteSource(payload.getInput()).hash(sha1()).asBytes()); } catch (Exception e) { Throwables.propagateIfPossible(e); throw new HttpException("error creating sigature for payload: " + payload, e); } }
@VisibleForTesting String hashBody(Payload payload) { if (payload == null) return emptyStringHash; payload = useTheFilePartIfForm(payload); checkArgument(payload != null, "payload was null"); checkArgument(payload.isRepeatable(), "payload must be repeatable: " + payload); try { return base64().encode(ByteSources.asByteSource(payload.getInput()).hash(sha1()).asBytes()); } catch (Exception e) { Throwables.propagateIfPossible(e); throw new HttpException("error creating sigature for payload: " + payload, e); } }
@Override protected HttpUriRequest convert(HttpRequest request) throws IOException { HttpUriRequest returnVal = apacheHCUtils.convertToApacheRequest(request); if (request.getPayload() != null && request.getPayload().getContentMetadata().getContentMD5() != null){ String md5 = base64().encode(asByteSource(request.getPayload().getInput()).hash(md5()).asBytes()); returnVal.addHeader("Content-MD5", md5); } return returnVal; }
@Test(groups = "live") public void testRemoteInputStream() throws Exception { try { URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl")); URLConnection connection = url.openConnection(); HttpWire wire = setUp(); InputStream in = wire.input(connection.getInputStream()); byte[] compare = asByteSource(in).hash(md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); } catch (UnknownHostException e) { // probably in offline mode } }
@Test(groups = "live") public void testRemoteInputStreamSynch() throws Exception { try { URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl")); URLConnection connection = url.openConnection(); HttpWire wire = setUpSynch(); InputStream in = wire.input(connection.getInputStream()); byte[] compare = asByteSource(in).hash(md5()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); } catch (UnknownHostException e) { // probably in offline mode } }
protected static byte[] md5Supplier(InputSupplier<InputStream> supplier) throws IOException { return asByteSource(supplier.getInput()).hash(md5()).asBytes(); }
private static void handlePost(HttpServletRequest request, HttpServletResponse response) throws IOException { InputStream body = request.getInputStream(); try { if (request.getHeader("Content-MD5") != null) { String expectedMd5 = request.getHeader("Content-MD5"); String realMd5FromRequest; realMd5FromRequest = base64().encode(asByteSource(body).hash(md5()).asBytes()); boolean matched = expectedMd5.equals(realMd5FromRequest); if (matched) { response.setStatus(SC_OK); response.addHeader("x-Content-MD5", realMd5FromRequest); } else { response.sendError(500, "didn't match"); } } else { String responseString = (request.getContentLength() < 10240) ? toStringAndClose(body) + "POST" : "POST"; body = null; for (String header : new String[] { CONTENT_DISPOSITION, CONTENT_LANGUAGE, CONTENT_ENCODING }) if (request.getHeader(header) != null) { response.addHeader("x-" + header, request.getHeader(header)); } response.setStatus(SC_OK); response.getWriter().println(responseString); } Request.class.cast(request).setHandled(true); } catch (IOException e) { closeQuietly(body); response.sendError(500, getStackTraceAsString(e)); } }
@BeforeClass(groups = { "integration", "live" }) @Override public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception { super.setUpResourcesOnThisThread(testContext); oneHundredOneConstitutions = getTestDataSupplier(); oneHundredOneConstitutionsMD5 = asByteSource(oneHundredOneConstitutions.getInput()).hash(md5()).asBytes(); }
protected void assertPayloadEquals(HttpRequest request, String toMatch, String contentType, String contentDispositon, String contentEncoding, String contentLanguage, boolean contentMD5, Date expires) { if (request.getPayload() == null) { assertNull(toMatch); } else { String payload = null; try { payload = Strings2.toString(request.getPayload()); } catch (IOException e) { propagate(e); } assertEquals(payload, toMatch); Long length = Long.valueOf(payload.getBytes().length); try { assertContentHeadersEqual(request, contentType, contentDispositon, contentEncoding, contentLanguage, length, contentMD5 ? asByteSource(request.getPayload().getInput()).hash(md5()).asBytes() : null, expires); } catch (IOException e) { propagate(e); } } }
/** * Calculates and sets {@link Payload#setContentMD5} on the payload. * * <p/> * note that this will rebuffer in memory if the payload is not repeatable. * * @param payload * payload to calculate * @return new Payload with md5 set. * @throws IOException */ public static Payload calculateMD5(Payload payload) throws IOException { checkNotNull(payload, "payload"); if (!payload.isRepeatable()) { MutableContentMetadata oldContentMetadata = payload.getContentMetadata(); Payload oldPayload = payload; try { payload = newByteArrayPayload(toByteArray(payload)); } finally { oldPayload.release(); } oldContentMetadata.setContentLength(payload.getContentMetadata().getContentLength()); oldContentMetadata.setContentMD5(payload.getContentMetadata().getContentMD5()); payload.setContentMetadata(oldContentMetadata); } payload.getContentMetadata().setContentMD5(asByteSource(payload.getInput()).hash(md5()).asBytes()); return payload; }
@Test public void testListCookbooks() throws Exception { Set<String> cookbookNames = api.listCookbooks(); assertFalse(cookbookNames.isEmpty()); for (String cookbook : cookbookNames) { for (String version : api.getVersionsOfCookbook(cookbook)) { CookbookVersion cookbookO = api.getCookbook(cookbook, version); for (Resource resource : ImmutableList.<Resource> builder().addAll(cookbookO.getDefinitions()) .addAll(cookbookO.getFiles()).addAll(cookbookO.getLibraries()).addAll(cookbookO.getSuppliers()) .addAll(cookbookO.getRecipes()).addAll(cookbookO.getResources()).addAll(cookbookO.getRootFiles()) .addAll(cookbookO.getTemplates()).build()) { try { InputStream stream = api.getResourceContents(resource); byte[] md5 = asByteSource(stream).hash(md5()).asBytes(); assertEquals(md5, resource.getChecksum()); } catch (NullPointerException e) { assert false : "resource not found: " + resource; } } } } }
/** * Calculates and sets {@link Payload#setContentMD5} on the payload. * * <p/> * note that this will rebuffer in memory if the payload is not repeatable. * * @param payload * payload to calculate * @return new Payload with md5 set. * @throws IOException */ public static Payload calculateMD5(Payload payload) throws IOException { checkNotNull(payload, "payload"); if (!payload.isRepeatable()) { MutableContentMetadata oldContentMetadata = payload.getContentMetadata(); Payload oldPayload = payload; try { payload = newByteArrayPayload(toByteArray(payload)); } finally { oldPayload.release(); } oldContentMetadata.setContentLength(payload.getContentMetadata().getContentLength()); oldContentMetadata.setContentMD5(payload.getContentMetadata().getContentMD5()); payload.setContentMetadata(oldContentMetadata); } payload.getContentMetadata().setContentMD5(asByteSource(payload.getInput()).hash(md5()).asBytes()); return payload; }
md5 = base64().encode(asByteSource(oneHundredOneConstitutions.getInput()).hash(md5()).asBytes());