Search in sources :

Example 36 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class PutObjectLockConfiguration method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_OBJECT_LOCK_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);
    }
    checkGrantBucketOwner(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE_ACP);
    DataPutBucketObjectLock dataPutBucketObjectLock = new DataPutBucketObjectLock(s3Parameter);
    dataPutBucketObjectLock.extract();
    String ObjectLockXml = dataPutBucketObjectLock.getObjectLockXml();
    if (Strings.isNullOrEmpty(getBucketInfo().getObjectLock())) {
        throw new GWException(GWErrorCode.INVALID_BUCKET_STATE, s3Parameter);
    }
    if (Strings.isNullOrEmpty(ObjectLockXml)) {
        throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
    }
    logger.info(GWConstants.LOG_PUT_BUCKET_OBJECT_LOCK, getBucketInfo().getObjectLock());
    try {
        ObjectLockConfiguration oc = new XmlMapper().readValue(getBucketInfo().getObjectLock(), ObjectLockConfiguration.class);
        if (!oc.objectLockEnabled.equals(GWConstants.STATUS_ENABLED)) {
            throw new GWException(GWErrorCode.INVALID_BUCKET_STATE, s3Parameter);
        }
        oc = new XmlMapper().readValue(ObjectLockXml, ObjectLockConfiguration.class);
        if (!oc.objectLockEnabled.equals(GWConstants.STATUS_ENABLED)) {
            throw new GWException(GWErrorCode.MALFORMED_X_M_L, s3Parameter);
        }
        if (!Strings.isNullOrEmpty(oc.rules.defaultRetention.days) && !Strings.isNullOrEmpty(oc.rules.defaultRetention.years)) {
            throw new GWException(GWErrorCode.MALFORMED_X_M_L, s3Parameter);
        }
        if (!Strings.isNullOrEmpty(oc.rules.defaultRetention.days)) {
            try {
                if (Integer.parseInt(oc.rules.defaultRetention.days) <= 0) {
                    throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
                }
            } catch (NumberFormatException e) {
                throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
            }
        }
        if (!Strings.isNullOrEmpty(oc.rules.defaultRetention.years)) {
            try {
                if (Integer.parseInt(oc.rules.defaultRetention.years) <= 0) {
                    throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
                }
            } catch (NumberFormatException e) {
                throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
            }
        }
        if (!Strings.isNullOrEmpty(oc.rules.defaultRetention.mode)) {
            if (!oc.rules.defaultRetention.mode.equals(GWConstants.GOVERNANCE) && !oc.rules.defaultRetention.mode.equals(GWConstants.COMPLIANCE)) {
                throw new GWException(GWErrorCode.MALFORMED_X_M_L, s3Parameter);
            }
        }
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    updateBucketObjectLock(bucket, ObjectLockXml);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) ObjectLockConfiguration(com.pspace.ifs.ksan.gw.format.ObjectLockConfiguration) GWException(com.pspace.ifs.ksan.gw.exception.GWException) IOException(java.io.IOException) DataPutBucketObjectLock(com.pspace.ifs.ksan.gw.data.DataPutBucketObjectLock) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 37 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket 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)

