Search in sources :

Example 1 with ObjMultipart

use of com.pspace.ifs.ksan.objmanager.ObjMultipart in project ksan by infinistor.

the class AbortMultipartUpload method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_ABORT_MULTIPART_UPLOAD_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    String object = s3Parameter.getObjectName();
    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);
    }
    DataAbortMultipartUpload dataAbortMultipartUpload = new DataAbortMultipartUpload(s3Parameter);
    dataAbortMultipartUpload.extract();
    String uploadId = dataAbortMultipartUpload.getUploadId();
    s3Parameter.setUploadId(uploadId);
    ObjMultipart objMultipart = null;
    SortedMap<Integer, Part> listPart = null;
    boolean isUploadId = true;
    try {
        objMultipart = new ObjMultipart(bucket);
        if (!objMultipart.isUploadId(uploadId)) {
            isUploadId = false;
            throw new GWException(GWErrorCode.NO_SUCH_UPLOAD, s3Parameter);
        }
        listPart = objMultipart.getParts(uploadId);
    } catch (Exception e) {
        if (!isUploadId) {
            throw new GWException(GWErrorCode.NO_SUCH_UPLOAD, s3Parameter);
        }
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    // get Paths
    Metadata objMeta = createLocal(bucket, object);
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, null, s3Parameter, null, null);
    objectOperation.abortMultipart(listPart);
    objMultipart.abortMultipartUpload(uploadId);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Also used : ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) DataAbortMultipartUpload(com.pspace.ifs.ksan.gw.data.DataAbortMultipartUpload) Part(com.pspace.ifs.ksan.gw.object.multipart.Part) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 2 with ObjMultipart

use of com.pspace.ifs.ksan.objmanager.ObjMultipart in project ksan by infinistor.

