Search in sources :

Example 21 with S3Bucket

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

the class ListParts method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_LIST_PARTS_START);
    String bucket = s3Parameter.getBucketName();
    if (!isExistBucket(bucket)) {
        logger.error(GWConstants.LOG_BUCKET_IS_NOT_EXIST, bucket);
        throw new GWException(GWErrorCode.NO_SUCH_BUCKET, s3Parameter);
    }
    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);
    DataListParts dataListParts = new DataListParts(s3Parameter);
    dataListParts.extract();
    String maxParts = dataListParts.getMaxParts();
    String partNumberMarker = dataListParts.getPartNumberMarker();
    String uploadId = dataListParts.getUploadId();
    int maxPartsValue = GWConstants.MAX_PART_VALUE;
    if (!Strings.isNullOrEmpty(maxParts)) {
        if (Integer.valueOf(maxParts) < 0) {
            throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
        }
        maxPartsValue = Integer.valueOf(maxParts);
    }
    ResultParts resultPart = null;
    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);
        }
        // check acl use multipart acl
        resultPart = objMultipart.getParts(uploadId, partNumberMarker, maxPartsValue);
    } 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);
    }
    String meta = multipart.getMeta();
    ObjectMapper jsonMapper = new ObjectMapper();
    S3Metadata s3Metadata;
    try {
        s3Metadata = jsonMapper.readValue(meta, S3Metadata.class);
    } catch (JsonMappingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    } catch (JsonProcessingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    s3Parameter.getResponse().setCharacterEncoding(GWConstants.CHARSET_UTF_8);
    try (Writer writer = s3Parameter.getResponse().getWriter()) {
        s3Parameter.getResponse().setContentType(GWConstants.XML_CONTENT_TYPE);
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(writer);
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(GWConstants.LIST_PARTS_RESULT);
        xmlStreamWriter.writeDefaultNamespace(GWConstants.AWS_XMLNS);
        writeSimpleElement(xmlStreamWriter, GWConstants.BUCKET, bucket);
        writeSimpleElement(xmlStreamWriter, GWConstants.KEY, s3Parameter.getObjectName());
        writeSimpleElement(xmlStreamWriter, GWConstants.XML_UPLOADID, uploadId);
        writeInitiatorStanza(xmlStreamWriter);
        writeOwnerInfini(xmlStreamWriter, s3Metadata.getOwnerId(), s3Metadata.getOwnerName());
        writeSimpleElement(xmlStreamWriter, GWConstants.STORAGE_CLASS, GWConstants.AWS_TIER_STANTARD);
        if (resultPart.isTruncated()) {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_TRUE);
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_NEXT_PART_NUMBER, String.valueOf(resultPart.getPartNumberMarker()));
        } else {
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_IS_TRUNCATED, GWConstants.XML_FALSE);
        }
        for (Iterator<Map.Entry<Integer, Part>> it = resultPart.getListPart().entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<Integer, Part> entry = it.next();
            xmlStreamWriter.writeStartElement(GWConstants.PART);
            writeSimpleElement(xmlStreamWriter, GWConstants.PARTNUMBER, String.valueOf(entry.getValue().getPartNumber()));
            writeSimpleElement(xmlStreamWriter, GWConstants.LAST_MODIFIED, formatDate(entry.getValue().getLastModified()));
            writeSimpleElement(xmlStreamWriter, GWConstants.ETAG, entry.getValue().getPartETag());
            writeSimpleElement(xmlStreamWriter, GWConstants.XML_SIZE, String.valueOf(entry.getValue().getPartSize()));
            xmlStreamWriter.writeEndElement();
            logger.debug(GWConstants.LOG_LIST_PARTS_PART_NUMBER, entry.getValue().getPartNumber());
            logger.debug(GWConstants.LOG_LIST_PARTS_LAST_MODIFIED, formatDate(entry.getValue().getLastModified()));
            logger.debug(GWConstants.LOG_LIST_PARTS_ETAG, entry.getValue().getPartETag());
            logger.debug(GWConstants.LOG_LIST_PARTS_SIZE, String.valueOf(entry.getValue().getPartSize()));
        }
        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) Multipart(com.pspace.ifs.ksan.gw.object.multipart.Multipart) ObjMultipart(com.pspace.ifs.ksan.objmanager.ObjMultipart) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataListParts(com.pspace.ifs.ksan.gw.data.DataListParts) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) UnknownHostException(java.net.UnknownHostException) ResultParts(com.pspace.ifs.ksan.gw.object.multipart.ResultParts) 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) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) XMLStreamException(javax.xml.stream.XMLStreamException) S3Metadata(com.pspace.ifs.ksan.gw.identity.S3Metadata) Part(com.pspace.ifs.ksan.gw.object.multipart.Part) Map(java.util.Map) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer)

