use of com.pspace.ifs.ksan.gw.data.DataListMultipartUploads in project ksan by infinistor.
the class ListMultipartUploads method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_LIST_MULTIPART_UPLOADS_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);
DataListMultipartUploads dataListMultipartUploads = new DataListMultipartUploads(s3Parameter);
dataListMultipartUploads.extract();
String delimiter = dataListMultipartUploads.getDelimiter();
String encodingType = dataListMultipartUploads.getEncodingType();
String prefix = dataListMultipartUploads.getPrefix();
String keyMarker = dataListMultipartUploads.getKeyMarker();
String uploadIdMarker = dataListMultipartUploads.getUploadIdMarker();
String maxUploads = dataListMultipartUploads.getMaxUploads();
if (Strings.isNullOrEmpty(maxUploads)) {
maxUploads = GWConstants.DEFAULT_MAX_KEYS;
} else {
if (Integer.valueOf(maxUploads) < 0) {
throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
}
}
ResultUploads resultUploads = null;
ObjMultipart objMultipart = null;
try {
objMultipart = new ObjMultipart(bucket);
resultUploads = objMultipart.getUploads(bucket, delimiter, prefix, keyMarker, uploadIdMarker, Integer.valueOf(maxUploads));
} 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);
}
s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
try (Writer writer = s3Parameter.getResponse().getWriter()) {
s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeStartElement(GWConstants.LIST_MULTIPART_UPLOADS_RESULT);
xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
if (prefix == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeBlob(encodingType, prefix));
}
writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_UPLOADS, String.valueOf(maxUploads));
if (keyMarker == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_KEY_MARKER);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_MARKER, GWUtils.encodeBlob(encodingType, keyMarker));
}
if (uploadIdMarker == null) {
xmlStreamWriter.writeEmptyElement(GWConstants.XML_UPLOADID_MARKER);
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID_MARKER, GWUtils.encodeBlob(encodingType, uploadIdMarker));
}
if (delimiter != null) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeBlob(encodingType, delimiter));
}
if (encodingType != null && encodingType.equals(GWConstants.URL)) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, encodingType);
}
if (resultUploads.isTruncated()) {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_KEY_MARKER, GWUtils.encodeBlob(encodingType, resultUploads.getKeyMarker()));
writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_UPLOADID_MARKER, GWUtils.encodeBlob(encodingType, resultUploads.getUploadIdMarker()));
} else {
writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
}
for (Upload upload : resultUploads.getList()) {
xmlStreamWriter.writeStartElement(GWConstants.XML_UPLOAD);
writeSimpleElement(xmlStreamWriter, GWConstants.KEY, upload.getObject());
writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID, upload.getUploadId());
writeInitiatorStanza(xmlStreamWriter);
writeOwnerInfini(xmlStreamWriter, upload.getOwnerID(), upload.getOwnerName());
writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, GWConstants.AWS_TIER_STANTARD);
writeSimpleElement(xmlStreamWriter, GWConstants.XML_INITIATED, formatDate(upload.getChangeTime()));
xmlStreamWriter.writeEndElement();
logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_KEY, upload.getObject());
logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_UPLOADID, upload.getUploadId());
logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_CHANGE_TIME, formatDate(upload.getChangeTime()));
}
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);
}
}
Aggregations