use of com.pspace.ifs.ksan.gw.exception.GWException 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);
}
}
use of com.pspace.ifs.ksan.gw.exception.GWException 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);
}
}
use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.
the class OptionsObject method process.
@Override
public void process() throws GWException {
logger.info(GWConstants.LOG_OPTIONS_OBJECT_START);
String bucket = s3Parameter.getBucketName();
initBucketInfo(bucket);
if (Strings.isNullOrEmpty(getBucketInfo().getCors())) {
throw new GWException(GWErrorCode.NO_SUCH_CORS_CONFIGURATION, s3Parameter);
} else {
XmlMapper xmlMapper = new XmlMapper();
try {
CORSConfiguration corsConfiguration = xmlMapper.readValue(getBucketInfo().getCors(), CORSConfiguration.class);
String corsOrigin = s3Parameter.getRequest().getHeader(HttpHeaders.ORIGIN);
if (Strings.isNullOrEmpty(corsOrigin)) {
throw new GWException(GWErrorCode.INVALID_CORS_ORIGIN, s3Parameter);
}
String corsMethods = s3Parameter.getRequest().getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD);
String corsHeaders = s3Parameter.getRequest().getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
boolean originpass = false;
String resOrigin = "";
String allowMethods = "";
String allowHeaders = "";
String maxAges = "";
String exposeHeaders = "";
for (CORSRule icors : corsConfiguration.CORSRules) {
for (String origin : icors.AllowedOrigins) {
if (GWUtils.likematch(origin, corsOrigin)) {
if (origin.equals(GWConstants.ASTERISK)) {
resOrigin = origin;
} else {
resOrigin = corsOrigin;
}
originpass = true;
}
}
if (originpass == false)
continue;
int first = 0;
if (!Strings.isNullOrEmpty(corsMethods) && icors.AllowedMethods != null) {
for (String corsMethod : corsMethods.split(GWConstants.COMMA)) {
boolean temp = false;
if (icors.AllowedMethods == null) {
continue;
}
for (String method : icors.AllowedMethods) {
if (method.compareTo(GWConstants.ASTERISK) == 0 || corsMethod.trim().compareTo(method) == 0) {
temp = true;
}
}
if (temp == true && first == 0) {
allowMethods += corsMethod;
first++;
} else if (temp == true && first > 0) {
allowMethods += GWConstants.COMMA + corsMethod;
first++;
}
}
} else if (Strings.isNullOrEmpty(corsMethods) && icors.AllowedMethods != null) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
first = 0;
if (!Strings.isNullOrEmpty(corsHeaders)) {
for (String corsHeader : corsHeaders.split(GWConstants.COMMA)) {
boolean temp = false;
if (icors.AllowedHeaders == null) {
continue;
}
for (String header : icors.AllowedHeaders) {
if (header.compareTo(GWConstants.ASTERISK) == 0 || corsHeader.trim().compareTo(header) == 0) {
temp = true;
}
}
if (temp == true && first == 0) {
allowHeaders += corsHeader;
first++;
} else if (temp == true && first > 0) {
allowHeaders += GWConstants.COMMA + corsHeader;
first++;
}
}
}
first = 0;
if (icors.ExposeHeaders != null) {
for (String exposeHeader : icors.ExposeHeaders) {
if (first == 0) {
exposeHeaders += exposeHeader;
first++;
} else {
exposeHeaders += GWConstants.COMMA + exposeHeader;
first++;
}
}
}
if (!Strings.isNullOrEmpty(icors.MaxAgeSeconds))
maxAges = icors.MaxAgeSeconds;
if (originpass == true)
break;
}
if (originpass == false) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
// check
if (Strings.isNullOrEmpty(allowMethods)) {
if (!Strings.isNullOrEmpty(corsMethods)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
}
// check
if (Strings.isNullOrEmpty(allowHeaders)) {
if (!Strings.isNullOrEmpty(corsHeaders)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
}
s3Parameter.getResponse().addHeader(HttpHeaders.VARY, HttpHeaders.ORIGIN);
s3Parameter.getResponse().addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, resOrigin);
if (!Strings.isNullOrEmpty(allowMethods)) {
s3Parameter.getResponse().addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, allowMethods);
}
if (!Strings.isNullOrEmpty(allowHeaders)) {
s3Parameter.getResponse().addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, allowHeaders);
}
if (!Strings.isNullOrEmpty(exposeHeaders)) {
s3Parameter.getResponse().addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, exposeHeaders);
}
if (!maxAges.isEmpty()) {
s3Parameter.getResponse().addHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, maxAges);
}
} catch (JsonProcessingException e) {
PrintStack.logging(logger, e);
}
}
s3Parameter.getResponse().setStatus(HttpServletResponse.SC_OK);
}
use of com.pspace.ifs.ksan.gw.exception.GWException 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);
}
use of com.pspace.ifs.ksan.gw.exception.GWException 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);
}
Aggregations