Skip to content

Commit

Permalink
api: add new bucket notification APIs. (#551)
Browse files Browse the repository at this point in the history
Below methods are added

* public NotificationConfiguration getBucketNotification(String bucketName)
* public void setBucketNotification(String bucketName,
                  NotificationConfiguration notificationConfiguration)
* public void removeAllBucketNotification(String bucketName)

Fixes #437
  • Loading branch information
balamurugana authored and harshavardhana committed Apr 18, 2017
1 parent c93f11b commit 755099a
Show file tree
Hide file tree
Showing 13 changed files with 1,149 additions and 0 deletions.
92 changes: 92 additions & 0 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.minio.messages.Part;
import io.minio.messages.Prefix;
import io.minio.messages.Upload;
import io.minio.messages.NotificationConfiguration;
import io.minio.org.apache.commons.validator.routines.InetAddressValidator;
import io.minio.policy.PolicyType;
import io.minio.policy.BucketPolicy;
Expand Down Expand Up @@ -2987,6 +2988,97 @@ public void setBucketPolicy(String bucketName, String objectPrefix, PolicyType p
}


/**
* Get bucket notification configuration
*
* @param bucketName Bucket name.
*
* </p><b>Example:</b><br>
* <pre>{@code NotificationConfiguration notificationConfig = minioClient.getBucketNotification("my-bucketname");
* }</pre>
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoResponseException upon no response from server
* @throws IOException upon connection error
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
*
*/
public NotificationConfiguration getBucketNotification(String bucketName)
throws InvalidBucketNameException, InvalidObjectPrefixException, NoSuchAlgorithmException,
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
XmlPullParserException, ErrorResponseException, InternalException {
Map<String,String> queryParamMap = new HashMap<>();
queryParamMap.put("notification", "");

HttpResponse response = executeGet(bucketName, null, null, queryParamMap);
NotificationConfiguration result = new NotificationConfiguration();
try {
result.parseXml(response.body().charStream());
} finally {
response.body().close();
}

return result;
}


/**
* Set bucket notification configuration
*
* @param bucketName Bucket name.
* @param notificationConfiguration Notification configuration to be set.
*
* </p><b>Example:</b><br>
* <pre>{@code minioClient.setBucketNotification("my-bucketname", notificationConfiguration);
* }</pre>
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoResponseException upon no response from server
* @throws IOException upon connection error
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
*
*/
public void setBucketNotification(String bucketName, NotificationConfiguration notificationConfiguration)
throws InvalidBucketNameException, InvalidObjectPrefixException, NoSuchAlgorithmException,
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
XmlPullParserException, ErrorResponseException, InternalException {
Map<String,String> queryParamMap = new HashMap<>();
queryParamMap.put("notification", "");
HttpResponse response = executePut(bucketName, null, null, queryParamMap, notificationConfiguration.toString(), 0);
response.body().close();
}


/**
* Remove all bucket notification.
*
* @param bucketName Bucket name.
*
* </p><b>Example:</b><br>
* <pre>{@code minioClient.removeAllBucketNotification("my-bucketname");
* }</pre>
*
* @throws InvalidBucketNameException upon invalid bucket name is given
* @throws NoResponseException upon no response from server
* @throws IOException upon connection error
* @throws XmlPullParserException upon parsing response xml
* @throws ErrorResponseException upon unsuccessful execution
* @throws InternalException upon internal library error
*
*/
public void removeAllBucketNotification(String bucketName)
throws InvalidBucketNameException, InvalidObjectPrefixException, NoSuchAlgorithmException,
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
XmlPullParserException, ErrorResponseException, InternalException {
NotificationConfiguration notificationConfiguration = new NotificationConfiguration();
setBucketNotification(bucketName, notificationConfiguration);
}


/**
* Returns next part if exists.
*/
Expand Down
111 changes: 111 additions & 0 deletions api/src/main/java/io/minio/messages/CloudFunctionConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.messages;

import java.util.LinkedList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import com.google.api.client.util.Key;

import io.minio.errors.InvalidArgumentException;


/**
* Helper class to parse Amazon AWS S3 response XML containing cloud function configuration.
*/
public class CloudFunctionConfiguration extends XmlEntity {
@Key("Id")
private String id;
@Key("CloudFunction")
private String cloudFunction;
@Key("Event")
private List<String> events = new LinkedList<>();
@Key("Filter")
private Filter filter;


public CloudFunctionConfiguration() throws XmlPullParserException {
super();
super.name = "CloudFunctionConfiguration";
}


/**
* Returns id.
*/
public String id() {
return id;
}


/**
* Sets id.
*/
public void setId(String id) {
this.id = id;
}


/**
* Returns cloudFunction.
*/
public String cloudFunction() {
return cloudFunction;
}


/**
* Sets cloudFunction.
*/
public void setCloudFunction(String cloudFunction) {
this.cloudFunction = cloudFunction;
}


/**
* Returns events.
*/
public List<EventType> events() throws InvalidArgumentException {
return EventType.fromStringList(events);
}


/**
* Sets event.
*/
public void setEvents(List<EventType> events) {
this.events = EventType.toStringList(events);
}


/**
* Returns filter.
*/
public Filter filter() {
return filter;
}


/**
* Sets filter.
*/
public void setFilter(Filter filter) {
this.filter = filter;
}
}
91 changes: 91 additions & 0 deletions api/src/main/java/io/minio/messages/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.messages;

import java.util.LinkedList;
import java.util.List;

import io.minio.errors.InvalidArgumentException;


/**
* Amazon AWS S3 event types for notifications.
*/
public enum EventType {
OBJECT_CREATED_ANY("s3:ObjectCreated:*"),
OBJECT_CREATED_PUT("s3:ObjectCreated:Put"),
OBJECT_CREATED_POST("s3:ObjectCreated:Post"),
OBJECT_CREATED_COPY("s3:ObjectCreated:Copy"),
OBJECT_CREATED_COMPLETE_MULTIPART_UPLOAD("s3:ObjectCreated:CompleteMultipartUpload"),
OBJECT_REMOVED_ANY("s3:ObjectRemoved:*"),
OBJECT_REMOVED_DELETE("s3:ObjectRemoved:Delete"),
OBJECT_REMOVED_DELETED_MARKER_CREATED("s3:ObjectRemoved:DeleteMarkerCreated"),
REDUCED_REDUNDANCY_LOST_OBJECT("s3:ReducedRedundancyLostObject");

private final String value;


private EventType(String value) {
this.value = value;
}


public String toString() {
return this.value;
}


/**
* Returns EventType of given string.
*/
public static EventType fromString(String eventTypeString) throws InvalidArgumentException {
for (EventType et : EventType.values()) {
if (eventTypeString.equals(et.value)) {
return et;
}
}


throw new InvalidArgumentException("unknown event '" + eventTypeString + "'");
}


/**
* Returns List&lt;EventType&gt; of given List&lt;String&gt;.
*/
public static List<EventType> fromStringList(List<String> eventList) throws InvalidArgumentException {
List<EventType> eventTypeList = new LinkedList<EventType>();
for (String event: eventList) {
eventTypeList.add(EventType.fromString(event));
}

return eventTypeList;
}


/**
* Returns List&lt;String&gt; of given List&lt;EventType&gt;.
*/
public static List<String> toStringList(List<EventType> eventTypeList) {
List<String> events = new LinkedList<String>();
for (EventType eventType: eventTypeList) {
events.add(eventType.toString());
}

return events;
}
}
65 changes: 65 additions & 0 deletions api/src/main/java/io/minio/messages/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.messages;

import org.xmlpull.v1.XmlPullParserException;

import com.google.api.client.util.Key;

import io.minio.errors.InvalidArgumentException;


/**
* Helper class to parse Amazon AWS S3 response XML containing Filter.
*/
@SuppressWarnings("WeakerAccess")
public class Filter extends XmlEntity {
@Key("S3Key")
private S3Key s3Key = new S3Key();


public Filter() throws XmlPullParserException {
super();
super.name = "Filter";
}


/**
* Returns S3 Key.
*/
public S3Key s3Key() {
return s3Key;
}


/**
* Sets S3 Key.
*/
public void setS3Key(S3Key s3Key) {
this.s3Key = s3Key;
}


public void setPrefixRule(String value) throws InvalidArgumentException, XmlPullParserException {
s3Key.setPrefixRule(value);
}


public void setSuffixRule(String value) throws InvalidArgumentException, XmlPullParserException {
s3Key.setSuffixRule(value);
}
}
Loading

0 comments on commit 755099a

Please sign in to comment.