use of com.pspace.ifs.ksan.gw.identity.S3Metadata in project ksan by infinistor.
the class ListObjects method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_LIST_OBJECT_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
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);
}
checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
DataListBuckets dataListBuckets = new DataListBuckets(s3Parameter);
dataListBuckets.extract();
S3ObjectList s3ObjectList = new S3ObjectList();
if (!Strings.isNullOrEmpty(dataListBuckets.getMaxkeys())) {
if (Integer.valueOf(dataListBuckets.getMaxkeys()) < 0) {
throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
}
s3ObjectList.setMaxKeys(dataListBuckets.getMaxkeys());
} else {
s3ObjectList.setMaxKeys(GWConstants.DEFAULT_MAX_KEYS);
}
s3ObjectList.setDelimiter(dataListBuckets.getDelimiter());
s3ObjectList.setEncodingType(dataListBuckets.getEncodingType());
s3ObjectList.setMarker(dataListBuckets.getMarker());
s3ObjectList.setPrefix(dataListBuckets.getPrefix());
logger.debug("delimiter : {}", dataListBuckets.getDelimiter());
logger.debug("marker : {}", dataListBuckets.getMarker());
logger.debug("prefix : {}", dataListBuckets.getPrefix());
logger.debug("maxKeys : {}", dataListBuckets.getMaxkeys());
s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
ObjectListParameter objectListParameter = listObject(bucket, s3ObjectList);
try (Writer writer = s3Parameter.getResponse().getWriter()) {
s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement(GWConstants.LIST_BUCKET_RESULT);
xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NAME, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), bucket));
if (s3ObjectList.getPrefix() == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getPrefix()));
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_KEYS, String.valueOf(s3ObjectList.getMaxKeys()));
if (s3ObjectList.getMarker() == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_MARKER);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_MARKER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getMarker()));
}
if (s3ObjectList.getDelimiter() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getDelimiter()));
}
if (s3ObjectList.getEncodingType() != null && s3ObjectList.getEncodingType().equals(GWConstants.URL)) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, s3ObjectList.getEncodingType());
}
if (objectListParameter.isTruncated()) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_MARKER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), objectListParameter.getNextMarker()));
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
}
for (S3Metadata s3Metadata : objectListParameter.getObjects()) {
xmlStreamWriter.writeStartElement(GWConstants.XML_CONTENTS);
logger.debug(s3Metadata.getName());
writeSimpleElement(xmlStreamWriter, GWConstants.KEY, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3Metadata.getName()));
if (s3Metadata.getLastModified() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
}
if (s3Metadata.getETag() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, s3Metadata.getSize().toString());
writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, s3Metadata.getTier());
writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
xmlStreamWriter.writeEndElement();
}
for (Entry<String, String> prefix : objectListParameter.getCommonPrefixes().entrySet()) {
xmlStreamWriter.writeStartElement(GWConstants.XML_COMMON_PREFIXES);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), prefix.getValue()));
xmlStreamWriter.writeEndElement();
logger.debug(GWConstants.LOG_LIST_OBJECT_PREFIX_ENCODING, s3ObjectList.getEncodingType(), prefix.getValue());
}
xmlStreamWriter.writeEndElement();
xmlStreamWriter.flush();
} catch (IOException | XMLStreamException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
}
use of com.pspace.ifs.ksan.gw.identity.S3Metadata in project ksan by infinistor.
the class ListParts method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_LIST_PARTS_START);
String bucket = s3Parameter.getBucketName();
if (!isExistBucket(bucket)) {
logger.error(GWConstants.LOG_BUCKET_IS_NOT_EXIST, bucket);
throw new GWException(GWErrorCode.NO_SUCH_BUCKET, s3Parameter);
}
initBucketInfo(bucket);
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);
}
checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
DataListParts dataListParts = new DataListParts(s3Parameter);
dataListParts.extract();
String maxParts = dataListParts.getMaxParts();
String partNumberMarker = dataListParts.getPartNumberMarker();
String uploadId = dataListParts.getUploadId();
int maxPartsValue = GWConstants.MAX_PART_VALUE;
if (!Strings.isNullOrEmpty(maxParts)) {
if (Integer.valueOf(maxParts) < 0) {
throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
}
maxPartsValue = Integer.valueOf(maxParts);
}
ResultParts resultPart = null;
ObjMultipart objMultipart = null;
Multipart multipart = null;
try {
objMultipart = new ObjMultipart(bucket);
multipart = objMultipart.getMultipart(uploadId);
if (multipart == null) {
logger.error(GWConstants.LOG_UPLOAD_NOT_FOUND, uploadId);
throw new GWException(GWErrorCode.NO_SUCH_UPLOAD, s3Parameter);
}
// check acl use multipart acl
resultPart = objMultipart.getParts(uploadId, partNumberMarker, maxPartsValue);
} catch (UnknownHostException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
} catch (Exception e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
}
String meta = multipart.getMeta();
ObjectMapper jsonMapper = new ObjectMapper();
S3Metadata s3Metadata;
try {
s3Metadata = jsonMapper.readValue(meta, S3Metadata.class);
} catch (JsonMappingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
}
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
try (Writer writer = s3Parameter.getResponse().getWriter()) {
s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement(GWConstants.LIST_PARTS_RESULT);
xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
writeSimpleElement(xmlStreamWriter, GWConstants.KEY, s3Parameter.getObjectName());
writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID, uploadId);
writeInitiatorStanza(xmlStreamWriter);
writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, GWConstants.AWS_TIER_STANTARD);
if (resultPart.isTruncated()) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_PART_NUMBER, String.valueOf(resultPart.getPartNumberMarker()));
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
}
for (Iterator<Map.Entry<Integer, Part>> it = resultPart.getListPart().entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Integer, Part> entry = it.next();
xmlStreamWriter.writeStartElement(GWConstants.PART);
writeSimpleElement(xmlStreamWriter, GWConstants.PARTNUMBER, String.valueOf(entry.getValue().getPartNumber()));
writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(entry.getValue().getLastModified()));
writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, entry.getValue().getPartETag());
writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, String.valueOf(entry.getValue().getPartSize()));
xmlStreamWriter.writeEndElement();
logger.debug(GWConstants.LOG_LIST_PARTS_PART_NUMBER, entry.getValue().getPartNumber());
logger.debug(GWConstants.LOG_LIST_PARTS_LAST_MODIFIED, formatDate(entry.getValue().getLastModified()));
logger.debug(GWConstants.LOG_LIST_PARTS_ETAG, entry.getValue().getPartETag());
logger.debug(GWConstants.LOG_LIST_PARTS_SIZE, String.valueOf(entry.getValue().getPartSize()));
}
xmlStreamWriter.writeEndElement();
xmlStreamWriter.flush();
} catch (IOException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
} catch (XMLStreamException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
}
use of com.pspace.ifs.ksan.gw.identity.S3Metadata in project ksan by infinistor.
the class PutObjectTagging method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_PUT_OBJECT_TAGGING_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
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);
}
checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
String object = s3Parameter.getObjectName();
DataPutObjectTagging dataPutObjectTagging = new DataPutObjectTagging(s3Parameter);
dataPutObjectTagging.extract();
String taggingCount = GWConstants.TAGGING_INIT;
String taggingXml = dataPutObjectTagging.getTaggingXml();
try {
Tagging tagging = new XmlMapper().readValue(taggingXml, Tagging.class);
// 중복 지우기 item이 10개 미만이기 때문에 for loop가 빠름
if (tagging != null) {
if (tagging.tagset != null && tagging.tagset.tags != null) {
for (Tag t : tagging.tagset.tags) {
// key, value 길이 체크
if (t.key.length() > GWConstants.TAG_KEY_MAX) {
throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
}
if (t.value.length() > GWConstants.TAG_VALUE_MAX) {
throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
}
}
}
if (tagging.tagset != null && tagging.tagset.tags != null) {
if (tagging.tagset.tags.size() > GWConstants.TAG_MAX_SIZE) {
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
taggingCount = String.valueOf(tagging.tagset.tags.size());
}
}
} catch (IOException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
String versionId = dataPutObjectTagging.getVersionId();
Metadata objMeta = null;
if (Strings.isNullOrEmpty(versionId)) {
objMeta = open(bucket, object);
} else {
objMeta = open(bucket, object, versionId);
}
S3Metadata s3Metadata = null;
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);
}
s3Metadata.setTaggingCount(taggingCount);
ObjectMapper jsonMapper = new ObjectMapper();
String jsonMeta = "";
try {
jsonMeta = jsonMapper.writeValueAsString(s3Metadata);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
objMeta.setMeta(jsonMeta);
objMeta.setTag(taggingXml);
updateObjectTagging(objMeta);
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
use of com.pspace.ifs.ksan.gw.identity.S3Metadata in project ksan by infinistor.
the class ListObjectsV2 method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_LIST_OBJECT_V2_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
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);
}
checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
DataListObjectV2 dataListObjectV2 = new DataListObjectV2(s3Parameter);
dataListObjectV2.extract();
// read header
S3ObjectList s3ObjectList = new S3ObjectList();
if (!Strings.isNullOrEmpty(dataListObjectV2.getMaxKeys())) {
if (Integer.valueOf(dataListObjectV2.getMaxKeys()) < 0) {
throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
}
s3ObjectList.setMaxKeys(dataListObjectV2.getMaxKeys());
} else {
s3ObjectList.setMaxKeys(GWConstants.DEFAULT_MAX_KEYS);
}
s3ObjectList.setContinuationToken(dataListObjectV2.getContinuationToken());
s3ObjectList.setDelimiter(dataListObjectV2.getDelimiter());
s3ObjectList.setEncodingType(dataListObjectV2.getEncodingType());
s3ObjectList.setPrefix(dataListObjectV2.getPrefix());
s3ObjectList.setStartAfter(dataListObjectV2.getStartAfter());
s3ObjectList.setFetchOwner(dataListObjectV2.getFetchOwner());
s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
ObjectListParameter objectListParameter = listObjectV2(bucket, s3ObjectList);
try (Writer writer = s3Parameter.getResponse().getWriter()) {
s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement(GWConstants.LIST_BUCKET_RESULT);
xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NAME, bucket);
String encodingType = s3ObjectList.getEncodingType();
String prefix = s3ObjectList.getPrefix();
if (prefix == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(encodingType, prefix));
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_KEYS, String.valueOf(s3ObjectList.getMaxKeys()));
if (s3ObjectList.getStartAfter() == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_START_AFTER);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_START_AFTER, GWUtils.encodeObjectName(encodingType, s3ObjectList.getStartAfter()));
}
if (s3ObjectList.getContinuationToken() == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_CONTINUEATION_TOKEN);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_CONTINUEATION_TOKEN, GWUtils.encodeObjectName(encodingType, s3ObjectList.getContinuationToken()));
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_COUNT, String.valueOf(objectListParameter.getObjects().size()));
if (s3ObjectList.getDelimiter() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeObjectName(encodingType, s3ObjectList.getDelimiter()));
}
if (encodingType != null && encodingType.equals(GWConstants.URL)) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, encodingType);
}
if (objectListParameter.isTruncated()) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_CONTINUATION_TOKEN, GWUtils.encodeObjectName(encodingType, objectListParameter.getNextMarker()));
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
}
for (S3Metadata s3Metadata : objectListParameter.getObjects()) {
xmlStreamWriter.writeStartElement(GWConstants.XML_CONTENTS);
writeSimpleElement(xmlStreamWriter, GWConstants.KEY, GWUtils.encodeObjectName(encodingType, s3Metadata.getName()));
if (s3Metadata.getLastModified() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
}
if (s3Metadata.getETag() != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
}
if (!Strings.isNullOrEmpty(s3ObjectList.getFetchOwner()) && s3ObjectList.getFetchOwner().equals(GWConstants.XML_TRUE)) {
writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, s3Metadata.getContentLength().toString());
writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, s3Metadata.getTier());
xmlStreamWriter.writeEndElement();
}
for (Entry<String, String> entry : objectListParameter.getCommonPrefixes().entrySet()) {
xmlStreamWriter.writeStartElement(GWConstants.XML_COMMON_PREFIXES);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(encodingType, entry.getValue()));
xmlStreamWriter.writeEndElement();
}
xmlStreamWriter.writeEndElement();
xmlStreamWriter.flush();
} catch (IOException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
} catch (XMLStreamException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
}
use of com.pspace.ifs.ksan.gw.identity.S3Metadata in project ksan by infinistor.
the class PostObject method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_POST_OBJECT_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
S3Bucket s3Bucket = new S3Bucket();
s3Bucket.setCors(getBucketInfo().getCors());
s3Bucket.setAccess(getBucketInfo().getAccess());
s3Parameter.setBucket(s3Bucket);
DataPostObject dataPostObject = new DataPostObject(s3Parameter);
dataPostObject.extract();
String object = dataPostObject.getKey();
s3Parameter.setObjectName(object);
logger.debug(GWConstants.LOG_BUCKET_OBJECT, bucket, object);
if (Strings.isNullOrEmpty(dataPostObject.getKey())) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
logger.info("policy list : {}", dataPostObject.getPolicy());
if (!Strings.isNullOrEmpty(dataPostObject.getPolicy())) {
Decoder decoder = Base64.getDecoder();
byte[] bytePostPolicy = decoder.decode(dataPostObject.getPolicy());
String postPolicy = new String(bytePostPolicy);
ObjectMapper jsonMapper = new ObjectMapper();
PostPolicy postPolicyJson = null;
try {
postPolicyJson = jsonMapper.readValue(postPolicy, PostPolicy.class);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
Map<String, String> conditionMap = new HashMap<String, String>();
if (postPolicyJson.conditions == null) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
if (postPolicyJson.conditions.size() == 0) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
for (Object o : postPolicyJson.conditions) {
// check
logger.info("conditions ==> className(" + o.getClass().getName() + ")");
if (o.getClass().getName().equals("java.util.LinkedHashMap")) {
@SuppressWarnings("unchecked") Map<String, String> policyMap = (HashMap<String, String>) o;
for (Map.Entry<String, String> s : policyMap.entrySet()) {
logger.info("conditions ==> key(" + s.getKey() + "), value(" + s.getValue() + ")");
dataPostObject.checkPolicy(s.getKey(), s.getValue());
conditionMap.put(s.getKey().toLowerCase(), s.getValue());
}
} else if (o.getClass().getName().equals("java.util.ArrayList")) {
@SuppressWarnings("unchecked") List<Object> policyList = (List<Object>) o;
if (!((String) policyList.get(0)).equalsIgnoreCase("starts-with") && !((String) policyList.get(0)).equalsIgnoreCase("eq") && !((String) policyList.get(0)).equalsIgnoreCase("content-length-range")) {
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
if (((String) policyList.get(0)).equalsIgnoreCase("eq")) {
logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
dataPostObject.checkPolicy((String) policyList.get(1), (String) policyList.get(2));
} else if (((String) policyList.get(0)).equalsIgnoreCase("starts-with")) {
logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
dataPostObject.checkPolityStarts((String) policyList.get(1), (String) policyList.get(2));
} else if (((String) policyList.get(0)).equalsIgnoreCase("content-length-range")) {
if (policyList.size() != 3) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
logger.info("conditions ==> cond(" + policyList.get(0) + "), value1 (" + policyList.get(1) + "), value2 (" + policyList.get(2) + ")");
if ((int) policyList.get(1) < 0) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
if (dataPostObject.getPayload().length < (int) policyList.get(1)) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
if ((int) policyList.get(2) < 0) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
if (dataPostObject.getPayload().length > (int) policyList.get(2)) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
}
} else {
logger.info(o.getClass().getName());
}
}
if (Strings.isNullOrEmpty(postPolicyJson.expiration)) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
} else {
dataPostObject.setExpiration(postPolicyJson.getExpiration());
}
// bucket check
if (Strings.isNullOrEmpty(conditionMap.get(GWConstants.CATEGORY_BUCKET))) {
logger.info(GWErrorCode.ACCESS_DENIED.getMessage());
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
}
if (!Strings.isNullOrEmpty(dataPostObject.getAccessKey())) {
if (Strings.isNullOrEmpty(dataPostObject.getSignature())) {
logger.info(GWErrorCode.BAD_REQUEST.getMessage());
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
// signing check
S3Signing s3signing = new S3Signing(s3Parameter);
s3Parameter = s3signing.validatePost(dataPostObject);
if (!isGrantBucket(String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
} else {
if (!isGrantBucket(GWConstants.LOG_REQUEST_ROOT_ID, GWConstants.GRANT_WRITE)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
}
s3Parameter.setInputStream(new ByteArrayInputStream(dataPostObject.getPayload()));
String cacheControl = dataPostObject.getCacheControl();
String contentDisposition = dataPostObject.getContentDisposition();
String contentEncoding = dataPostObject.getContentEncoding();
String contentLanguage = dataPostObject.getContentLanguage();
String contentType = dataPostObject.getContentType();
String customerAlgorithm = dataPostObject.getServerSideEncryptionCustomerAlgorithm();
String customerKey = dataPostObject.getServerSideEncryptionCustomerKey();
String customerKeyMD5 = dataPostObject.getServerSideEncryptionCustomerKeyMD5();
String serversideEncryption = dataPostObject.getServerSideEncryption();
S3Metadata s3Metadata = new S3Metadata();
s3Metadata.setOwnerId(Long.toString(s3Parameter.getUser().getUserId()));
s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
s3Metadata.setUserMetadataMap(dataPostObject.getUserMetadata());
if (!Strings.isNullOrEmpty(serversideEncryption)) {
if (!serversideEncryption.equalsIgnoreCase(GWConstants.AES256)) {
logger.error(GWErrorCode.NOT_IMPLEMENTED.getMessage() + GWConstants.SERVER_SIDE_OPTION);
throw new GWException(GWErrorCode.NOT_IMPLEMENTED, s3Parameter);
} else {
s3Metadata.setServersideEncryption(serversideEncryption);
}
}
if (!Strings.isNullOrEmpty(cacheControl)) {
s3Metadata.setCacheControl(cacheControl);
}
if (!Strings.isNullOrEmpty(contentDisposition)) {
s3Metadata.setContentDisposition(contentDisposition);
}
if (!Strings.isNullOrEmpty(contentEncoding)) {
s3Metadata.setContentEncoding(contentEncoding);
}
if (!Strings.isNullOrEmpty(contentLanguage)) {
s3Metadata.setContentLanguage(contentLanguage);
}
if (!Strings.isNullOrEmpty(contentType)) {
s3Metadata.setContentType(contentType);
}
if (!Strings.isNullOrEmpty(customerAlgorithm)) {
s3Metadata.setCustomerAlgorithm(customerAlgorithm);
}
if (!Strings.isNullOrEmpty(customerKey)) {
s3Metadata.setCustomerKey(customerKey);
}
if (!Strings.isNullOrEmpty(customerKeyMD5)) {
s3Metadata.setCustomerKeyMD5(customerKeyMD5);
}
String aclXml = GWUtils.makeAclXml(accessControlPolicy, null, dataPostObject.getAclKeyword(), null, dataPostObject.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataPostObject.getGrantRead(), dataPostObject.getGrantWrite(), dataPostObject.getGrantFullControl(), dataPostObject.getGrantReadAcp(), dataPostObject.getGrantWriteAcp(), s3Parameter);
String bucketEncryption = getBucketInfo().getEncryption();
S3ServerSideEncryption encryption = new S3ServerSideEncryption(bucketEncryption, serversideEncryption, customerAlgorithm, customerKey, customerKeyMD5, s3Parameter);
encryption.build();
// Tagging information
String taggingCount = GWConstants.TAGGING_INIT;
String taggingxml = "";
Tagging tagging = new Tagging();
tagging.tagset = new TagSet();
try {
if (dataPostObject.getTagging() != null)
tagging = new XmlMapper().readValue(dataPostObject.getTagging(), Tagging.class);
} catch (JsonProcessingException e) {
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
try {
if (tagging != null)
taggingxml = new XmlMapper().writeValueAsString(tagging);
} catch (JsonProcessingException e) {
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
if (tagging != null) {
if (tagging.tagset != null && tagging.tagset.tags != null) {
for (Tag t : tagging.tagset.tags) {
// key, value 길이 체크
if (t.key.length() > 128) {
throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
}
if (t.value.length() > 256) {
throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
}
}
}
if (tagging.tagset != null && tagging.tagset.tags != null) {
if (tagging.tagset.tags.size() > 10) {
throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
}
taggingCount = String.valueOf(tagging.tagset.tags.size());
}
}
String versioningStatus = getBucketVersioning(bucket);
String versionId = null;
Metadata objMeta = null;
try {
// check exist object
objMeta = open(bucket, object);
if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
versionId = String.valueOf(System.nanoTime());
} else {
versionId = GWConstants.VERSIONING_DISABLE_TAIL;
}
} catch (GWException e) {
logger.info(e.getMessage());
if (GWConfig.getReplicaCount() > 1) {
objMeta = create(bucket, object);
} else {
objMeta = createLocal(bucket, object);
}
if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
versionId = String.valueOf(System.nanoTime());
} else {
versionId = GWConstants.VERSIONING_DISABLE_TAIL;
}
}
S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, versionId, encryption);
S3Object s3Object = objectOperation.putObject();
s3Metadata.setETag(s3Object.getEtag());
s3Metadata.setSize(s3Object.getFileSize());
s3Metadata.setContentLength(s3Object.getFileSize());
s3Metadata.setTier(GWConstants.AWS_TIER_STANTARD);
s3Metadata.setLastModified(s3Object.getLastModified());
s3Metadata.setDeleteMarker(s3Object.getDeleteMarker());
s3Metadata.setVersionId(s3Object.getVersionId());
s3Metadata.setTaggingCount(taggingCount);
if (encryption.isEnableSSEServer()) {
s3Metadata.setServersideEncryption(GWConstants.AES256);
}
s3Parameter.setFileSize(s3Object.getFileSize());
ObjectMapper jsonMapper = new ObjectMapper();
String jsonmeta = "";
try {
jsonmeta = jsonMapper.writeValueAsString(s3Metadata);
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
logger.debug(GWConstants.LOG_PUT_OBJECT_PRIMARY_DISK_ID, objMeta.getPrimaryDisk().getId());
try {
objMeta.set(s3Object.getEtag(), taggingxml, jsonmeta, aclXml, s3Object.getFileSize());
objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_FILE, true);
insertObject(bucket, object, objMeta);
logger.debug(GWConstants.LOG_PUT_OBJECT_INFO, bucket, object, s3Object.getFileSize(), s3Object.getEtag(), aclXml, versionId);
} catch (GWException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
}
s3Parameter.getResponse().addHeader(HttpHeaders.ETAG, GWUtils.maybeQuoteETag(s3Object.getEtag()));
if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, s3Object.getVersionId());
logger.debug(GWConstants.LOG_PUT_OBJECT_VERSIONID, s3Object.getVersionId());
}
if (!Strings.isNullOrEmpty(dataPostObject.getSuccessActionRedirect())) {
try {
s3Parameter.getResponse().sendRedirect(dataPostObject.getSuccessActionRedirect() + GWConstants.PARAMETER_BUCKET + bucket + GWConstants.PARAMETER_KEY + s3Parameter.getObjectName() + GWConstants.PARAMETER_ETAG + s3Metadata.getETag() + GWConstants.ENCODING_DOUBLE_QUOTE);
} catch (IOException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
}
dataPostObject.setSuccessActionStatus(GWConstants.STATUS_SC_OK);
}
if (!Strings.isNullOrEmpty(dataPostObject.getSuccessActionStatus())) {
switch(Integer.parseInt(dataPostObject.getSuccessActionStatus())) {
case HttpServletResponse.SC_OK:
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
break;
case HttpServletResponse.SC_CREATED:
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_CREATED);
break;
case HttpServletResponse.SC_NO_CONTENT:
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
break;
default:
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
break;
}
} else {
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
Aggregations