Search in sources :

Example 1 with S3Object

use of com.pspace.ifs.ksan.gw.object.S3Object 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 2 with S3Object

use of com.pspace.ifs.ksan.gw.object.S3Object in project ksan by infinistor.

the class CopyObject method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_COPY_OBJECT_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);
    try {
        object = URLDecoder.decode(object, GWConstants.CHARSET_UTF_8);
    } catch (UnsupportedEncodingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    DataCopyObject dataCopyObject = new DataCopyObject(s3Parameter);
    dataCopyObject.extract();
    String cacheControl = dataCopyObject.getCacheControl();
    String contentDisposition = dataCopyObject.getContentDisposition();
    String contentEncoding = dataCopyObject.getContentEncoding();
    String contentLanguage = dataCopyObject.getContentLanguage();
    String contentType = dataCopyObject.getContentType();
    String contentLengthString = dataCopyObject.getContentLength();
    String metadataDirective = dataCopyObject.getMetadataDirective();
    String serversideEncryption = dataCopyObject.getServerSideEncryption();
    String copySource = dataCopyObject.getCopySource();
    String copySourceIfMatch = dataCopyObject.getCopySourceIfMatch();
    String copySourceIfNoneMatch = dataCopyObject.getCopySourceIfNoneMatch();
    String copySourceIfModifiedSince = dataCopyObject.getCopySourceIfModifiedSince();
    String copySourceIfUnmodifiedSince = dataCopyObject.getCopySourceIfUnmodifiedSince();
    String expires = dataCopyObject.getExpires();
    String customerAlgorithm = dataCopyObject.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataCopyObject.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataCopyObject.getServerSideEncryptionCustomerKeyMD5();
    String context = dataCopyObject.getServerSideEncryptionContext();
    String bucketKeyEnabled = dataCopyObject.getServerSideEncryptionBucketKeyEnabled();
    String copySourceCustomerAlgorithm = dataCopyObject.getCopySourceServerSideEncryptionCustomerAlgorithm();
    String copySourceCustomerKey = dataCopyObject.getCopySourceServerSideEncryptionCustomerKey();
    String copySourceCustomerKeyMD5 = dataCopyObject.getCopySourceServerSideEncryptionCustomerKeyMD5();
    Map<String, String> userMetadata = dataCopyObject.getUserMetadata();
    // Check copy source
    if (Strings.isNullOrEmpty(copySource)) {
        logger.error(GWConstants.LOG_COPY_SOURCE_IS_NULL);
        throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
    }
    try {
        copySource = URLDecoder.decode(copySource, GWConstants.CHARSET_UTF_8);
    } catch (UnsupportedEncodingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    if (copySource.startsWith(GWConstants.SLASH)) {
        copySource = copySource.substring(1);
    } else if (copySource.contains(GWConstants.S3_ARN)) {
        logger.error(GWConstants.LOG_COPY_SOURCE_IS_NOT_IMPLEMENTED, copySource);
        throw new GWException(GWErrorCode.NOT_IMPLEMENTED, s3Parameter);
    }
    String[] sourcePath = copySource.split(GWConstants.SLASH, 2);
    if (sourcePath.length != 2) {
        throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
    }
    String srcBucket = sourcePath[0];
    String srcObjectName = sourcePath[1];
    String srcVersionId = null;
    setSrcBucket(srcBucket);
    if (srcObjectName.contains(GWConstants.SUB_PARAMETER_VERSIONID) == true) {
        String[] source = sourcePath[1].split(GWConstants.PARAMETER_BACKSLASH_VERSIONID, 2);
        srcObjectName = source[0];
        srcVersionId = source[1].replaceAll(GWConstants.DOUBLE_QUOTE, "");
    }
    s3Parameter.setSrcBucketName(srcBucket);
    s3Parameter.setSrcVersionId(srcVersionId);
    s3Parameter.setSrcPath(srcObjectName);
    logger.debug(GWConstants.LOG_SOURCE_INFO, srcBucket, srcObjectName, srcVersionId);
    String versioningStatus = getBucketVersioning(srcBucket);
    Metadata srcMeta = null;
    if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
        if (!Strings.isNullOrEmpty(srcVersionId) && !GWConstants.VERSIONING_DISABLE_TAIL.equals(srcVersionId)) {
            srcMeta = open(srcBucket, srcObjectName, srcVersionId);
        } else {
            srcMeta = open(srcBucket, srcObjectName);
        }
    } else {
        srcMeta = open(srcBucket, srcObjectName);
    }
    srcVersionId = srcMeta.getVersionId();
    srcMeta.setAcl(GWUtils.makeOriginalXml(srcMeta.getAcl(), s3Parameter));
    checkGrantObject(s3Parameter.isPublicAccess(), srcMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    // get metadata
    S3Metadata s3Metadata = null;
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        s3Metadata = objectMapper.readValue(srcMeta.getMeta(), S3Metadata.class);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    // Check match
    if (!Strings.isNullOrEmpty(copySourceIfMatch)) {
        logger.debug(GWConstants.LOG_SOURCE_ETAG_MATCH, s3Metadata.getETag(), copySourceIfMatch.replace(GWConstants.DOUBLE_QUOTE, ""));
        if (!GWUtils.maybeQuoteETag(s3Metadata.getETag()).equals(copySourceIfMatch.replace(GWConstants.DOUBLE_QUOTE, ""))) {
            throw new GWException(GWErrorCode.PRECONDITION_FAILED, s3Parameter);
        }
    }
    if (!Strings.isNullOrEmpty(copySourceIfNoneMatch)) {
        logger.debug(GWConstants.LOG_SOURCE_ETAG_MATCH, s3Metadata.getETag(), copySourceIfNoneMatch.replace(GWConstants.DOUBLE_QUOTE, ""));
        if (GWUtils.maybeQuoteETag(s3Metadata.getETag()).equals(copySourceIfNoneMatch.replace(GWConstants.DOUBLE_QUOTE, ""))) {
            throw new GWException(GWErrorCode.DOES_NOT_MATCH, String.format(GWConstants.LOG_ETAG_IS_MISMATCH), s3Parameter);
        }
    }
    if (!Strings.isNullOrEmpty(copySourceIfModifiedSince)) {
        long copySourceIfModifiedSinceLong = Long.parseLong(copySourceIfModifiedSince);
        if (copySourceIfModifiedSinceLong != -1) {
            Date modifiedSince = new Date(copySourceIfModifiedSinceLong);
            if (s3Metadata.getLastModified().before(modifiedSince)) {
                throw new GWException(GWErrorCode.DOES_NOT_MATCH, String.format(GWConstants.LOG_MATCH_BEFORE, s3Metadata.getLastModified(), modifiedSince), s3Parameter);
            }
        }
    }
    if (!Strings.isNullOrEmpty(copySourceIfUnmodifiedSince)) {
        long copySourceIfUnmodifiedSinceLong = Long.parseLong(copySourceIfUnmodifiedSince);
        if (copySourceIfUnmodifiedSinceLong != -1) {
            Date unmodifiedSince = new Date(copySourceIfUnmodifiedSinceLong);
            if (s3Metadata.getLastModified().after(unmodifiedSince)) {
                throw new GWException(GWErrorCode.PRECONDITION_FAILED, String.format(GWConstants.LOG_MATCH_AFTER, s3Metadata.getLastModified(), unmodifiedSince), s3Parameter);
            }
        }
    }
    accessControlPolicy = new AccessControlPolicy();
    accessControlPolicy.aclList = new AccessControlList();
    accessControlPolicy.aclList.grants = new ArrayList<Grant>();
    accessControlPolicy.owner = new Owner();
    accessControlPolicy.owner.id = String.valueOf(s3Parameter.getUser().getUserId());
    accessControlPolicy.owner.displayName = s3Parameter.getUser().getUserName();
    String aclXml = GWUtils.makeAclXml(accessControlPolicy, null, dataCopyObject.hasAclKeyword(), null, dataCopyObject.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataCopyObject.getGrantRead(), dataCopyObject.getGrantWrite(), dataCopyObject.getGrantFullControl(), dataCopyObject.getGrantReadAcp(), dataCopyObject.getGrantWriteAcp(), s3Parameter);
    boolean bReplaceMetadata = false;
    logger.debug(GWConstants.LOG_COPY_OBJECT_METADATA_DIRECTIVE, metadataDirective);
    if (!Strings.isNullOrEmpty(metadataDirective) && metadataDirective.equalsIgnoreCase(GWConstants.REPLACE)) {
        bReplaceMetadata = true;
    }
    s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    if (userMetadata.size() > 0) {
        for (String key : userMetadata.keySet()) {
            logger.info(GWConstants.LOG_COPY_OBJECT_USER_METADATA, key, userMetadata.get(key));
        }
        if (bReplaceMetadata) {
            logger.info(GWConstants.LOG_COPY_OBJECT_REPLACE_USER_METADATA, userMetadata.toString());
            s3Metadata.setUserMetadataMap(userMetadata);
        }
    }
    if (!Strings.isNullOrEmpty(cacheControl) && bReplaceMetadata) {
        s3Metadata.setCacheControl(cacheControl);
    }
    if (!Strings.isNullOrEmpty(contentDisposition) && bReplaceMetadata) {
        s3Metadata.setContentDisposition(contentDisposition);
    }
    if (!Strings.isNullOrEmpty(contentEncoding) && bReplaceMetadata) {
        s3Metadata.setContentEncoding(contentEncoding);
    }
    if (!Strings.isNullOrEmpty(contentLanguage) && bReplaceMetadata) {
        s3Metadata.setContentLanguage(contentLanguage);
    }
    if (!Strings.isNullOrEmpty(contentType) && bReplaceMetadata) {
        s3Metadata.setContentType(contentType);
    }
    if (Strings.isNullOrEmpty(contentLengthString)) {
        logger.error(GWErrorCode.MISSING_CONTENT_LENGTH.getMessage());
        throw new GWException(GWErrorCode.MISSING_CONTENT_LENGTH, s3Parameter);
    }
    /*else {
            try {
                long contentLength = Long.parseLong(contentLengthString);
                s3Metadata.setContentLength(contentLength);
            } catch (NumberFormatException e) {
                logger.error(e.getMessage());
				throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, e);
            }
        }*/
    String jsonmeta = "";
    // update
    if (s3Parameter.getSrcBucketName().equals(bucket) && s3Parameter.getSrcPath().equals(object)) {
        if (!Strings.isNullOrEmpty(metadataDirective) && bReplaceMetadata) {
            // update metadata
            try {
                S3Metadata metaClass = objectMapper.readValue(srcMeta.getMeta(), S3Metadata.class);
                s3Metadata.setTier(GWConstants.AWS_TIER_STANTARD);
                s3Metadata.setContentLength(metaClass.getContentLength());
                s3Metadata.setETag(metaClass.getETag());
            } catch (JsonProcessingException e) {
                PrintStack.logging(logger, e);
                throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
            }
            s3Metadata.setLastModified(new Date());
            try {
                jsonmeta = objectMapper.writeValueAsString(s3Metadata);
            } catch (JsonProcessingException e) {
                PrintStack.logging(logger, e);
                throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
            }
            logger.debug(GWConstants.LOG_COPY_OBJECT_META, jsonmeta);
            srcMeta.setMeta(jsonmeta);
            updateObjectMeta(srcMeta);
            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 xmlout = xmlOutputFactory.createXMLStreamWriter(writer);
                xmlout.writeStartDocument();
                xmlout.writeStartElement(GWConstants.COPY_OBJECT_RESULT);
                xmlout.writeDefaultNamespace(GWConstants.AWS_XMLNS);
                writeSimpleElement(xmlout, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
                writeSimpleElement(xmlout, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
                xmlout.writeEndElement();
                xmlout.flush();
            } catch (XMLStreamException | IOException e) {
                PrintStack.logging(logger, e);
                throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
            }
            s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
            return;
        } else {
            logger.error(GWErrorCode.INVALID_REQUEST.getMessage());
            throw new GWException(GWErrorCode.INVALID_REQUEST, s3Parameter);
        }
    }
    Metadata objMeta = createCopy(srcBucket, srcObjectName, srcVersionId, bucket, object);
    versioningStatus = getBucketVersioning(bucket);
    String versionId = null;
    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, null);
    S3Object s3Object = objectOperation.copyObject(srcMeta);
    s3Metadata.setETag(s3Object.getEtag());
    s3Metadata.setSize(s3Object.getFileSize());
    s3Metadata.setTier(GWConstants.AWS_TIER_STANTARD);
    s3Metadata.setLastModified(s3Object.getLastModified());
    s3Metadata.setDeleteMarker(s3Object.getDeleteMarker());
    s3Metadata.setVersionId(s3Object.getVersionId());
    try {
        jsonmeta = objectMapper.writeValueAsString(s3Metadata);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    logger.debug(GWConstants.LOG_COPY_OBJECT_META, jsonmeta);
    try {
        int result;
        objMeta.set(s3Object.getEtag(), srcMeta.getTag(), jsonmeta, aclXml, s3Object.getFileSize());
        objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_FILE, true);
        result = insertObject(bucket, object, objMeta);
        if (result != 0) {
            logger.error(GWConstants.LOG_COPY_OBJECT_FAILED, bucket, object);
        }
        logger.debug(GWConstants.LOG_COPY_OBJECT_INFO, bucket, object, s3Object.getFileSize(), s3Object.getEtag(), srcMeta.getAcl(), srcMeta.getAcl(), versionId);
    } catch (GWException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.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 xmlout = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlout.writeStartDocument();
        xmlout.writeStartElement(GWConstants.COPY_OBJECT_RESULT);
        xmlout.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlout, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
        writeSimpleElement(xmlout, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Object.getEtag()));
        xmlout.writeEndElement();
        xmlout.flush();
    } catch (XMLStreamException | IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : AccessControlList(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList) AccessControlPolicy(com.pspace.ifs.ksan.gw.format.AccessControlPolicy) Owner(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner) 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) 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) Grant(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DataCopyObject(com.pspace.ifs.ksan.gw.data.DataCopyObject) IOException(java.io.IOException) Date(java.util.Date) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 3 with S3Object

use of com.pspace.ifs.ksan.gw.object.S3Object 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);
    }
}
Also used : HashMap(java.util.HashMap) S3ServerSideEncryption(com.pspace.ifs.ksan.gw.object.S3ServerSideEncryption) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) Decoder(java.util.Base64.Decoder) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) TagSet(com.pspace.ifs.ksan.gw.format.Tagging.TagSet) S3Signing(com.pspace.ifs.ksan.gw.sign.S3Signing) List(java.util.List) GWException(com.pspace.ifs.ksan.gw.exception.GWException) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) DataPostObject(com.pspace.ifs.ksan.gw.data.DataPostObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) ByteArrayInputStream(java.io.ByteArrayInputStream) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) DataPostObject(com.pspace.ifs.ksan.gw.data.DataPostObject) S3Object(com.pspace.ifs.ksan.gw.object.S3Object) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag) HashMap(java.util.HashMap) Map(java.util.Map) PostPolicy(com.pspace.ifs.ksan.gw.format.PostPolicy)

