List of usage examples for com.google.common.hash HashCode fromBytes
@CheckReturnValue public static HashCode fromBytes(byte[] bytes)
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handlePutBlob(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore, String containerName, String blobName) throws IOException, S3Exception { // Flag headers present since HttpServletResponse.getHeader returns // null for empty headers values. String contentLengthString = null; String contentMD5String = null; for (String headerName : Collections.list(request.getHeaderNames())) { String headerValue = Strings.nullToEmpty(request.getHeader(headerName)); if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { contentLengthString = headerValue; } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) { contentMD5String = headerValue; }/*from w ww . ja v a 2 s .co m*/ } HashCode contentMD5 = null; if (contentMD5String != null) { try { contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String)); } catch (IllegalArgumentException iae) { throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae); } if (contentMD5.bits() != Hashing.md5().bits()) { throw new S3Exception(S3ErrorCode.INVALID_DIGEST); } } if (contentLengthString == null) { throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH); } long contentLength; try { contentLength = Long.parseLong(contentLengthString); } catch (NumberFormatException nfe) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe); } if (contentLength < 0) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT); } try (InputStream is = request.getInputStream()) { BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(blobName).payload(is) .contentLength(request.getContentLength()); addContentMetdataFromHttpRequest(builder, request); if (contentMD5 != null) { builder = builder.contentMD5(contentMD5); } PutOptions options = new PutOptions(); String blobStoreType = getBlobStoreType(blobStore); if (blobStoreType.equals("azureblob") && contentLength > 64 * 1024 * 1024) { options.multipart(true); } String eTag; try { eTag = blobStore.putBlob(containerName, builder.build(), options); } catch (HttpResponseException hre) { HttpResponse hr = hre.getResponse(); if (hr == null) { return; } int status = hr.getStatusCode(); switch (status) { case HttpServletResponse.SC_BAD_REQUEST: case 422: // Swift returns 422 Unprocessable Entity throw new S3Exception(S3ErrorCode.BAD_DIGEST); default: // TODO: emit hre.getContent() ? response.sendError(status); break; } return; } catch (RuntimeException re) { if (Throwables2.getFirstThrowableOfType(re, TimeoutException.class) != null) { throw new S3Exception(S3ErrorCode.REQUEST_TIMEOUT, re); } else { throw re; } } // S3 quotes ETag while Swift does not if (!eTag.startsWith("\"") && !eTag.endsWith("\"")) { eTag = '"' + eTag + '"'; } response.addHeader(HttpHeaders.ETAG, eTag); } // TODO: jclouds should include this in PutOptions String cannedAcl = request.getHeader("x-amz-acl"); if (cannedAcl != null && !cannedAcl.equalsIgnoreCase("private")) { handleSetBlobAcl(request, response, blobStore, containerName, blobName); } }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleUploadPart(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore, String containerName, String blobName, String uploadId) throws IOException, S3Exception { // TODO: duplicated from handlePutBlob String contentLengthString = null; String contentMD5String = null; for (String headerName : Collections.list(request.getHeaderNames())) { String headerValue = Strings.nullToEmpty(request.getHeader(headerName)); if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) { contentLengthString = headerValue; } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) { contentMD5String = headerValue; }/*from w ww .j a v a2s . co m*/ } HashCode contentMD5 = null; if (contentMD5String != null) { try { contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String)); } catch (IllegalArgumentException iae) { throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae); } if (contentMD5.bits() != Hashing.md5().bits()) { throw new S3Exception(S3ErrorCode.INVALID_DIGEST); } } if (contentLengthString == null) { throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH); } long contentLength; try { contentLength = Long.parseLong(contentLengthString); } catch (NumberFormatException nfe) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe); } if (contentLength < 0) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT); } String partNumberString = request.getParameter("partNumber"); if (partNumberString == null) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT); } int partNumber; try { partNumber = Integer.parseInt(partNumberString); } catch (NumberFormatException nfe) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, "Part number must be an integer between 1 and 10000" + ", inclusive", nfe, ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString)); } if (partNumber < 1 || partNumber > 10_000) { throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, "Part number must be an integer between 1 and 10000" + ", inclusive", (Throwable) null, ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString)); } // TODO: how to reconstruct original mpu? MultipartUpload mpu = MultipartUpload.create(containerName, blobName, uploadId, createFakeBlobMetadata(blobStore)); try (InputStream is = request.getInputStream()) { Payload payload = Payloads.newInputStreamPayload(is); payload.getContentMetadata().setContentLength(contentLength); if (contentMD5 != null) { payload.getContentMetadata().setContentMD5(contentMD5); } MultipartPart part = blobStore.uploadMultipartPart(mpu, partNumber, payload); response.addHeader(HttpHeaders.ETAG, "\"" + part.partETag() + "\""); } }