Search in sources :

Example 16 with S3Metadata

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

the class DeleteObject method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_DELETE_OBJECT_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    String object = s3Parameter.getObjectName();
    logger.debug(GWConstants.LOG_DELETE_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);
    }
    checkGrantBucketOwner(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE);
    DataDeleteObject dataDeleteObject = new DataDeleteObject(s3Parameter);
    dataDeleteObject.extract();
    S3Metadata s3Metadata = new S3Metadata();
    s3Metadata.setName(object);
    s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
    s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
    String versionId = dataDeleteObject.getVersionId();
    boolean isLastVersion = true;
    String deleteMarker = null;
    String versioningStatus = null;
    versioningStatus = getBucketVersioning(bucket);
    Metadata objMeta = null;
    try {
        if (Strings.isNullOrEmpty(versionId)) {
            objMeta = open(bucket, object);
        } else {
            objMeta = open(bucket, object, versionId);
        }
    } catch (GWException e) {
        if (e.getError().equals(GWErrorCode.NO_SUCH_KEY) && Strings.isNullOrEmpty(versionId) && versioningStatus.equalsIgnoreCase(GWConstants.VERSIONING_ENABLED)) {
            objMeta = createLocal(bucket, object);
            putDeleteMarker(bucket, object, s3Metadata, objMeta);
        }
        s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, versionId);
        s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
        return;
    }
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, s3Metadata, s3Parameter, versionId, null);
    isLastVersion = objMeta.getLastVersion();
    deleteMarker = objMeta.getDeleteMarker();
    logger.debug(GWConstants.LOG_DELETE_OBJECT_INFO, versionId, isLastVersion, deleteMarker);
    logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING, versioningStatus);
    if (Strings.isNullOrEmpty(versioningStatus)) {
        logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_DISABLED);
        remove(bucket, object);
        objectOperation.deleteObject();
    } else {
        if (versioningStatus.equalsIgnoreCase(GWConstants.VERSIONING_ENABLED)) {
            // Bucket Versioning Enabled
            logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_ENABLED);
            if (Strings.isNullOrEmpty(versionId)) {
                // request versionId is null
                if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_MARK)) {
                    remove(bucket, object, GWConstants.VERSIONING_DISABLE_TAIL);
                } else {
                    // put delete marker
                    putDeleteMarker(bucket, object, s3Metadata, objMeta);
                }
            } else {
                // request with versionId
                if (isLastVersion) {
                    remove(bucket, object, versionId);
                    if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_FILE)) {
                        objectOperation.deleteObject();
                    }
                } else {
                    // request with versionId not currentVid
                    remove(bucket, object, versionId);
                    objectOperation.deleteObject();
                }
            }
        } else if (versioningStatus.equalsIgnoreCase(GWConstants.VERSIONING_SUSPENDED)) {
            // Bucket Versioning Suspended
            logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_SUSPENDED);
            if (Strings.isNullOrEmpty(versionId)) {
                if (isLastVersion) {
                    if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_MARK)) {
                        remove(bucket, object, GWConstants.OBJECT_TYPE_MARK);
                    } else {
                        // put delete marker
                        putDeleteMarker(bucket, object, s3Metadata, objMeta);
                    }
                }
            } else {
                // request with versionId
                remove(bucket, object, versionId);
                objectOperation.deleteObject();
            }
        } else {
            logger.error(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_WRONG, versioningStatus);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    }
    s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, versionId);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Also used : S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) DataDeleteObject(com.pspace.ifs.ksan.gw.data.DataDeleteObject) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 17 with S3Metadata

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

the class DeleteObjects method deleteObject.