Example 38 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class UploadPartCopy method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_UPLOAD_PART_COPY_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);
    DataUploadPartCopy dataUploadPartCopy = new DataUploadPartCopy(s3Parameter);
    dataUploadPartCopy.extract();
    String partNumber = dataUploadPartCopy.getPartNumber();
    String uploadId = dataUploadPartCopy.getUploadId();
    String copySource = dataUploadPartCopy.getCopySource();
    String copySourceRange = dataUploadPartCopy.getCopySourceRange();
    String copySourceIfMatch = dataUploadPartCopy.getCopySourceIfMatch();
    String copySourceIfNoneMatch = dataUploadPartCopy.getCopySourceIfNoneMatch();
    ;
    String copySourceIfModifiedSince = dataUploadPartCopy.getCopySourceIfModifiedSince();
    String copySourceIfUnmodifiedSince = dataUploadPartCopy.getCopySourceIfUnmodifiedSince();
    String customerAlgorithm = dataUploadPartCopy.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataUploadPartCopy.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataUploadPartCopy.getServerSideEncryptionCustomerKeyMD5();
    String copySourceCustomerAlgorithm = dataUploadPartCopy.getCopySourceServerSideEncryptionCustomerAlgorithm();
    String copySourceCustomerKey = dataUploadPartCopy.getCopySourceServerSideEncryptionCustomerKey();
    String copySourceCustomerKeyMD5 = dataUploadPartCopy.getCopySourceServerSideEncryptionCustomerKeyMD5();
    // 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);
        logger.info(GWConstants.LOG_UPLOAD_PART_COPY_SOURCE, copySource);
    } 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;
    if (!isExistBucket(srcBucket)) {
        logger.error(GWConstants.LOG_BUCKET_IS_NOT_EXIST, srcBucket);
        throw new GWException(GWErrorCode.NO_SUCH_BUCKET, s3Parameter);
    }
    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, "");
    }
    String versioningStatus = getBucketVersioning(srcBucket);
    Metadata srcMeta = null;
    if (GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(versioningStatus)) {
        if (!Strings.isNullOrEmpty(srcVersionId)) {
            srcMeta = open(srcBucket, srcObjectName, srcVersionId);
        } else {
            srcMeta = open(srcBucket, srcObjectName);
            srcVersionId = srcMeta.getVersionId();
        }
    } else {
        srcMeta = open(srcBucket, srcObjectName);
        srcVersionId = srcMeta.getVersionId();
    }
    s3Parameter.setSrcVersionId(srcVersionId);
    s3Parameter.setSrcPath(srcObjectName);
    s3Parameter.setPartNumber(partNumber);
    logger.debug(GWConstants.LOG_SOURCE_INFO, srcBucket, srcObjectName, srcVersionId);
    srcMeta.setAcl(GWUtils.makeOriginalXml(srcMeta.getAcl(), s3Parameter));
    checkGrantObject(s3Parameter.isPublicAccess(), srcMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    // get metadata
    S3Metadata s3Metadata = new S3Metadata();
    ObjectMapper jsonMapper = new ObjectMapper();
    try {
        s3Metadata = jsonMapper.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);
            }
        }
    }
    // Check copy source Range
    S3Range s3Range = new S3Range(s3Parameter);
    if (!Strings.isNullOrEmpty(copySourceRange)) {
        logger.info(GWConstants.LOG_UPLOAD_PART_COPY_SOURCE_RANGE, copySourceRange, s3Metadata.getSize());
        s3Range.parseRange(copySourceRange, s3Metadata.getSize(), true);
    }
    Metadata objMeta = createCopy(srcBucket, srcObjectName, srcVersionId, bucket, object);
    String path = GWDiskConfig.getInstance().getLocalPath();
    S3Object s3Object = null;
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, null, s3Parameter, null, null);
    try {
        s3Object = objectOperation.uploadPartCopy(path, srcMeta, s3Range);
    } catch (GWException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    ObjMultipart objMultipart = null;
    try {
        objMultipart = new ObjMultipart(bucket);
    } 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);
    }
    objMultipart.startSingleUpload(object, uploadId, Integer.parseInt(partNumber), "", "", s3Object.getEtag(), s3Object.getFileSize(), objMeta.getPrimaryDisk().getId());
    objMultipart.finishSingleUpload(uploadId, Integer.parseInt(partNumber));
    s3Parameter.setFileSize(s3Object.getFileSize());
    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_PART_RESULT);
        xmlout.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlout, GWConstants.LAST_MODIFIED, formatDate(s3Object.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 : S3Range(com.pspace.ifs.ksan.gw.object.S3Range) ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) UnknownHostException(java.net.UnknownHostException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DataUploadPartCopy(com.pspace.ifs.ksan.gw.data.DataUploadPartCopy) Date(java.util.Date) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) 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) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 39 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class CreateMultipartUpload method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_CREATE_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);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
    DataCreateMultipartUpload dataCreateMultipartUpload = new DataCreateMultipartUpload(s3Parameter);
    dataCreateMultipartUpload.extract();
    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 xml = GWUtils.makeAclXml(accessControlPolicy, null, dataCreateMultipartUpload.hasAclKeyword(), null, dataCreateMultipartUpload.getAcl(), getBucketInfo(), String.valueOf(s3Parameter.getUser().getUserId()), s3Parameter.getUser().getUserName(), dataCreateMultipartUpload.getGrantRead(), dataCreateMultipartUpload.getGrantWrite(), dataCreateMultipartUpload.getGrantFullControl(), dataCreateMultipartUpload.getGrantReadAcp(), dataCreateMultipartUpload.getGrantWriteAcp(), s3Parameter);
    String customerAlgorithm = dataCreateMultipartUpload.getServerSideEncryptionCustomerAlgorithm();
    String customerKey = dataCreateMultipartUpload.getServerSideEncryptionCustomerKey();
    String customerKeyMD5 = dataCreateMultipartUpload.getServerSideEncryptionCustomerKeyMD5();
    String serverSideEncryption = dataCreateMultipartUpload.getServerSideEncryption();
    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);
        }
    }
    S3Metadata s3Metadata = new S3Metadata();
    s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    s3Metadata.setServersideEncryption(serverSideEncryption);
    s3Metadata.setCustomerAlgorithm(customerAlgorithm);
    s3Metadata.setCustomerKey(customerKey);
    s3Metadata.setCustomerKeyMD5(customerKeyMD5);
    s3Metadata.setName(object);
    String cacheControl = dataCreateMultipartUpload.getCacheControl();
    String contentDisposition = dataCreateMultipartUpload.getContentDisposition();
    String contentEncoding = dataCreateMultipartUpload.getContentEncoding();
    String contentLanguage = dataCreateMultipartUpload.getContentLanguage();
    String contentType = dataCreateMultipartUpload.getContentType();
    String serversideEncryption = dataCreateMultipartUpload.getServerSideEncryption();
    s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    s3Metadata.setUserMetadataMap(dataCreateMultipartUpload.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);
    }
    ObjectMapper jsonMapper = new ObjectMapper();
    String metaJson = "";
    try {
        metaJson = jsonMapper.writeValueAsString(s3Metadata);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    }
    Metadata objMeta = null;
    try {
        // check exist object
        objMeta = createLocal(bucket, object);
    } catch (GWException e) {
        logger.info(e.getMessage());
        logger.error(GWConstants.LOG_CREATE_MULTIPART_UPLOAD_FAILED, bucket, object);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    }
    String uploadId = null;
    try {
        ObjMultipart objMultipart = new ObjMultipart(bucket);
        uploadId = objMultipart.createMultipartUpload(bucket, object, xml, metaJson, objMeta.getPrimaryDisk().getId());
    } 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);
    }
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.INITATE_MULTIPART_UPLOAD_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
        writeSimpleElement(xmlStreamWriter, GWConstants.KEY, object);
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID, uploadId);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (XMLStreamException xse) {
        PrintStack.logging(logger, xse);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
}
Also used : AccessControlList(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList) ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) Grant(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant) AccessControlPolicy(com.pspace.ifs.ksan.gw.format.AccessControlPolicy) Owner(com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) UnknownHostException(java.net.UnknownHostException) DataCreateMultipartUpload(com.pspace.ifs.ksan.gw.data.DataCreateMultipartUpload) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) IOException(java.io.IOException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 40 with S3Bucket

use of com.pspace.ifs.ksan.gw.identity.S3Bucket in project ksan by infinistor.

the class DeleteBucket method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_DELETE_BUCKET_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);
    }
    deleteBucket(bucket);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Aggregations

GWException (com.pspace.ifs.ksan.gw.exception.GWException)57 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)57 IOException (java.io.IOException)31 Metadata (com.pspace.ifs.ksan.objmanager.Metadata)17 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)16 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLStreamException (javax.xml.stream.XMLStreamException)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Writer (java.io.Writer)12 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)12 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)10 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)9 ObjMultipart (com.pspace.ifs.ksan.objmanager.ObjMultipart)7 S3Object (com.pspace.ifs.ksan.gw.object.S3Object)6 UnknownHostException (java.net.UnknownHostException)6 AccessControlPolicy (com.pspace.ifs.ksan.gw.format.AccessControlPolicy)5 AccessControlList (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList)5 Grant (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant)5 Owner (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.Owner)5