use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class AmbrySecurityService method setGetBlobResponseHeaders.
/**
* Sets the required headers in the response.
* @param blobInfo the {@link BlobInfo} to refer to while setting headers.
* @param options the {@link GetBlobOptions} associated with the request.
* @param restResponseChannel the {@link RestResponseChannel} to set headers on.
* @throws RestServiceException if there was any problem setting the headers.
*/
private void setGetBlobResponseHeaders(BlobInfo blobInfo, GetBlobOptions options, RestResponseChannel restResponseChannel) throws RestServiceException {
BlobProperties blobProperties = blobInfo.getBlobProperties();
restResponseChannel.setHeader(RestUtils.Headers.BLOB_SIZE, blobProperties.getBlobSize());
restResponseChannel.setHeader(RestUtils.Headers.ACCEPT_RANGES, RestUtils.BYTE_RANGE_UNITS);
long contentLength = blobProperties.getBlobSize();
if (options.getRange() != null) {
Pair<String, Long> rangeAndLength = RestUtils.buildContentRangeAndLength(options.getRange(), contentLength, options.resolveRangeOnEmptyBlob());
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_RANGE, rangeAndLength.getFirst());
contentLength = rangeAndLength.getSecond();
}
if (contentLength < frontendConfig.chunkedGetResponseThresholdInBytes) {
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, contentLength);
}
if (blobProperties.getContentType() != null) {
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_TYPE, blobProperties.getContentType());
// Ensure browsers do not execute html with embedded exploits.
if (blobProperties.getContentType().equals("text/html")) {
restResponseChannel.setHeader("Content-Disposition", "attachment");
}
}
if (blobProperties.getContentEncoding() != null) {
restResponseChannel.setHeader(Headers.CONTENT_ENCODING, blobProperties.getContentEncoding());
}
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method oldStyleUserMetadataTest.
/**
* Tests how metadata that has not been POSTed in the form of headers is returned.
* @throws Exception
*/
@Test
public void oldStyleUserMetadataTest() throws Exception {
ByteBuffer content = ByteBuffer.allocate(0);
BlobProperties blobProperties = new BlobProperties(0, "userMetadataTestOldStyleServiceID", Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false);
byte[] usermetadata = TestUtils.getRandomBytes(25);
String blobId = router.putBlob(blobProperties, usermetadata, new ByteBufferReadableStreamChannel(content), new PutBlobOptionsBuilder().build()).get();
RestUtils.SubResource[] subResources = { RestUtils.SubResource.UserMetadata, RestUtils.SubResource.BlobInfo };
for (RestUtils.SubResource subResource : subResources) {
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + subResource, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status for " + subResource, ResponseStatus.Ok, restResponseChannel.getStatus());
assertEquals("Unexpected Content-Type for " + subResource, "application/octet-stream", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Unexpected Content-Length for " + subResource, usermetadata.length, Integer.parseInt(restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertArrayEquals("Unexpected user metadata for " + subResource, usermetadata, restResponseChannel.getResponseBody());
}
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyResponsePathAccountAndContainerInjection.
/**
* Test response path account and container injection for V1 blob IDs.
* @param serviceId the service ID for the blob.
* @param isPrivate {@code true} if the blob is private.
* @param expectedAccount the expected {@link Account} to verify its presence in {@link RestRequest}.
* @param expectedContainer the expected {@link Container} to verify its presence in {@link RestRequest}.
* @throws Exception
*/
private void verifyResponsePathAccountAndContainerInjection(String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
BlobProperties blobProperties = new BlobProperties(0, serviceId, "owner", "image/gif", isPrivate, Utils.Infinite_Time, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false, null, null, null);
ReadableStreamChannel content = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
String blobId = router.putBlobWithIdVersion(blobProperties, new byte[0], content, BlobId.BLOB_ID_V1).get();
verifyAccountAndContainerFromBlobId(blobId, expectedAccount, expectedContainer, null);
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class NamedBlobPutHandlerTest method uploadChunksViaRouter.
/**
* Upload chunks using the router directly.
* @param creationTimeMs the creation time to set for the chunks.
* @param container the {@link Container} to create the chunks in.
* @param chunkSizes the sizes for each chunk to upload.
* @return a list of {@link ChunkInfo} objects that contains metadata about each chunk uploaded.
*/
private List<ChunkInfo> uploadChunksViaRouter(long creationTimeMs, Container container, int... chunkSizes) throws Exception {
long blobTtlSecs = TimeUnit.DAYS.toSeconds(1);
List<ChunkInfo> chunks = new ArrayList<>();
for (int chunkSize : chunkSizes) {
byte[] content = TestUtils.getRandomBytes(chunkSize);
BlobProperties blobProperties = new BlobProperties(-1, SERVICE_ID, OWNER_ID, CONTENT_TYPE, !container.isCacheable(), blobTtlSecs, creationTimeMs, container.getParentAccountId(), container.getId(), container.isEncrypted(), null, null, null);
String blobId = router.putBlob(blobProperties, null, new ByteBufferReadableStreamChannel(ByteBuffer.wrap(content)), new PutBlobOptionsBuilder().chunkUpload(true).build()).get(TIMEOUT_SECS, TimeUnit.SECONDS);
chunks.add(new ChunkInfo(blobId, chunkSize, Utils.addSecondsToEpochTime(creationTimeMs, blobTtlSecs)));
}
return chunks;
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class AmbryIdConverterFactoryTest method testConversionForNamedBlob.
/**
* Tests the conversion by the {@code idConverter}.
* @param idConverter the {@link IdConverter} instance to use.
* @param restMethod the {@link RestMethod} of the {@link RestRequest} that will be created.
* @param signedIdMetadata the headers of the {@link RestRequest}.
* @param expectedOutput the expected output from the {@code idConverter}.
* @param input the input string
* @throws Exception
*/
private void testConversionForNamedBlob(IdConverter idConverter, RestMethod restMethod, Map<String, String> signedIdMetadata, String expectedOutput, String input) throws Exception {
JSONObject requestData = new JSONObject();
JSONObject headers = new JSONObject();
String contentType = "application/octet-stream";
headers.put(RestUtils.Headers.AMBRY_CONTENT_TYPE, contentType);
requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
requestData.put(MockRestRequest.URI_KEY, NAMED_BLOB_PATH);
requestData.put(MockRestRequest.HEADERS_KEY, headers);
RestRequest restRequest = new MockRestRequest(requestData, null);
if (signedIdMetadata != null) {
restRequest.setArg(RestUtils.InternalKeys.SIGNED_ID_METADATA_KEY, signedIdMetadata);
}
BlobInfo blobInfo = null;
if (restMethod.equals(RestMethod.PUT)) {
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(NAMED_BLOB_PATH, Collections.emptyMap(), Collections.emptyList(), "Ambry-test"));
blobInfo = new BlobInfo(new BlobProperties(-1, "service", accountId, containerId, false), new byte[0]);
}
IdConversionCallback callback = new IdConversionCallback();
assertEquals("Converted ID does not match expected (Future)", expectedOutput, idConverter.convert(restRequest, input, blobInfo, callback).get(5, TimeUnit.SECONDS));
assertEquals("Converted ID does not match expected (Callback)", expectedOutput, callback.result);
}
Aggregations