Search in sources :

Example 11 with Metadata

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

the class PutObjectTagging method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_OBJECT_TAGGING_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_WRITE);
    String object = s3Parameter.getObjectName();
    DataPutObjectTagging dataPutObjectTagging = new DataPutObjectTagging(s3Parameter);
    dataPutObjectTagging.extract();
    String taggingCount = GWConstants.TAGGING_INIT;
    String taggingXml = dataPutObjectTagging.getTaggingXml();
    try {
        Tagging tagging = new XmlMapper().readValue(taggingXml, Tagging.class);
        // 중복 지우기 item이 10개 미만이기 때문에 for loop가 빠름
        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) {
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                    if (t.value.length() > GWConstants.TAG_VALUE_MAX) {
                        throw new GWException(GWErrorCode.INVALID_TAG, s3Parameter);
                    }
                }
            }
            if (tagging.tagset != null && tagging.tagset.tags != null) {
                if (tagging.tagset.tags.size() > GWConstants.TAG_MAX_SIZE) {
                    throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                }
                taggingCount = String.valueOf(tagging.tagset.tags.size());
            }
        }
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    String versionId = dataPutObjectTagging.getVersionId();
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
    } else {
        objMeta = open(bucket, object, versionId);
    }
    S3Metadata s3Metadata = null;
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        logger.debug(GWConstants.LOG_META, objMeta.getMeta());
        s3Metadata = objectMapper.readValue(objMeta.getMeta(), S3Metadata.class);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Metadata.setTaggingCount(taggingCount);
    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);
    }
    objMeta.setMeta(jsonMeta);
    objMeta.setTag(taggingXml);
    updateObjectTagging(objMeta);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) DataPutObjectTagging(com.pspace.ifs.ksan.gw.data.DataPutObjectTagging) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) DataPutObjectTagging(com.pspace.ifs.ksan.gw.data.DataPutObjectTagging) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 12 with Metadata

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

the class S3Request method create.

protected Metadata create(String bucket, String object) throws GWException {
    Metadata meta = null;
    try {
        setObjManager();
        meta = objManager.create(bucket, object);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } finally {
        try {
            releaseObjManager();
        } catch (Exception e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    }
    return meta;
}
Also used : Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) ResourceAlreadyExistException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceAlreadyExistException) XMLStreamException(javax.xml.stream.XMLStreamException) ResourceNotFoundException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 13 with Metadata

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

the class S3Request method createLocal.

protected Metadata createLocal(String bucket, String object) throws GWException {
    Metadata meta = null;
    try {
        setObjManager();
        meta = objManager.createLocal(bucket, object);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } finally {
        try {
            releaseObjManager();
        } catch (Exception e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    }
    return meta;
}
Also used : Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) ResourceAlreadyExistException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceAlreadyExistException) XMLStreamException(javax.xml.stream.XMLStreamException) ResourceNotFoundException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 14 with Metadata

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

use of com.pspace.ifs.ksan.objmanager.Metadata 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)

Aggregations

Metadata (com.pspace.ifs.ksan.objmanager.Metadata)31 GWException (com.pspace.ifs.ksan.gw.exception.GWException)23 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)17 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)17 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 ResourceNotFoundException (com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException)12 XMLStreamException (javax.xml.stream.XMLStreamException)12 IOException (java.io.IOException)11 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)10 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)6 S3Object (com.pspace.ifs.ksan.gw.object.S3Object)6 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)6 ResourceAlreadyExistException (com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceAlreadyExistException)5 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)5 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)4 AccessControlPolicy (com.pspace.ifs.ksan.gw.format.AccessControlPolicy)4 AccessControlList (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList)4 Grant (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant)4 Owner (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner)4