public void deleteObject(S3Parameter s3Parameter, String object, String versionId, XMLStreamWriter xml, boolean quiet) throws GWException {
    String bucket = s3Parameter.getBucketName();
    String versioningStatus = getBucketInfo().getVersioning();
    Metadata objMeta = null;
    try {
        if (Strings.isNullOrEmpty(versionId)) {
            objMeta = open(bucket, object);
            versionId = objMeta.getVersionId();
        } else {
            objMeta = open(bucket, object, versionId);
        }
    } catch (GWException e) {
        logger.debug(GWConstants.LOG_DELETE_OBJECTS_QUIET_VALUE, quiet);
        if (!quiet) {
            try {
                xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                writeSimpleElement(xml, GWConstants.VERSIONID, versionId);
                xml.writeEndElement();
                logger.debug(GWConstants.LOG_DELETE_OBJECTS_ERROR, object, versionId, e.getError().getErrorCode(), e.getError().getMessage());
                return;
            } catch (XMLStreamException e1) {
                PrintStack.logging(logger, e1);
                throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
            }
        }
    }
    boolean isLastVersion = objMeta.getLastVersion();
    String deleteMarker = objMeta.getDeleteMarker();
    logger.debug(GWConstants.LOG_DELETE_OBJECT_INFO, versionId, isLastVersion, deleteMarker);
    S3ObjectOperation objectOperation = new S3ObjectOperation(objMeta, null, s3Parameter, versionId, null);
    try {
        if (versioningStatus.equalsIgnoreCase(GWConstants.VERSIONING_ENABLED)) {
            // Bucket Versioning Enabled
            logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_ENABLED);
            if (GWConstants.VERSIONING_DISABLE_TAIL.equals(versionId)) {
                // request versionId is null
                if (deleteMarker != null && deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_MARK)) {
                    remove(bucket, object, versionId);
                    if (!quiet) {
                        xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                        writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                        xml.writeEndElement();
                    }
                } else {
                    try {
                        S3Metadata s3Metadata = new S3Metadata();
                        s3Metadata.setName(object);
                        s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
                        s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
                        s3Metadata.setDeleteMarker(GWConstants.OBJECT_TYPE_MARK);
                        s3Metadata.setVersionId(versionId);
                        ObjectMapper jsonMapper = new ObjectMapper();
                        String jsonmeta = "";
                        jsonmeta = jsonMapper.writeValueAsString(s3Metadata);
                        int result;
                        objMeta.set("", "", jsonmeta, "", 0L);
                        objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_MARK, true);
                        result = insertObject(bucket, object, objMeta);
                        logger.debug(GWConstants.LOG_PUT_DELETE_MARKER);
                        xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                        writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                        writeSimpleElement(xml, GWConstants.XML_DELETE_MARKER, GWConstants.XML_TRUE);
                        writeSimpleElement(xml, GWConstants.DELETE_RESULT_DELETE_MARKER_VERSION_ID, versionId);
                        xml.writeEndElement();
                    } catch (GWException | JsonProcessingException e) {
                        PrintStack.logging(logger, e);
                        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
                    }
                }
            } else {
                // request with versionId
                if (isLastVersion) {
                    // request with versionId and same currentVid
                    if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_MARK)) {
                        remove(bucket, object);
                    } else if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_FILE)) {
                        remove(bucket, object, versionId);
                        objectOperation.deleteObject();
                    }
                } else {
                    // request with versionId not currentVid
                    remove(bucket, object, versionId);
                    objectOperation.deleteObject();
                }
                if (!quiet) {
                    xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                    writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                    writeSimpleElement(xml, GWConstants.VERSIONID, versionId);
                    xml.writeEndElement();
                }
            }
        } else if (versioningStatus.equalsIgnoreCase(GWConstants.VERSIONING_SUSPENDED)) {
            // Bucket Versioning Suspended
            logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_SUSPENDED);
            if (GWConstants.VERSIONING_DISABLE_TAIL.equals(versionId)) {
                if (isLastVersion) {
                    if (deleteMarker.equalsIgnoreCase(GWConstants.OBJECT_TYPE_MARK)) {
                        remove(bucket, object, versionId);
                    } else {
                        try {
                            S3Metadata s3Metadata = new S3Metadata();
                            s3Metadata.setName(object);
                            s3Metadata.setOwnerId(String.valueOf(s3Parameter.getUser().getUserId()));
                            s3Metadata.setOwnerName(s3Parameter.getUser().getUserName());
                            s3Metadata.setDeleteMarker(GWConstants.OBJECT_TYPE_MARK);
                            s3Metadata.setVersionId(versionId);
                            ObjectMapper jsonMapper = new ObjectMapper();
                            String jsonmeta = "";
                            jsonmeta = jsonMapper.writeValueAsString(s3Metadata);
                            int result;
                            objMeta.set("", "", jsonmeta, "", 0L);
                            objMeta.setVersionId(versionId, GWConstants.OBJECT_TYPE_MARK, true);
                            result = insertObject(bucket, object, objMeta);
                            // }
                            if (result != 0) {
                                logger.error(GWConstants.LOG_DELETE_OBJECT_FAILED_MARKER, bucket, object);
                            }
                            logger.debug(GWConstants.LOG_PUT_DELETE_MARKER);
                        } catch (GWException | JsonProcessingException e) {
                            PrintStack.logging(logger, e);
                            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
                        }
                    }
                }
            } else {
                // request with versionId
                remove(bucket, object, versionId);
                objectOperation.deleteObject();
            }
            if (!quiet) {
                xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                writeSimpleElement(xml, GWConstants.XML_DELETE_MARKER, GWConstants.STRING_TRUE);
                writeSimpleElement(xml, GWConstants.DELETE_RESULT_DELETE_MARKER_VERSION_ID, GWConstants.VERSIONING_DISABLE_TAIL);
                xml.writeEndElement();
            }
        } else {
            // Bucket Versioning Disabled
            logger.debug(GWConstants.LOG_DELETE_OBJECT_BUCKET_VERSIONING_DISABLED);
            remove(bucket, object);
            objectOperation.deleteObject();
            if (!quiet) {
                xml.writeStartElement(GWConstants.DELETE_RESULT_DELETED);
                writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                xml.writeEndElement();
            }
        }
    } catch (GWException e) {
        try {
            if (!quiet) {
                xml.writeStartElement(GWConstants.STRING_ERROR);
                writeSimpleElement(xml, GWConstants.DELETE_RESULT_KEY, object);
                writeSimpleElement(xml, GWConstants.VERSIONID, versionId);
                writeSimpleElement(xml, GWConstants.CODE, e.getError().getErrorCode());
                writeSimpleElement(xml, GWConstants.MESSAGE, e.getError().getMessage());
                xml.writeEndElement();
            }
        } catch (XMLStreamException e1) {
            throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
        }
    } catch (XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
}
Also used : S3ObjectOperation(com.pspace.ifs.ksan.gw.object.S3ObjectOperation) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 18 with S3Metadata

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