Example 22 with S3Bucket

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

the class PutBucketCors method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_CORS_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);
    DataPutBucketCors bucketCors = new DataPutBucketCors(s3Parameter);
    bucketCors.extract();
    String corsInfo = bucketCors.getCorsXml();
    updateBucketCors(bucket, corsInfo);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : DataPutBucketCors(com.pspace.ifs.ksan.gw.data.DataPutBucketCors) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 23 with S3Bucket

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

the class PutBucketReplication method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_REPLICATION_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);
    if (!GWConstants.VERSIONING_ENABLED.equalsIgnoreCase(getBucketVersioning(bucket))) {
        throw new GWException(GWErrorCode.INVALID_REPLICATION_REQUEST, s3Parameter);
    }
    DataPutBucketReplication dataPutBucketReplication = new DataPutBucketReplication(s3Parameter);
    dataPutBucketReplication.extract();
    String replicationXml = dataPutBucketReplication.getReplicationXml();
    checkBucketReplication(replicationXml);
    updateBucketReplication(bucket, replicationXml);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) DataPutBucketReplication(com.pspace.ifs.ksan.gw.data.DataPutBucketReplication) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 24 with S3Bucket

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

the class PutBucketTagging method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_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);
    }
    checkGrantBucketOwner(s3Parameter.isPublicAccess(), String.valueOf(s3Parameter.getUser().getUserId()), GWConstants.GRANT_WRITE_ACP);
    DataPutBucketTagging dataPutBucketTagging = new DataPutBucketTagging(s3Parameter);
    dataPutBucketTagging.extract();
    String taggingXml = dataPutBucketTagging.getTaggingXml();
    logger.debug(GWConstants.LOG_PUT_BUCKET_TAGGING, taggingXml);
    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() > 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);
                }
            }
        }
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    }
    updateBucketTagging(bucket, taggingXml);
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) DataPutBucketTagging(com.pspace.ifs.ksan.gw.data.DataPutBucketTagging) Tagging(com.pspace.ifs.ksan.gw.format.Tagging) DataPutBucketTagging(com.pspace.ifs.ksan.gw.data.DataPutBucketTagging) GWException(com.pspace.ifs.ksan.gw.exception.GWException) Tag(com.pspace.ifs.ksan.gw.format.Tagging.TagSet.Tag) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 25 with S3Bucket

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

the class PutBucketVersioning method process.

@Override
public void process() throws GWException {
    logger.info(GWConstants.LOG_PUT_BUCKET_VERSIONING_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);
    DataPutBucketVersioning dataPutBucketVersioning = new DataPutBucketVersioning(s3Parameter);
    dataPutBucketVersioning.extract();
    String versionXml = dataPutBucketVersioning.getVersioningXml();
    if (!Strings.isNullOrEmpty(versionXml)) {
        Versioning versioning = new Versioning();
        try {
            versioning = new XmlMapper().readValue(versionXml, Versioning.class);
        } catch (JsonProcessingException e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
        if (versioning.status != null && !versioning.status.equalsIgnoreCase(GWConstants.VERSIONING_ENABLED) && !versioning.status.equalsIgnoreCase(GWConstants.VERSIONING_SUSPENDED)) {
            throw new GWException(GWErrorCode.MALFORMED_X_M_L, s3Parameter);
        }
        if (!Strings.isNullOrEmpty(getBucketInfo().getObjectLock())) {
            if (!versioning.status.equalsIgnoreCase(GWConstants.VERSIONING_ENABLED)) {
                throw new GWException(GWErrorCode.INVALID_BUCKET_STATE, s3Parameter);
            }
        }
        if (!Strings.isNullOrEmpty(versioning.status)) {
            putBucketVersioning(bucket, versioning.status);
        }
    }
    s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
Also used : DataPutBucketVersioning(com.pspace.ifs.ksan.gw.data.DataPutBucketVersioning) Versioning(com.pspace.ifs.ksan.gw.format.Versioning) DataPutBucketVersioning(com.pspace.ifs.ksan.gw.data.DataPutBucketVersioning) S3Bucket(com.pspace.ifs.ksan.gw.identity.S3Bucket) GWException(com.pspace.ifs.ksan.gw.exception.GWException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

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