Search in sources :

Example 16 with S3Bucket

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

the class GetObjectRetention method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_GET_OBJECT_RETENTION_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);
    }
    DataGetObjectRetention dataGetObjectRetention = new DataGetObjectRetention(s3Parameter);
    dataGetObjectRetention.extract();
    String versionId = dataGetObjectRetention.getVersionId();
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
    } else {
        objMeta = open(bucket, object, versionId);
    }
    logger.debug(GWConstants.LOG_OBJECT_META, objMeta.toString());
    objMeta.setAcl(GWUtils.makeOriginalXml(objMeta.getAcl(), s3Parameter));
    checkGrantObjectOwner(s3Parameter.isPublicAccess(), objMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    String meta = objMeta.getMeta();
    S3Metadata s3Metadata;
    try {
        s3Metadata = new ObjectMapper().readValue(meta, S3Metadata.class);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    if (!Strings.isNullOrEmpty(s3Metadata.getLockMode())) {
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
        javax.xml.stream.XMLStreamWriter xml;
        try {
            xml = xmlOutputFactory.createXMLStreamWriter(s3Parameter.getResponse().getOutputStream());
            xml.writeStartDocument();
            xml.writeStartElement(GWConstants.RETENTION);
            writeSimpleElement(xml, GWConstants.XML_MODE, s3Metadata.getLockMode());
            writeSimpleElement(xml, GWConstants.RETAIN_UNTIL_DATE, s3Metadata.getLockExpires());
            xml.writeEndElement();
            xml.flush();
        } catch (XMLStreamException | IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    } else {
        throw new GWException(GWErrorCode.INVALID_REQUEST, s3Parameter);
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataGetObjectRetention(com.pspace.ifs.ksan.gw.data.DataGetObjectRetention) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) IOException(java.io.IOException) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 17 with S3Bucket

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

the class GetObjectTagging method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_GET_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);
    }
    DataGetObjectTagging dataGetObjectTagging = new DataGetObjectTagging(s3Parameter);
    dataGetObjectTagging.extract();
    String object = s3Parameter.getObjectName();
    String versionId = dataGetObjectTagging.getVersionId();
    Metadata objMeta = null;
    if (Strings.isNullOrEmpty(versionId)) {
        objMeta = open(bucket, object);
    } else {
        objMeta = open(bucket, object, versionId);
    }
    objMeta.setAcl(GWUtils.makeOriginalXml(objMeta.getAcl(), s3Parameter));
    checkGrantObjectOwner(s3Parameter.isPublicAccess(), objMeta, String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    String taggingInfo = objMeta.getTag();
    logger.info(GWConstants.LOG_TAGGING, taggingInfo);
    if (Strings.isNullOrEmpty(taggingInfo)) {
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
        javax.xml.stream.XMLStreamWriter xml;
        try {
            xml = xmlOutputFactory.createXMLStreamWriter(s3Parameter.getResponse().getOutputStream());
            xml.writeStartDocument();
            xml.writeStartElement(GWConstants.TAGGING);
            xml.writeDefaultNamespace(GWConstants.AWS_XMLNS);
            xml.writeStartElement(GWConstants.TAG_SET);
            xml.writeEndElement();
            xml.writeEndElement();
            xml.flush();
        } catch (XMLStreamException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    } else {
        try {
            if (!Strings.isNullOrEmpty(taggingInfo)) {
                s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
                s3Parameter.getResponse().getOutputStream().write(taggingInfo.getBytes());
            }
        } catch (IOException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) Metadata(com.pspace.ifs.ksan.objmanager.Metadata) DataGetObjectTagging(com.pspace.ifs.ksan.gw.data.DataGetObjectTagging) GWException(com.pspace.ifs.ksan.gw.exception.GWException) IOException(java.io.IOException)

Example 18 with S3Bucket

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

the class ListMultipartUploads method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_LIST_MULTIPART_UPLOADS_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    DataListMultipartUploads dataListMultipartUploads = new DataListMultipartUploads(s3Parameter);
    dataListMultipartUploads.extract();
    String delimiter = dataListMultipartUploads.getDelimiter();
    String encodingType = dataListMultipartUploads.getEncodingType();
    String prefix = dataListMultipartUploads.getPrefix();
    String keyMarker = dataListMultipartUploads.getKeyMarker();
    String uploadIdMarker = dataListMultipartUploads.getUploadIdMarker();
    String maxUploads = dataListMultipartUploads.getMaxUploads();
    if (Strings.isNullOrEmpty(maxUploads)) {
        maxUploads = GWConstants.DEFAULT_MAX_KEYS;
    } else {
        if (Integer.valueOf(maxUploads) < 0) {
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
    }
    ResultUploads resultUploads = null;
    ObjMultipart objMultipart = null;
    try {
        objMultipart = new ObjMultipart(bucket);
        resultUploads = objMultipart.getUploads(bucket, delimiter, prefix, keyMarker, uploadIdMarker, Integer.valueOf(maxUploads));
    } catch (UnknownHostException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.LIST_MULTIPART_UPLOADS_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
        if (prefix == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeBlob(encodingType, prefix));
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_UPLOADS, String.valueOf(maxUploads));
        if (keyMarker == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_KEY_MARKER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_MARKER, GWUtils.encodeBlob(encodingType, keyMarker));
        }
        if (uploadIdMarker == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_UPLOADID_MARKER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID_MARKER, GWUtils.encodeBlob(encodingType, uploadIdMarker));
        }
        if (delimiter != null) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeBlob(encodingType, delimiter));
        }
        if (encodingType != null && encodingType.equals(GWConstants.URL)) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, encodingType);
        }
        if (resultUploads.isTruncated()) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_KEY_MARKER, GWUtils.encodeBlob(encodingType, resultUploads.getKeyMarker()));
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_UPLOADID_MARKER, GWUtils.encodeBlob(encodingType, resultUploads.getUploadIdMarker()));
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
        }
        for (Upload upload : resultUploads.getList()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_UPLOAD);
            writeSimpleElement(xmlStreamWriter, GWConstants.KEY, upload.getObject());
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID, upload.getUploadId());
            writeInitiatorStanza(xmlStreamWriter);
            writeOwnerInfini(xmlStreamWriter, upload.getOwnerID(), upload.getOwnerName());
            writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, GWConstants.AWS_TIER_STANTARD);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_INITIATED, formatDate(upload.getChangeTime()));
            xmlStreamWriter.writeEndElement();
            logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_KEY, upload.getObject());
            logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_UPLOADID, upload.getUploadId());
            logger.debug(GWConstants.LOG_LIST_MULTIPART_UPLOADS_CHANGE_TIME, formatDate(upload.getChangeTime()));
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
}
Also used : ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) UnknownHostException(java.net.UnknownHostException) Upload(com.pspace.ifs.ksan.gw.object.multipart.Upload) IOException(java.io.IOException) ResultUploads(com.pspace.ifs.ksan.gw.object.multipart.ResultUploads) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) XMLStreamException(javax.xml.stream.XMLStreamException) DataListMultipartUploads(com.pspace.ifs.ksan.gw.data.DataListMultipartUploads) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Writer(java.io.Writer) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 19 with S3Bucket

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

