Search in sources :

Example 1 with ResultParts

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);
    }
}
Also used : ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) Multipart(com.pspace.ifs.ksan.gw.object.multipart.Multipart) ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataListParts(com.pspace.ifs.ksan.gw.data.DataListParts) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UnknownHostException(java.net.UnknownHostException) ResultParts(com.pspace.ifs.ksan.gw.object.multipart.ResultParts) IOException(java.io.IOException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Part(com.pspace.ifs.ksan.gw.object.multipart.Part) Map(java.util.Map) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 2 with ResultParts

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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Part(com.pspace.ifs.ksan.gw.object.multipart.Part) Iterator(java.util.Iterator) ResultParts(com.pspace.ifs.ksan.gw.object.multipart.ResultParts) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document)

Example 3 with 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;
}
Also used : Part(com.pspace.ifs.ksan.gw.object.multipart.Part) ResultParts(com.pspace.ifs.ksan.gw.object.multipart.ResultParts)

Aggregations

Part (com.pspace.ifs.ksan.gw.object.multipart.Part)3 ResultParts (com.pspace.ifs.ksan.gw.object.multipart.ResultParts)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BasicDBObject (com.mongodb.BasicDBObject)1 FindIterable (com.mongodb.client.FindIterable)1 DataListParts (com.pspace.ifs.ksan.gw.data.DataListParts)1 GWException (com.pspace.ifs.ksan.gw.exception.GWException)1 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)1 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)1 Multipart (com.pspace.ifs.ksan.gw.object.multipart.Multipart)1 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 UnknownHostException (java.net.UnknownHostException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1