the class HeadObject method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_HEAD_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);
    }
    DataHeadObject dataHeadObject = new DataHeadObject(s3Parameter);
    dataHeadObject.extract();
    String versionId = dataHeadObject.getVersionId();
    String expectedBucketOwner = dataHeadObject.getExpectedBucketOwner();
    if (!Strings.isNullOrEmpty(expectedBucketOwner)) {
        if (!isBucketOwner(expectedBucketOwner)) {
            throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
        }
    }
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
        versionId = objMeta.getVersionId();
    } else {
        objMeta = open(bucket, object, versionId);
    }
    objMeta.setAcl(GWUtils.makeOriginalXml(objMeta.getAcl(), s3Parameter));
    checkGrantObject(s3Parameter.isPublicAccess(), objMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    // meta info
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        logger.debug(GWConstants.LOG_META, objMeta.getMeta());
        S3Metadata s3Metadata = objectMapper.readValue(objMeta.getMeta(), S3Metadata.class);
        // check customer-key
        if (!Strings.isNullOrEmpty(s3Metadata.getCustomerKey())) {
            if (!Strings.isNullOrEmpty(dataHeadObject.getServerSideEncryptionCustomerKey())) {
                if (!s3Metadata.getCustomerKey().equals(dataHeadObject.getServerSideEncryptionCustomerKey())) {
                    logger.warn(GWConstants.ENCRYPTION_CUSTOMER_KEY_IS_INVALID);
                    throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
                }
            } else {
                logger.warn(GWConstants.ENCRYPTION_CUSTOMER_KEY_IS_NULL);
                throw new GWException(GWErrorCode.BAD_REQUEST, s3Parameter);
            }
        }
        s3Parameter.getResponse().addHeader(GWConstants.X_AMZ_VERSION_ID, s3Metadata.getVersionId());
        GWUtils.addMetadataToResponse(s3Parameter.getRequest(), s3Parameter.getResponse(), s3Metadata, null, null);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) DataHeadObject(com.pspace.ifs.ksan.gw.data.DataHeadObject) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

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