Example usage for com.google.common.hash HashCode bits

List of usage examples for com.google.common.hash HashCode bits

Introduction

In this page you can find the example usage for com.google.common.hash HashCode bits.

Prototype

@CheckReturnValue
public abstract int bits();

Source Link

Document

Returns the number of bits in this hash code; a positive multiple of 8.

Usage

From source file:com.google.devtools.build.lib.analysis.ServerDirectories.java

private static HashCode checkMD5(HashCode hash) {
    Preconditions.checkArgument(hash.bits() == Hashing.md5().bits(), "Hash '%s' has %s bits", hash,
            hash.bits());/*from  w w w. j  a va 2 s.com*/
    return hash;
}

From source file:org.gradle.caching.internal.DefaultBuildCacheHasher.java

@Override
public BuildCacheHasher putHash(HashCode hashCode) {
    hasher.putInt(hashCode.bits() / 8);
    hasher.putBytes(hashCode.asBytes());
    return this;
}

From source file:com.bouncestorage.swiftproxy.v1.ObjectResource.java

@PUT
public Response putObject(@NotNull @PathParam("container") String container,
        @NotNull @Encoded @PathParam("object") String objectName, @NotNull @PathParam("account") String account,
        @QueryParam("multipart-manifest") String multiPartManifest, @QueryParam("signature") String signature,
        @QueryParam("expires") String expires, @HeaderParam(DYNAMIC_OBJECT_MANIFEST) String objectManifest,
        @HeaderParam("X-Auth-Token") String authToken,
        @HeaderParam(HttpHeaders.CONTENT_LENGTH) String contentLengthParam,
        @HeaderParam("Transfer-Encoding") String transferEncoding,
        @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType contentType,
        @HeaderParam("X-Detect-Content-Type") boolean detectContentType,
        @HeaderParam("X-Copy-From") String copyFrom, @HeaderParam("X-Copy-From-Account") String copyFromAccount,
        @HeaderParam(HttpHeaders.ETAG) String eTag,
        @HeaderParam(HttpHeaders.CONTENT_DISPOSITION) String contentDisposition,
        @HeaderParam(HttpHeaders.CONTENT_ENCODING) String contentEncoding,
        @HeaderParam("X-Delete-At") long deleteAt, @HeaderParam("X-Delete-After") long deleteAfter,
        @HeaderParam(HttpHeaders.IF_MATCH) String ifMatch,
        @HeaderParam(HttpHeaders.IF_NONE_MATCH) String ifNoneMatch,
        @HeaderParam(HttpHeaders.IF_MODIFIED_SINCE) Date ifModifiedSince,
        @HeaderParam(HttpHeaders.IF_UNMODIFIED_SINCE) Date ifUnmodifiedSince,
        @HeaderParam(SwiftHeaders.OBJECT_COPY_FRESH_METADATA) boolean freshMetadata, @Context Request request) {
    //objectName = normalizePath(objectName);
    if (objectName.length() > InfoResource.CONFIG.swift.max_object_name_length) {
        return badRequest();
    }//from   ww w .ja v  a 2s. c om
    if (transferEncoding != null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.NOT_IMPLEMENTED).build();
    }

    if (contentLengthParam == null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.LENGTH_REQUIRED).build();
    }
    long contentLength = contentLengthParam == null ? 0 : Long.parseLong(contentLengthParam);

    logger.info("PUT {}", objectName);

    if (copyFromAccount == null) {
        copyFromAccount = account;
    }

    if (copyFrom != null) {
        Pair<String, String> copy = validateCopyParam(copyFrom);
        return copyObject(copy.getFirst(), copy.getSecond(), copyFromAccount, authToken,
                container + "/" + objectName, account, null, contentType.toString(), contentEncoding,
                contentDisposition, ifMatch, ifModifiedSince, ifUnmodifiedSince, freshMetadata, request);
    }

    Map<String, String> metadata = getUserMetadata(request);
    validateUserMetadata(metadata);
    InputStream copiedStream = null;

    BlobStore blobStore = getBlobStore(authToken).get(container, objectName);
    if ("put".equals(multiPartManifest)) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try (TeeInputStream tee = new TeeInputStream(request.getInputStream(), buffer, true)) {
            ManifestEntry[] manifest = readSLOManifest(tee);
            validateManifest(manifest, blobStore, authToken);
            Pair<Long, String> sizeAndEtag = getManifestTotalSizeAndETag(Arrays.asList(manifest));
            metadata.put(STATIC_OBJECT_MANIFEST, sizeAndEtag.getFirst() + " " + sizeAndEtag.getSecond());
            copiedStream = new ByteArrayInputStream(buffer.toByteArray());
        } catch (IOException e) {
            throw propagate(e);
        }
    } else if (objectManifest != null) {
        metadata.put(DYNAMIC_OBJECT_MANIFEST, objectManifest);
    }

    if (!blobStore.containerExists(container)) {
        return notFound();
    }

    HashCode contentMD5 = null;
    if (eTag != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(eTag));
        } catch (IllegalArgumentException iae) {
            throw new ClientErrorException(422, iae); // Unprocessable Entity
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            // Unprocessable Entity
            throw new ClientErrorException(contentMD5.bits() + " != " + Hashing.md5().bits(), 422);
        }
    }

    try (InputStream is = copiedStream != null ? copiedStream : request.getInputStream()) {
        BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(objectName).userMetadata(metadata)
                .payload(is);
        if (contentDisposition != null) {
            builder.contentDisposition(contentDisposition);
        }
        if (contentEncoding != null) {
            builder.contentEncoding(contentEncoding);
        }
        if (contentType != null) {
            builder.contentType(contentType.toString());
        }
        if (contentLengthParam != null) {
            builder.contentLength(contentLength);
        }
        if (contentMD5 != null) {
            builder.contentMD5(contentMD5);
        }
        try {
            String remoteETag;
            try {
                remoteETag = blobStore.putBlob(container, builder.build());
            } catch (HttpResponseException e) {
                HttpResponse response = e.getResponse();
                if (response == null) {
                    throw e;
                }
                int code = response.getStatusCode();

                if (code == 400 && !"openstack-swift".equals(blobStore.getContext().unwrap().getId())) {
                    // swift expects 422 for md5 mismatch
                    throw new ClientErrorException(response.getStatusLine(), 422, e.getCause());
                } else {
                    throw new ClientErrorException(response.getStatusLine(), code, e.getCause());
                }
            }
            BlobMetadata meta = blobStore.blobMetadata(container, objectName);
            return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, remoteETag)
                    .header(HttpHeaders.LAST_MODIFIED, meta.getLastModified())
                    .header(HttpHeaders.CONTENT_LENGTH, 0).header(HttpHeaders.CONTENT_TYPE, contentType)
                    .header(HttpHeaders.DATE, new Date()).build();
        } catch (ContainerNotFoundException e) {
            return notFound();
        }
    } catch (IOException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

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;
        }// www.j  ava 2  s .  c o 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() + "\"");
    }
}

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;
        }/*  w ww . ja v a  2  s  .c om*/
    }

    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);
    }
}