the class CompleteMultipartUpload method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    String object = s3Parameter.getObjectName();
    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);
    }
    DataCompleteMultipartUpload dataCompleteMultipartUpload = new DataCompleteMultipartUpload(s3Parameter);
    dataCompleteMultipartUpload.extract();
    String uploadId = dataCompleteMultipartUpload.getUploadId();
    s3Parameter.setUploadId(uploadId);
    String multipartXml = dataCompleteMultipartUpload.getMultipartXml();
    XmlMapper xmlMapper = new XmlMapper();
    CompleteMultipartUploadRequest completeMultipartUpload = new CompleteMultipartUploadRequest();
    try {
        completeMultipartUpload = xmlMapper.readValue(multipartXml, CompleteMultipartUploadRequest.class);
    } catch (JsonMappingException e) {
        PrintStack.logging(logger, e);
        new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    if (completeMultipartUpload.parts == null || completeMultipartUpload.parts.size() == 0) {
        logger.error(GWErrorCode.MALFORMED_X_M_L.getMessage() + GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_PART_NO_EXIST);
        throw new GWException(GWErrorCode.MALFORMED_X_M_L, s3Parameter);
    }
    SortedMap<Integer, Part> xmlListPart = new TreeMap<Integer, Part>();
    for (CompleteMultipartUploadRequest.Part sPart : completeMultipartUpload.parts) {
        Part op = new Part();
        op.setPartETag(sPart.eTag);
        op.setPartNumber(sPart.partNumber);
        xmlListPart.put(sPart.partNumber, op);
    }
    SortedMap<Integer, Part> listPart = null;
    ObjMultipart objMultipart = null;
    try {
        objMultipart = new ObjMultipart(bucket);
        listPart = objMultipart.getParts(uploadId);
    } 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);
    }
    logger.info(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_XML_PARTS_SIZE, completeMultipartUpload.parts.size());
    logger.info(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_PARTS_SIZE, listPart.size());
    if (completeMultipartUpload.parts.size() > listPart.size()) {
        logger.warn(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_LESS_THAN);
        throw new GWException(GWErrorCode.INVALID_PART, s3Parameter);
    } else if (completeMultipartUpload.parts.size() < listPart.size()) {
        logger.warn(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_GREATER_THAN);
        throw new GWException(GWErrorCode.INVALID_PART, s3Parameter);
    }
    for (Iterator<Map.Entry<Integer, Part>> it = xmlListPart.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Integer, Part> entry = it.next();
        String eTag = entry.getValue().getPartETag().replace(GWConstants.DOUBLE_QUOTE, "");
        if (listPart.containsKey(entry.getKey())) {
            if (eTag.compareTo(listPart.get(entry.getKey()).getPartETag()) == 0) {
                if (listPart.get(entry.getKey()).getPartSize() < GWConstants.PARTS_MIN_SIZE && entry.getKey() < listPart.size()) {
                    logger.error(GWErrorCode.ENTITY_TOO_SMALL.getMessage());
                    throw new GWException(GWErrorCode.ENTITY_TOO_SMALL, s3Parameter);
                }
            } else {
                throw new GWException(GWErrorCode.INVALID_PART, s3Parameter);
            }
        } else {
            throw new GWException(GWErrorCode.INVALID_PART, s3Parameter);
        }
    }
    // get Acl, Meta data
    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);
        }
    } catch (UnknownHostException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    String acl = multipart.getAcl();
    String meta = multipart.getMeta();
    // check bucket versioning, and set versionId
    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());
        objMeta = createLocal(bucket, object);
        if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
            versionId = String.valueOf(System.nanoTime());
        } else {
            versionId = GWConstants.VERSIONING_DISABLE_TAIL;
        }
    }
    if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
        logger.info(GWConstants.LOG_COMPLETE_MULTIPART_VERSION_ID, versionId);
        s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, versionId);
    }
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        final AtomicReference<S3Object> s3Object = new AtomicReference<>();
        final AtomicReference<Exception> S3Excp = new AtomicReference<>();
        S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, null, s3Parameter, versionId, null);
        ObjectMapper jsonMapper = new ObjectMapper();
        S3Metadata s3Metadata = jsonMapper.readValue(meta, S3Metadata.class);
        SortedMap<Integer, Part> constListPart = listPart;
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    s3Object.set(objectOperation.completeMultipart(constListPart));
                } catch (Exception e) {
                    S3Excp.set(e);
                }
            }
        };
        thread.start();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.COMPLETE_MULTIPART_UPLOAD_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        while (thread.isAlive()) {
            try {
                thread.join(500);
            } catch (InterruptedException ie) {
                PrintStack.logging(logger, ie);
                throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
            }
            xmlStreamWriter.writeCharacters(GWConstants.NEWLINE);
        }
        if (S3Excp.get() != null) {
            PrintStack.logging(logger, S3Excp.get());
            logger.error(S3Excp.get().getMessage());
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.LOCATION, GWConstants.HTTP + bucket + GWConstants.S3_AMAZON_AWS_COM + object);
        writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
        writeSimpleElement(xmlStreamWriter, GWConstants.KEY, object);
        // make ETag
        StringBuilder sb = new StringBuilder();
        for (Iterator<Map.Entry<Integer, Part>> it = listPart.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<Integer, Part> entry = it.next();
            sb.append(entry.getValue().getPartETag());
        }
        String hex = sb.toString();
        byte[] raw = BaseEncoding.base16().decode(hex.toUpperCase());
        Hasher hasher = Hashing.md5().newHasher();
        hasher.putBytes(raw);
        String digest = hasher.hash().toString() + GWConstants.DASH + listPart.size();
        s3Object.get().setEtag(digest);
        logger.debug(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_MD5, s3Object.get().getEtag());
        writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWConstants.DOUBLE_QUOTE + s3Object.get().getEtag() + GWConstants.DOUBLE_QUOTE);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
        s3Metadata.setSize(s3Object.get().getFileSize());
        s3Metadata.setContentLength(s3Object.get().getFileSize());
        s3Metadata.setETag(s3Object.get().getEtag());
        s3Metadata.setLastModified(s3Object.get().getLastModified());
        s3Metadata.setTier(GWConstants.AWS_TIER_STANTARD);
        s3Metadata.setDeleteMarker(s3Object.get().getDeleteMarker());
        s3Metadata.setVersionId(s3Object.get().getVersionId());
        String jsonmeta = "";
        try {
            jsonmeta = jsonMapper.writeValueAsString(s3Metadata);
        } catch (JsonProcessingException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        int result = 0;
        try {
            remove(bucket, object);
            objMeta.set(s3Object.get().getEtag(), "", jsonmeta, acl, s3Object.get().getFileSize());
            objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_FILE, true);
            result = insertObject(bucket, object, objMeta);
        } catch (GWException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        if (result != 0) {
            logger.error(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_FAILED, bucket, object);
        }
        logger.debug(GWConstants.LOG_COMPLETE_MULTIPART_UPLOAD_INFO, bucket, object, s3Object.get().getFileSize(), s3Object.get().getEtag(), acl, versionId);
        objMultipart.abortMultipartUpload(uploadId);
    } 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) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) 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) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataCompleteMultipartUpload(com.pspace.ifs.ksan.gw.data.DataCompleteMultipartUpload) UnknownHostException(java.net.UnknownHostException) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) TreeMap(java.util.TreeMap) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) ResourceNotFoundException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Hasher(com.google.common.hash.Hasher) XMLStreamException(javax.xml.stream.XMLStreamException) Part(com.pspace.ifs.ksan.gw.object.multipart.Part) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) CompleteMultipartUploadRequest(com.pspace.ifs.ksan.gw.format.CompleteMultipartUploadRequest)

Example 3 with ObjMultipart

