List of usage examples for com.google.common.net HttpHeaders CONTENT_LENGTH
String CONTENT_LENGTH
To view the source code for com.google.common.net HttpHeaders CONTENT_LENGTH.
Click Source Link
From source file:com.tinspx.util.net.Headers.java
/** * Parses a the value of the {@code Content-Length} header, throwing a * {@code NumberFormatException} if missing or invalid. * // w ww.j a va 2s. c om * @see #contentLength() */ public long parseContentLength() { final long len = Long.parseLong(last(HttpHeaders.CONTENT_LENGTH).trim()); if (len < 0) { throw new NumberFormatException("Content-Length cannot be negative: " + len); } return len; }
From source file:org.gaul.s3proxy.S3ProxyHandler.java
private void handleContainerCreate(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore, String containerName) throws IOException, S3Exception { if (containerName.isEmpty()) { throw new S3Exception(S3ErrorCode.METHOD_NOT_ALLOWED); }/*from ww w. j a va2 s .c o m*/ if (containerName.length() < 3 || containerName.length() > 255 || !VALID_BUCKET_PATTERN.matcher(containerName).matches()) { throw new S3Exception(S3ErrorCode.INVALID_BUCKET_NAME); } String contentLengthString = request.getHeader(HttpHeaders.CONTENT_LENGTH); if (contentLengthString != null) { 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); } } Collection<String> locations; try (PushbackInputStream pis = new PushbackInputStream(request.getInputStream())) { int ch = pis.read(); if (ch == -1) { // handle empty bodies locations = new ArrayList<>(); } else { pis.unread(ch); locations = parseSimpleXmlElements(pis, "LocationConstraint"); } } Location location = null; if (locations.size() == 1) { String locationString = locations.iterator().next(); for (Location loc : blobStore.listAssignableLocations()) { if (loc.getId().equalsIgnoreCase(locationString)) { location = loc; break; } } if (location == null) { throw new S3Exception(S3ErrorCode.INVALID_LOCATION_CONSTRAINT); } } logger.debug("Creating bucket with location: {}", location); CreateContainerOptions options = new CreateContainerOptions(); String acl = request.getHeader("x-amz-acl"); if ("public-read".equalsIgnoreCase(acl)) { options.publicRead(); } boolean created; try { created = blobStore.createContainerInLocation(location, containerName, options); } catch (AuthorizationException ae) { throw new S3Exception(S3ErrorCode.BUCKET_ALREADY_EXISTS, ae); } if (!created) { throw new S3Exception(S3ErrorCode.BUCKET_ALREADY_OWNED_BY_YOU, null, null, ImmutableMap.of("BucketName", containerName)); } }
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 w w.j a va2 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; }// www . jav 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); } 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 static void addMetadataToResponse(HttpServletResponse response, BlobMetadata metadata) { ContentMetadata contentMetadata = metadata.getContentMetadata(); response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentMetadata.getContentDisposition()); response.addHeader(HttpHeaders.CONTENT_ENCODING, contentMetadata.getContentEncoding()); response.addHeader(HttpHeaders.CONTENT_LANGUAGE, contentMetadata.getContentLanguage()); response.addHeader(HttpHeaders.CONTENT_LENGTH, contentMetadata.getContentLength().toString()); response.setContentType(contentMetadata.getContentType()); HashCode contentMd5 = contentMetadata.getContentMD5AsHashCode(); if (contentMd5 != null) { byte[] contentMd5Bytes = contentMd5.asBytes(); response.addHeader(HttpHeaders.CONTENT_MD5, BaseEncoding.base64().encode(contentMd5Bytes)); response.addHeader(HttpHeaders.ETAG, "\"" + BaseEncoding.base16().lowerCase().encode(contentMd5Bytes) + "\""); }//from ww w . ja v a2 s .c o m Date expires = contentMetadata.getExpires(); if (expires != null) { response.addDateHeader(HttpHeaders.EXPIRES, expires.getTime()); } response.addDateHeader(HttpHeaders.LAST_MODIFIED, metadata.getLastModified().getTime()); for (Map.Entry<String, String> entry : metadata.getUserMetadata().entrySet()) { response.addHeader(USER_METADATA_PREFIX + entry.getKey(), entry.getValue()); } }