Search in sources :

Example 1 with DataListObjectVersions

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

Aggregations

DataListObjectVersions (com.pspace.ifs.ksan.gw.data.DataListObjectVersions)1 GWException (com.pspace.ifs.ksan.gw.exception.GWException)1 ObjectListParameter (com.pspace.ifs.ksan.gw.identity.ObjectListParameter)1 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)1 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)1 S3ObjectList (com.pspace.ifs.ksan.gw.identity.S3ObjectList)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1