use of com.pspace.ifs.ksan.gw.data.DataGetObject in project ksan by infinistor.
the class GetObject method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_GET_OBJECT_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
String object = s3Parameter.getObjectName();
logger.debug(GWConstants.LOG_BUCKET_OBJECT, bucket, object);
S3Bucket s3Bucket = new S3Bucket();
s3Bucket.setCors(getBucketInfo().getCors());
s3Bucket.setAccess(getBucketInfo().getAccess());
s3Parameter.setBucket(s3Bucket);
GWUtils.checkCors(s3Parameter);
if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
DataGetObject dataGetObject = new DataGetObject(s3Parameter);
dataGetObject.extract();
String versionId = dataGetObject.getVersionId();
String range = dataGetObject.getRange();
String ifMatch = dataGetObject.getIfMatch();
String ifNoneMatch = dataGetObject.getIfNoneMatch();
String ifModifiedSince = dataGetObject.getIfModifiedSince();
String ifUnmodifiedSince = dataGetObject.getIfUnmodifiedSince();
String versioningStatus = getBucketVersioning(bucket);
Metadata objMeta = null;
if (Strings.isNullOrEmpty(versionId)) {
objMeta = open(bucket, object);
versionId = objMeta.getVersionId();
} else {
objMeta = open(bucket, object, versionId);
}
logger.debug(GWConstants.LOG_OBJECT_META, objMeta.toString());
objMeta.setAcl(GWUtils.makeOriginalXml(objMeta.getAcl(), s3Parameter));
checkGrantObject(s3Parameter.isPublicAccess(), objMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
S3Metadata s3Metadata = null;
// meta info
ObjectMapper objectMapper = new ObjectMapper();
try {
logger.debug(GWConstants.LOG_META, objMeta.getMeta());
s3Metadata = objectMapper.readValue(objMeta.getMeta(), S3Metadata.class);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
// check customer-key
if (!Strings.isNullOrEmpty(s3Metadata.getCustomerKey())) {
if (!Strings.isNullOrEmpty(dataGetObject.getServerSideEncryptionCustomerKey())) {
if (!s3Metadata.getCustomerKey().equals(dataGetObject.getServerSideEncryptionCustomerKey())) {
logger.warn(GWConstants.LOG_GET_OBJECT_CUSTOMER_KEY_NO_MATCH);
throw new GWException(GWErrorCode.KEY_DOES_NOT_MATCH, s3Parameter);
}
} else {
logger.warn(GWConstants.ENCRYPTION_CUSTOMER_KEY_IS_NULL);
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
}
// Check match
if (!Strings.isNullOrEmpty(ifMatch)) {
logger.debug(GWConstants.LOG_GET_OBJECT_IF_MATCH_ETAG, s3Metadata.getETag(), ifMatch.replace(GWConstants.DOUBLE_QUOTE, ""));
if (!s3Metadata.getETag().equals(ifMatch.replace(GWConstants.DOUBLE_QUOTE, ""))) {
logger.error(GWConstants.LOG_GET_OBJECT_ETAG_DIFFERENT);
throw new GWException(GWErrorCode.PRECONDITION_FAILED, s3Parameter);
}
}
if (!Strings.isNullOrEmpty(ifNoneMatch)) {
logger.debug(GWConstants.LOG_GET_OBJECT_IF_NONE_MATCH_ETAG, s3Metadata.getETag(), ifNoneMatch.replace(GWConstants.DOUBLE_QUOTE, ""));
if (s3Metadata.getETag().equals(ifNoneMatch.replace(GWConstants.DOUBLE_QUOTE, ""))) {
logger.error(GWConstants.LOG_GET_OBJECT_ETAG_SAME);
throw new GWException(GWErrorCode.DOES_NOT_MATCH, String.format(GWConstants.LOG_ETAG_IS_MISMATCH), s3Parameter);
}
}
if (!Strings.isNullOrEmpty(ifModifiedSince)) {
Date modifiedSince = new Date(ifModifiedSince);
if (s3Metadata.getLastModified().before(modifiedSince)) {
throw new GWException(GWErrorCode.DOES_NOT_MATCH, String.format(GWConstants.LOG_MATCH_BEFORE, s3Metadata.getLastModified(), modifiedSince), s3Parameter);
}
}
if (!Strings.isNullOrEmpty(ifUnmodifiedSince)) {
Date unmodifiedSince = new Date(ifUnmodifiedSince);
if (s3Metadata.getLastModified().after(unmodifiedSince)) {
throw new GWException(GWErrorCode.PRECONDITION_FAILED, String.format(GWConstants.LOG_MATCH_AFTER, s3Metadata.getLastModified(), unmodifiedSince), s3Parameter);
}
}
ResultRange resultRange = new ResultRange(range, s3Metadata, s3Parameter);
addMetadataToResponse(s3Parameter.getResponse(), s3Metadata, resultRange.getContentLengthHeaders(), resultRange.getStreamSize());
S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, versionId, null);
try {
objectOperation.getObject(resultRange.getS3Range());
} catch (Exception e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
s3Parameter.getResponse().setStatus(resultRange.getStatus());
}
Aggregations