use of com.pspace.ifs.ksan.gw.object.multipart.ResultParts 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.object.multipart.ResultParts in project ksan by infinistor.
the class MongoDataRepository method getParts.
// SELECT changeTime, etag, size, partNo FROM MULTIPARTS WHERE uploadid=? AND partNo > ? ORDER BY partNo LIMIT ?")
@Override
public ResultParts getParts(String uploadId, int partNumberMarker, int maxParts) throws SQLException {
MongoCollection<Document> multip;
ResultParts resultParts = new ResultParts(uploadId, maxParts);
resultParts.setListPart(new TreeMap<>());
multip = getMultiPartUploadCollection();
if (multip == null)
return resultParts;
BasicDBObject sortList = new BasicDBObject(PARTNO, 1);
FindIterable fit = multip.find(Filters.and(Filters.eq(UPLOADID, uploadId), Filters.gt(PARTNO, partNumberMarker))).limit(maxParts + 1).sort(sortList);
Iterator it = fit.iterator();
int count = 0;
resultParts.setTruncated(false);
while ((it.hasNext())) {
Document doc = (Document) it.next();
count++;
if (count > maxParts) {
resultParts.setPartNumberMarker(doc.getString(PARTNO));
resultParts.setTruncated(true);
break;
}
Part part = new Part();
part.setLastModified((Date) doc.getDate(LASTMODIFIED));
part.setPartETag(doc.getString(ETAG));
part.setPartSize(doc.getLong(SIZE));
part.setPartNumber(doc.getInteger(PARTNO));
part.setDiskID(PDISKID);
resultParts.getListPart().put(part.getPartNumber(), part);
}
return resultParts;
}
use of com.pspace.ifs.ksan.gw.object.multipart.ResultParts in project ksan by infinistor.
the class MysqlDataRepository method getParts.
// TO BE
@Override
public ResultParts getParts(String uploadId, int partNumberMarker, int maxParts) throws SQLException {
ResultParts resultParts = new ResultParts(uploadId, maxParts);
resultParts.setListPart(new TreeMap<Integer, Part>());
pstGetPartsMax.clearParameters();
pstGetPartsMax.setString(1, uploadId);
/*if (Strings.isNullOrEmpty(partNumberMarker)) {
pstGetPartsMax.setInt(2, 0);
} else*/
{
pstGetPartsMax.setInt(2, Integer.valueOf(partNumberMarker));
}
pstGetPartsMax.setInt(3, maxParts + 1);
ResultSet rs = pstGetPartsMax.executeQuery();
resultParts.setTruncated(false);
int count = 0;
while (rs.next()) {
count++;
if (count > maxParts) {
resultParts.setPartNumberMarker(String.valueOf(rs.getInt(4)));
resultParts.setTruncated(true);
break;
}
Part part = new Part();
part.setLastModified((Date) rs.getObject(1));
part.setPartETag(rs.getString(2));
part.setPartSize(rs.getLong(3));
part.setPartNumber(rs.getInt(4));
part.setDiskID(rs.getString(5));
resultParts.getListPart().put(part.getPartNumber(), part);
}
return resultParts;
}
Aggregations