use of com.pspace.ifs.ksan.objmanager.ObjMultipart 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);
    }
}
Also used : ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) UnknownHostException(java.net.UnknownHostException) Upload(com.pspace.ifs.ksan.gw.object.multipart.Upload) IOException(java.io.IOException) ResultUploads(com.pspace.ifs.ksan.gw.object.multipart.ResultUploads) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) DataListMultipartUploads(com.pspace.ifs.ksan.gw.data.DataListMultipartUploads) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Writer(java.io.Writer) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 4 with ObjMultipart

use of com.pspace.ifs.ksan.objmanager.ObjMultipart 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 5 with ObjMultipart

use of com.pspace.ifs.ksan.objmanager.ObjMultipart in project ksan by infinistor.

the class UploadPart method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_UPLOAD_PART_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    String object = s3Parameter.getObjectName();
    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);
    DataUploadPart dataUploadPart = new DataUploadPart(s3Parameter);
    dataUploadPart.extract();
    String partNumberStr = dataUploadPart.getPartNumber();
    int partNumber = Integer.parseInt(partNumberStr);
    String uploadId = dataUploadPart.getUploadId();
    s3Parameter.setUploadId(uploadId);
    s3Parameter.setPartNumber(partNumberStr);
    if (partNumber < 1 || partNumber > GWConstants.MAX_PARTS_SIZE) {
        logger.error(GWErrorCode.INVALID_ARGUMENT.getMessage() + GWConstants.LOG_UPLOAD_PART_WRONG_PART_NUMBER);
        throw new GWException(GWErrorCode.INVALID_ARGUMENT, GWConstants.LOG_UPLOAD_PART_WRONG_PART_NUMBER, (Throwable) null, ImmutableMap.of(GWConstants.ARGMENT_NAME, GWConstants.PART_NUMBER, GWConstants.ARGMENT_VALUE, partNumberStr), s3Parameter);
    }
    String contentLength = dataUploadPart.getContentLength();
    String contentMD5String = dataUploadPart.getContentMD5();
    String customerAlgorithm = dataUploadPart.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataUploadPart.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataUploadPart.getServerSideEncryptionCustomerKeyMD5();
    if (Strings.isNullOrEmpty(contentLength)) {
        logger.error(GWConstants.LENGTH_REQUIRED);
        throw new GWException(GWErrorCode.MISSING_CONTENT_LENGTH, s3Parameter);
    }
    // get metadata
    S3Metadata s3Metadata = new S3Metadata();
    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);
        }
    } catch (UnknownHostException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    ObjectMapper jsonMapper = new ObjectMapper();
    try {
        s3Metadata = jsonMapper.readValue(multipart.getMeta(), S3Metadata.class);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    Metadata objMeta = createLocal(bucket, object);
    if (!Strings.isNullOrEmpty(contentMD5String)) {
        s3Metadata.setContentMD5(contentMD5String);
    }
    long length = Long.parseLong(contentLength);
    s3Metadata.setContentLength(length);
    String path = GWDiskConfig.getInstance().getLocalPath();
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, null, null);
    Metadata part = null;
    S3Object s3Object = null;
    try {
        part = objMultipart.getObjectWithUploadIdPartNo(uploadId, partNumber);
        if (part != null) {
            objectOperation.deletePart(part.getPrimaryDisk().getId());
        }
        s3Object = objectOperation.uploadPart(path, length);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    objMultipart.startSingleUpload(object, uploadId, partNumber, "", "", s3Object.getEtag(), s3Object.getFileSize(), objMeta.getPrimaryDisk().getId());
    objMultipart.finishSingleUpload(uploadId, partNumber);
    s3Parameter.addRequestSize(s3Object.getFileSize());
    s3Parameter.setFileSize(s3Object.getFileSize());
    s3Parameter.getResponse().addHeader(HttpHeaders.ETAG, GWUtils.maybeQuoteETag(s3Object.getEtag()));
}
Also used : ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) Multipart(com.pspace.ifs.ksan.gw.object.multipart.Multipart) UnknownHostException(java.net.UnknownHostException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) SQLException(java.sql.SQLException) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) DataUploadPart(com.pspace.ifs.ksan.gw.data.DataUploadPart) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

GWException (com.pspace.ifs.ksan.gw.exception.GWException)7 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)7 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)7 UnknownHostException (java.net.UnknownHostException)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)5 Metadata (com.pspace.ifs.ksan.objmanager.Metadata)5 IOException (java.io.IOException)5 Writer (java.io.Writer)5 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)4 S3Object (com.pspace.ifs.ksan.gw.object.S3Object)3 Multipart (com.pspace.ifs.ksan.gw.object.multipart.Multipart)3 Part (com.pspace.ifs.ksan.gw.object.multipart.Part)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 Map (java.util.Map)2 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1