Example 4 with S3Object

use of com.pspace.ifs.ksan.gw.object.S3Object in project ksan by infinistor.

the class PutObject method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_OBJECT_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    String object = s3Parameter.getObjectName();
    logger.debug(GWConstants.LOG_BUCKET_OBJECT, bucket, object);
    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);
    DataPutObject dataPutObject = new DataPutObject(s3Parameter);
    dataPutObject.extract();
    S3Metadata s3Metadata = new S3Metadata();
    String cacheControl = dataPutObject.getCacheControl();
    String contentDisposition = dataPutObject.getContentDisposition();
    String contentEncoding = dataPutObject.getContentEncoding();
    String contentLanguage = dataPutObject.getContentLanguage();
    String contentType = dataPutObject.getContentType();
    String contentLengthString = dataPutObject.getContentLength();
    String decodedContentLengthString = dataPutObject.getDecodedContentLength();
    String contentMD5String = dataPutObject.getContentMD5();
    String customerAlgorithm = dataPutObject.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataPutObject.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataPutObject.getServerSideEncryptionCustomerKeyMD5();
    String serversideEncryption = dataPutObject.getServerSideEncryption();
    s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    s3Metadata.setUserMetadataMap(dataPutObject.getUserMetadata());
    if (!Strings.isNullOrEmpty(serversideEncryption)) {
        if (!GWConstants.AES256.equalsIgnoreCase(serversideEncryption)) {
            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);
    }
    if (!Strings.isNullOrEmpty(decodedContentLengthString)) {
        contentLengthString = decodedContentLengthString;
    }
    HashCode contentMD5 = null;
    if (!Strings.isNullOrEmpty(contentMD5String)) {
        s3Metadata.setContentMD5(contentMD5String);
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String));
        } catch (IllegalArgumentException iae) {
            PrintStack.logging(logger, iae);
            throw new GWException(GWErrorCode.INVALID_DIGEST, iae, s3Parameter);
        }
        if (contentMD5.bits() != MD5.bits()) {
            logger.error(GWErrorCode.INVALID_DIGEST.getMessage() + GWConstants.LOG_PUT_OBJECT_HASHCODE_ILLEGAL);
            throw new GWException(GWErrorCode.INVALID_DIGEST, s3Parameter);
        }
    }
    long contentLength;
    if (Strings.isNullOrEmpty(contentLengthString)) {
        logger.error(GWErrorCode.MISSING_CONTENT_LENGTH.getMessage());
        throw new GWException(GWErrorCode.MISSING_CONTENT_LENGTH, s3Parameter);
    } else {
        try {
            contentLength = Long.parseLong(contentLengthString);
            s3Metadata.setContentLength(contentLength);
        } catch (NumberFormatException nfe) {
            PrintStack.logging(logger, nfe);
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, nfe, s3Parameter);
        }
    }
    accessControlPolicy = new AccessControlPolicy();
    accessControlPolicy.aclList = new AccessControlList();
    accessControlPolicy.aclList.grants = new ArrayList<Grant>();
    accessControlPolicy.owner = new Owner();
    accessControlPolicy.owner.id = String.valueOf(s3Parameter.getUser().getUserId());
    accessControlPolicy.owner.displayName = s3Parameter.getUser().getUserName();
    String aclXml = GWUtils.makeAclXml(accessControlPolicy, null, dataPutObject.hasAclKeyword(), null, dataPutObject.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataPutObject.getGrantRead(), dataPutObject.getGrantWrite(), dataPutObject.getGrantFullControl(), dataPutObject.getGrantReadAcp(), dataPutObject.getGrantWriteAcp(), s3Parameter);
    logger.debug(GWConstants.LOG_ACL, aclXml);
    String bucketEncryption = getBucketInfo().getEncryption();
    // check encryption
    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();
    if (!Strings.isNullOrEmpty(dataPutObject.getTagging())) {
        String strtaggingInfo = dataPutObject.getTagging();
        String[] strtagset = strtaggingInfo.split(GWConstants.AMPERSAND);
        int starttag = 0;
        for (String strtag : strtagset) {
            if (starttag == 0)
                tagging.tagset.tags = new ArrayList<Tag>();
            starttag += 1;
            Tag tag = new Tag();
            String[] keyvalue = strtag.split(GWConstants.EQUAL);
            if (keyvalue.length == GWConstants.TAG_KEY_SIZE) {
                tag.key = keyvalue[GWConstants.TAG_KEY_INDEX];
                tag.value = keyvalue[GWConstants.TAG_VALUE_INDEX];
            } else {
                tag.key = keyvalue[GWConstants.TAG_KEY_INDEX];
                tag.value = "";
            }
            tagging.tagset.tags.add(tag);
        }
        try {
            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() > GWConstants.TAG_KEY_MAX) {
                        logger.error(GWConstants.LOG_PUT_OBJECT_TAGGING_KEY_LENGTH, t.key.length());
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                    if (t.value.length() > GWConstants.TAG_VALUE_MAX) {
                        logger.error(GWConstants.LOG_PUT_OBJECT_TAGGING_VALUE_LENGTH, t.value.length());
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                }
            }
            if (tagging.tagset != null && tagging.tagset.tags != null) {
                if (tagging.tagset.tags.size() > GWConstants.TAG_MAX_SIZE) {
                    logger.error(GWConstants.LOG_PUT_OBJECT_TAGGING_SIZE, tagging.tagset.tags.size());
                    throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                }
                taggingCount = String.valueOf(tagging.tagset.tags.size());
            }
        }
    }
    if (!Strings.isNullOrEmpty(dataPutObject.getObjectLockMode())) {
        try {
            logger.debug(GWConstants.LOG_OBJECT_LOCK, getBucketInfo().getObjectLock());
            ObjectLockConfiguration oc = new XmlMapper().readValue(getBucketInfo().getObjectLock(), ObjectLockConfiguration.class);
            if (!oc.objectLockEnabled.equals(GWConstants.STATUS_ENABLED)) {
                logger.error(GWConstants.LOG_PUT_OBJECT_LOCK_STATUS, oc.objectLockEnabled);
                throw new GWException(GWErrorCode.INVALID_REQUEST, s3Parameter);
            }
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        if (!dataPutObject.getObjectLockMode().equals(GWConstants.GOVERNANCE) && !dataPutObject.getObjectLockMode().equals(GWConstants.COMPLIANCE)) {
            logger.error(GWConstants.LOG_PUT_OBJECT_LOCK_MODE, dataPutObject.getObjectLockMode());
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
        s3Metadata.setLockMode(dataPutObject.getObjectLockMode());
    }
    if (!Strings.isNullOrEmpty(dataPutObject.getObjectLockRetainUntilDate())) {
        if (!dataPutObject.getObjectLockMode().equals(GWConstants.GOVERNANCE) && !dataPutObject.getObjectLockMode().equals(GWConstants.COMPLIANCE)) {
            logger.error(GWConstants.LOG_PUT_OBJECT_LOCK_MODE, dataPutObject.getObjectLockMode());
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
        try {
            ObjectLockConfiguration oc = new XmlMapper().readValue(getBucketInfo().getObjectLock(), ObjectLockConfiguration.class);
            if (!oc.objectLockEnabled.equals(GWConstants.STATUS_ENABLED)) {
                logger.error(GWConstants.LOG_PUT_OBJECT_LOCK_STATUS, oc.objectLockEnabled);
                throw new GWException(GWErrorCode.INVALID_REQUEST, s3Parameter);
            }
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        s3Metadata.setLockExpires(dataPutObject.getObjectLockRetainUntilDate());
    }
    if (!Strings.isNullOrEmpty(dataPutObject.getObjectLockLegalHold())) {
        try {
            ObjectLockConfiguration oc = new XmlMapper().readValue(getBucketInfo().getObjectLock(), ObjectLockConfiguration.class);
            if (!oc.objectLockEnabled.equals(GWConstants.STATUS_ENABLED)) {
                logger.error(GWConstants.LOG_PUT_OBJECT_LOCK_STATUS, oc.objectLockEnabled);
                throw new GWException(GWErrorCode.INVALID_REQUEST, s3Parameter);
            }
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        s3Metadata.setLegalHold(dataPutObject.getObjectLockLegalHold());
    }
    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 {
        int result;
        objMeta.set(s3Object.getEtag(), taggingxml, jsonmeta, aclXml, s3Object.getFileSize());
        objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_FILE, true);
        result = 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());
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : AccessControlList(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList) AccessControlPolicy(com.pspace.ifs.ksan.gw.format.AccessControlPolicy) Owner(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner) S3ServerSideEncryption(com.pspace.ifs.ksan.gw.object.S3ServerSideEncryption) ArrayList(java.util.ArrayList) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) DataPutObject(com.pspace.ifs.ksan.gw.data.DataPutObject) HashCode(com.google.common.hash.HashCode) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) TagSet(com.pspace.ifs.ksan.gw.format.Tagging.TagSet) ObjectLockConfiguration(com.pspace.ifs.ksan.gw.format.ObjectLockConfiguration) 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) Grant(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag)

Example 5 with S3Object

use of com.pspace.ifs.ksan.gw.object.S3Object 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

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 GWException (com.pspace.ifs.ksan.gw.exception.GWException)6 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)6 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)6 S3Object (com.pspace.ifs.ksan.gw.object.S3Object)6 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)6 Metadata (com.pspace.ifs.ksan.objmanager.Metadata)6 IOException (java.io.IOException)5 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)3 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)3 Writer (java.io.Writer)3 UnknownHostException (java.net.UnknownHostException)3 AccessControlPolicy (com.pspace.ifs.ksan.gw.format.AccessControlPolicy)2 AccessControlList (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList)2 Grant (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant)2 Owner (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner)2 Tagging (com.pspace.ifs.ksan.gw.format.Tagging)2 TagSet (com.pspace.ifs.ksan.gw.format.Tagging.TagSet)2 Tag (com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag)2