the class ListObjectVersions method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_LIST_OBJECT_VERSIONS_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    DataListObjectVersions dataListObjectVersions = new DataListObjectVersions(s3Parameter);
    dataListObjectVersions.extract();
    String delimiter = dataListObjectVersions.getDelimiter();
    String encodingType = dataListObjectVersions.getEncodingType();
    String keyMarker = dataListObjectVersions.getKeyMarker();
    String maxKeys = dataListObjectVersions.getMaxKeys();
    String prefix = dataListObjectVersions.getPrefix();
    String versionIdMarker = dataListObjectVersions.getVersionIdMarker();
    S3ObjectList s3ObjectList = new S3ObjectList();
    s3ObjectList.setDelimiter(delimiter);
    s3ObjectList.setEncodingType(encodingType);
    s3ObjectList.setKeyMarker(keyMarker);
    s3ObjectList.setMaxKeys(maxKeys);
    if (Strings.isNullOrEmpty(s3ObjectList.getMaxKeys())) {
        s3ObjectList.setMaxKeys(GWConstants.DEFAULT_MAX_KEYS);
    }
    logger.debug(GWConstants.LOG_LIST_OBJECT_VERSIONS_MAXKEYS, s3ObjectList.getMaxKeys());
    if (Integer.valueOf(s3ObjectList.getMaxKeys()) < 0) {
        throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
    }
    s3ObjectList.setPrefix(prefix);
    s3ObjectList.setVersionIdMarker(versionIdMarker);
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    ObjectListParameter objectListParameter = listObjectVersions(bucket, s3ObjectList);
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.LIST_VERSIONS_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_NAME, bucket);
        if (prefix == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeBlob(encodingType, prefix));
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_KEYS, String.valueOf(s3ObjectList.getMaxKeys()));
        if (keyMarker == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_KEY_MARKER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_MARKER, GWUtils.encodeBlob(encodingType, keyMarker));
        }
        if (versionIdMarker == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_VERSIONID_MARKER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_VERSIONID_MARKER, GWUtils.encodeBlob(encodingType, versionIdMarker));
        }
        logger.debug(GWConstants.LOG_LIST_OBJECT_VERSIONS_PARAMETER_INFO, objectListParameter, objectListParameter.getObjects());
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_KEY_COUNT, String.valueOf(objectListParameter.getObjects().size()));
        if (delimiter != null) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeBlob(encodingType, delimiter));
        }
        if (encodingType != null && encodingType.equals(GWConstants.URL)) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, encodingType);
        }
        if (objectListParameter.isTruncated()) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_KEY_MARKER, GWUtils.encodeBlob(encodingType, objectListParameter.getNextMarker()));
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_VERSIONID_MARKER, GWUtils.encodeBlob(encodingType, objectListParameter.getNextVersion()));
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
        }
        for (S3Metadata s3Metadata : objectListParameter.getObjects()) {
            if (s3Metadata != null) {
                if (s3Metadata.getDeleteMarker() != null) {
                    if (s3Metadata.getDeleteMarker().compareTo(GWConstants.OBJECT_TYPE_MARK) == 0) {
                        xmlStreamWriter.writeStartElement(GWConstants.XML_DELETE_MARKER);
                    } else {
                        xmlStreamWriter.writeStartElement(GWConstants.VERSION);
                    }
                    writeSimpleElement(xmlStreamWriter, GWConstants.KEY, GWUtils.encodeBlob(encodingType, s3Metadata.getName()));
                    if (s3Metadata.getLastModified() != null) {
                        writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
                    }
                    if (s3Metadata.getETag() != null) {
                        writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
                    }
                    writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, s3Metadata.getSize().toString());
                    writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, s3Metadata.getTier());
                    writeSimpleElement(xmlStreamWriter, GWConstants.VERSIONID, s3Metadata.getVersionId());
                    writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_LATEST, s3Metadata.getIsLatest());
                    writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
                    xmlStreamWriter.writeEndElement();
                    logger.debug(GWConstants.LOG_LIST_OBJECT_VERSIONS_INFO, s3Metadata.getName(), s3Metadata.getVersionId());
                }
            }
        }
        for (Entry<String, String> entry : objectListParameter.getCommonPrefixes().entrySet()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_COMMON_PREFIXES);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeBlob(encodingType, entry.getValue()));
            xmlStreamWriter.writeEndElement();
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } catch (XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataListObjectVersions(com.pspace.ifs.ksan.gw.data.DataListObjectVersions) IOException(java.io.IOException) S3ObjectList(com.pspace.ifs.ksan.gw.identity.S3ObjectList) ObjectListParameter(com.pspace.ifs.ksan.gw.identity.ObjectListParameter) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Writer(java.io.Writer) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 20 with S3Bucket

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

the class ListObjects method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_LIST_OBJECT_START);
    String bucket = s3Parameter.getBucketName();
    initBucketInfo(bucket);
    S3Bucket s3Bucket = new S3Bucket();
    s3Bucket.setCors(getBucketInfo().getCors());
    s3Bucket.setAccess(getBucketInfo().getAccess());
    s3Parameter.setBucket(s3Bucket);
    GWUtils.checkCors(s3Parameter);
    if (s3Parameter.isPublicAccess() && GWUtils.isIgnorePublicAcls(s3Parameter)) {
        throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
    }
    checkGrantBucket(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_READ);
    DataListBuckets dataListBuckets = new DataListBuckets(s3Parameter);
    dataListBuckets.extract();
    S3ObjectList s3ObjectList = new S3ObjectList();
    if (!Strings.isNullOrEmpty(dataListBuckets.getMaxkeys())) {
        if (Integer.valueOf(dataListBuckets.getMaxkeys()) < 0) {
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
        s3ObjectList.setMaxKeys(dataListBuckets.getMaxkeys());
    } else {
        s3ObjectList.setMaxKeys(GWConstants.DEFAULT_MAX_KEYS);
    }
    s3ObjectList.setDelimiter(dataListBuckets.getDelimiter());
    s3ObjectList.setEncodingType(dataListBuckets.getEncodingType());
    s3ObjectList.setMarker(dataListBuckets.getMarker());
    s3ObjectList.setPrefix(dataListBuckets.getPrefix());
    logger.debug("delimiter : {}", dataListBuckets.getDelimiter());
    logger.debug("marker : {}", dataListBuckets.getMarker());
    logger.debug("prefix : {}", dataListBuckets.getPrefix());
    logger.debug("maxKeys : {}", dataListBuckets.getMaxkeys());
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    ObjectListParameter objectListParameter = listObject(bucket, s3ObjectList);
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.LIST_BUCKET_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_NAME, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), bucket));
        if (s3ObjectList.getPrefix() == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_PREFIX);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getPrefix()));
        }
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_MAX_KEYS, String.valueOf(s3ObjectList.getMaxKeys()));
        if (s3ObjectList.getMarker() == null) {
            xmlStreamWriter.writeEmptyElement(GWConstants.XML_MARKER);
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_MARKER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getMarker()));
        }
        if (s3ObjectList.getDelimiter() != null) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_DELIMITER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3ObjectList.getDelimiter()));
        }
        if (s3ObjectList.getEncodingType() != null && s3ObjectList.getEncodingType().equals(GWConstants.URL)) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_ENCODING_TYPE, s3ObjectList.getEncodingType());
        }
        if (objectListParameter.isTruncated()) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_MARKER, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), objectListParameter.getNextMarker()));
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
        }
        for (S3Metadata s3Metadata : objectListParameter.getObjects()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_CONTENTS);
            logger.debug(s3Metadata.getName());
            writeSimpleElement(xmlStreamWriter, GWConstants.KEY, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), s3Metadata.getName()));
            if (s3Metadata.getLastModified() != null) {
                writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(s3Metadata.getLastModified()));
            }
            if (s3Metadata.getETag() != null) {
                writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, GWUtils.maybeQuoteETag(s3Metadata.getETag()));
            }
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, s3Metadata.getSize().toString());
            writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, s3Metadata.getTier());
            writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
            xmlStreamWriter.writeEndElement();
        }
        for (Entry<String, String> prefix : objectListParameter.getCommonPrefixes().entrySet()) {
            xmlStreamWriter.writeStartElement(GWConstants.XML_COMMON_PREFIXES);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_PREFIX, GWUtils.encodeObjectName(s3ObjectList.getEncodingType(), prefix.getValue()));
            xmlStreamWriter.writeEndElement();
            logger.debug(GWConstants.LOG_LIST_OBJECT_PREFIX_ENCODING, s3ObjectList.getEncodingType(), prefix.getValue());
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.flush();
    } catch (IOException | XMLStreamException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) IOException(java.io.IOException) S3ObjectList(com.pspace.ifs.ksan.gw.identity.S3ObjectList) DataListBuckets(com.pspace.ifs.ksan.gw.data.DataListBuckets) ObjectListParameter(com.pspace.ifs.ksan.gw.identity.ObjectListParameter) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Writer(java.io.Writer) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

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