use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class RouterStore method writeAccountMapToRouter.
/**
* Save the given {@link Account} metadata, as json object, in a blob in {@code AmbryServer}.
* @param accountMap The {@link Account} metadata to save.
* @param router The {@link Router} instance.
* @return A blob id if the operation is finished successfully.
* @throws Exception If there is any exceptions while saving the bytes to {@code AmbryServer}.
*/
static String writeAccountMapToRouter(Map<String, String> accountMap, Router router) throws Exception {
Objects.requireNonNull(router, "Router is null");
// Construct the json object and save it to ambry server.
JSONObject object = new JSONObject();
for (Map.Entry<String, String> entry : accountMap.entrySet()) {
object.put(entry.getKey(), entry.getValue());
}
ByteBufferReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(object.toString().getBytes(Charsets.UTF_8)));
BlobProperties properties = new BlobProperties(channel.getSize(), SERVICE_ID, ACCOUNT_ID, CONTAINER_ID, false);
return router.putBlob(properties, null, channel, PutBlobOptions.DEFAULT).get();
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class MockHeadCallback method setBlobPropertiesResponseHeaders.
/**
* Sets the required blob properties headers in the response.
* @param blobInfo the {@link BlobInfo} to refer to while setting headers.
* @throws RestServiceException if there was any problem setting the headers.
*/
private void setBlobPropertiesResponseHeaders(BlobInfo blobInfo) throws RestServiceException {
BlobProperties blobProperties = blobInfo.getBlobProperties();
restResponseChannel.setHeader(RestUtils.Headers.LAST_MODIFIED, new Date(blobProperties.getCreationTimeInMs()));
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, blobProperties.getBlobSize());
// Blob props
restResponseChannel.setHeader(RestUtils.Headers.BLOB_SIZE, blobProperties.getBlobSize());
restResponseChannel.setHeader(RestUtils.Headers.SERVICE_ID, blobProperties.getServiceId());
restResponseChannel.setHeader(RestUtils.Headers.CREATION_TIME, new Date(blobProperties.getCreationTimeInMs()));
restResponseChannel.setHeader(RestUtils.Headers.PRIVATE, blobProperties.isPrivate());
if (blobProperties.getTimeToLiveInSeconds() != Utils.Infinite_Time) {
restResponseChannel.setHeader(RestUtils.Headers.TTL, Long.toString(blobProperties.getTimeToLiveInSeconds()));
}
if (blobProperties.getContentType() != null) {
restResponseChannel.setHeader(RestUtils.Headers.AMBRY_CONTENT_TYPE, blobProperties.getContentType());
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_TYPE, blobProperties.getContentType());
}
if (blobProperties.getOwnerId() != null) {
restResponseChannel.setHeader(RestUtils.Headers.OWNER_ID, blobProperties.getOwnerId());
}
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class RestUtilsTest method verifyBlobPropertiesConstructionSuccess.
/**
* Verifies that a request with headers defined by {@code headers} builds {@link BlobProperties} successfully and
* matches the values of the properties with those in {@code headers}.
* @param headers the headers that need to go with the request that is used to construct {@link BlobProperties}.
* @throws Exception
*/
private void verifyBlobPropertiesConstructionSuccess(JSONObject headers) throws Exception {
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers);
Account account = (Account) headers.get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY);
Container container = (Container) headers.get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY);
restRequest.setArg(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY, account);
restRequest.setArg(RestUtils.InternalKeys.TARGET_CONTAINER_KEY, container);
BlobProperties blobProperties = RestUtils.buildBlobProperties(restRequest.getArgs());
long expectedTTL = Utils.Infinite_Time;
if (headers.has(RestUtils.Headers.TTL) && !JSONObject.NULL.equals(headers.get(RestUtils.Headers.TTL))) {
expectedTTL = headers.getLong(RestUtils.Headers.TTL);
}
assertEquals("Blob TTL does not match", expectedTTL, blobProperties.getTimeToLiveInSeconds());
assertEquals("Blob isPrivate does not match", !container.isCacheable(), blobProperties.isPrivate());
assertEquals("Blob service ID does not match", headers.getString(RestUtils.Headers.SERVICE_ID), blobProperties.getServiceId());
assertEquals("Blob content type does not match", headers.getString(RestUtils.Headers.AMBRY_CONTENT_TYPE), blobProperties.getContentType());
if (headers.has(RestUtils.Headers.OWNER_ID) && !JSONObject.NULL.equals(headers.get(RestUtils.Headers.OWNER_ID))) {
assertEquals("Blob owner ID does not match", headers.getString(RestUtils.Headers.OWNER_ID), blobProperties.getOwnerId());
}
assertEquals("Target account id does not match", account.getId(), blobProperties.getAccountId());
assertEquals("Target container id does not match", container.getId(), blobProperties.getContainerId());
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class InMemoryCloudDestinationErrorSimulationTest method doPut.
/**
* Do a put directly to the cloud mock servers.
* @param partitionId partition id
* @param expectedCode expected response error code
* @return the blob id
* @throws Exception
*/
private BlobId doPut(PartitionId partitionId, ServerErrorCode expectedCode) throws Exception {
int blobSize = 4096;
// direct put DataBlob
BlobType blobType = BlobType.DataBlob;
BlobProperties blobProperties = new BlobProperties(blobSize, "serviceId", "memberId", "contentType", false, Utils.Infinite_Time, Utils.getRandomShort(random), Utils.getRandomShort(random), false, null, null, null);
byte[] userMetadata = new byte[10];
random.nextBytes(userMetadata);
byte[] putContent = new byte[blobSize];
random.nextBytes(putContent);
ByteBuf blobContent = PooledByteBufAllocator.DEFAULT.heapBuffer(blobSize);
blobContent.writeBytes(putContent);
BlobId blobId = new BlobId(routerConfig.routerBlobidCurrentVersion, BlobId.BlobIdType.NATIVE, mockClusterMap.getLocalDatacenterId(), blobProperties.getAccountId(), blobProperties.getContainerId(), partitionId, blobProperties.isEncrypted(), blobType == BlobType.MetadataBlob ? BlobId.BlobDataType.METADATA : BlobId.BlobDataType.DATACHUNK);
ByteBuffer userMetadataBuf = ByteBuffer.wrap(userMetadata);
// send to Cloud destinations.
PutRequest request = new PutRequest(random.nextInt(), "clientId", blobId, blobProperties, userMetadataBuf.duplicate(), blobContent.retainedDuplicate(), blobContent.readableBytes(), blobType, null);
RequestInfo requestInfo = new RequestInfo(hostname, port, request, replica, null);
ResponseInfo responseInfo = sendAndWaitForResponses(requestInfo);
Assert.assertEquals("doPut should succeed.", responseInfo.getError(), null);
// PutResponse response = PutResponse.readFrom(new NettyByteBufDataInputStream(responseInfo.content()));
PutResponse response = (PutResponse) RouterUtils.mapToReceivedResponse((PutResponse) responseInfo.getResponse());
Assert.assertEquals("The PutResponse is not expected.", expectedCode, response.getError());
request.release();
blobContent.release();
return blobId;
}
use of com.github.ambry.messageformat.BlobProperties in project ambry by linkedin.
the class AmbrySecurityService method setHeadResponseHeaders.
/**
* Sets the required headers in the HEAD response.
* @param blobInfo the {@link BlobInfo} to refer to while setting headers.
* @param options the {@link GetBlobOptions} associated with the request.
* @param restRequest the {@link RestRequest} that was received.
* @param restResponseChannel the {@link RestResponseChannel} to set headers on.
* @throws RestServiceException if there was any problem setting the headers.
*/
private void setHeadResponseHeaders(BlobInfo blobInfo, GetBlobOptions options, RestRequest restRequest, RestResponseChannel restResponseChannel) throws RestServiceException {
BlobProperties blobProperties = blobInfo.getBlobProperties();
if (blobProperties.getContentType() != null) {
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_TYPE, blobProperties.getContentType());
}
if (blobProperties.getContentEncoding() != null) {
restResponseChannel.setHeader(Headers.CONTENT_ENCODING, blobProperties.getContentEncoding());
}
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();
}
restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, contentLength);
setBlobPropertiesHeaders(blobProperties, restResponseChannel);
setAccountAndContainerHeaders(restRequest, restResponseChannel);
restResponseChannel.setHeader(RestUtils.Headers.LIFE_VERSION, blobInfo.getLifeVersion());
}
Aggregations