Skip to content

Commit

Permalink
move xml parsing using SimpleXML (#857)
Browse files Browse the repository at this point in the history
This breaks backward compatibility
  • Loading branch information
balamurugana authored Mar 17, 2020
1 parent e54bfc2 commit d972526
Show file tree
Hide file tree
Showing 77 changed files with 1,525 additions and 1,696 deletions.
27 changes: 21 additions & 6 deletions api/src/main/java/io/minio/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

package io.minio;

import org.simpleframework.xml.Root;
import org.simpleframework.xml.convert.Convert;
import org.simpleframework.xml.convert.Converter;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.OutputNode;

/** Amazon AWS S3 error codes. */
@Root(name = "ErrorCode")
@Convert(ErrorCode.ErrorCodeConverter.class)
public enum ErrorCode {
// custom error codes
NO_SUCH_OBJECT("NoSuchKey", "Object does not exist"),
Expand Down Expand Up @@ -167,17 +175,24 @@ public String message() {

/** Returns ErrorCode of given code string. */
public static ErrorCode fromString(String codeString) {
if (codeString == null) {
return null;
}

for (ErrorCode ec : ErrorCode.values()) {
if (codeString.equals(ec.code)) {
return ec;
}
}

// Unknown error code string. Its not a standard Amazon S3 error.
return null;
throw new IllegalArgumentException("unknown error code string '" + codeString + "'");
}

public static class ErrorCodeConverter implements Converter<ErrorCode> {
@Override
public ErrorCode read(InputNode node) throws Exception {
return ErrorCode.fromString(node.getValue());
}

@Override
public void write(OutputNode node, ErrorCode errorCode) throws Exception {
node.setValue(errorCode.code());
}
}
}
547 changes: 237 additions & 310 deletions api/src/main/java/io/minio/MinioClient.java

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions api/src/main/java/io/minio/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.XmlParserException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.xmlpull.v1.XmlPullParserException;

/** A container class keeps any type and exception occured. */
public class Result<T> {
private final T type;
private final Exception ex;

public Result(T type, Exception ex) {
public Result(T type) {
this.type = type;
this.ex = null;
}

public Result(Exception ex) {
this.type = null;
this.ex = ex;
}

/** Returns given Type if exception is null, else respective exception is thrown. */
public T get()
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException,
JsonParseException, JsonMappingException, IOException, InvalidKeyException,
XmlPullParserException, ErrorResponseException, InternalException {
XmlParserException, ErrorResponseException, InternalException {
if (ex == null) {
return type;
}
Expand All @@ -62,8 +67,8 @@ public T get()
throw (InvalidKeyException) ex;
}

if (ex instanceof XmlPullParserException) {
throw (XmlPullParserException) ex;
if (ex instanceof XmlParserException) {
throw (XmlParserException) ex;
}

if (ex instanceof ErrorResponseException) {
Expand Down
11 changes: 7 additions & 4 deletions api/src/main/java/io/minio/SelectResponseStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.io.ByteStreams;
import io.minio.errors.InternalException;
import io.minio.errors.MinioException;
import io.minio.messages.Progress;
import io.minio.messages.Stats;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
Expand Down Expand Up @@ -141,15 +142,17 @@ private boolean populate()
new ByteArrayInputStream(data, headerLength, payloadLength);

if (headerMap.get(":event-type").equals("Progress")) {
Stats stats = new Stats("Progress");
stats.parseXml(new InputStreamReader(payloadStream, StandardCharsets.UTF_8));
Stats stats =
(Stats)
Xml.unmarshal(
Progress.class, new InputStreamReader(payloadStream, StandardCharsets.UTF_8));
this.stats = stats;
return false;
}

if (headerMap.get(":event-type").equals("Stats")) {
Stats stats = new Stats("Stats");
stats.parseXml(new InputStreamReader(payloadStream, StandardCharsets.UTF_8));
Stats stats =
Xml.unmarshal(Stats.class, new InputStreamReader(payloadStream, StandardCharsets.UTF_8));
this.stats = stats;
return false;
}
Expand Down
82 changes: 82 additions & 0 deletions api/src/main/java/io/minio/Xml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2020 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;

import io.minio.errors.XmlParserException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;

public class Xml {
/**
* This marshal method will traverse the provided object checking for field annotations in order
* to compose the XML data.
*/
public static String marshal(Object source) throws XmlParserException {
try {
Serializer serializer = new Persister(new AnnotationStrategy());
StringWriter writer = new StringWriter();
serializer.write(source, writer);
return writer.toString();
} catch (Exception e) {
throw new XmlParserException(e);
}
}

/**
* This unmarshal method will read the contents of the XML document from the provided source and
* populate the object with the values deserialized.
*/
public static <T> T unmarshal(Class<? extends T> type, Reader source) throws XmlParserException {
try {
Serializer serializer = new Persister(new AnnotationStrategy());
return serializer.read(type, source);
} catch (Exception e) {
throw new XmlParserException(e);
}
}

/**
* This unmarshal method will read the contents of the XML document from the provided source and
* populate the object with the values deserialized.
*/
public static <T> T unmarshal(Class<? extends T> type, String source) throws XmlParserException {
try {
Serializer serializer = new Persister(new AnnotationStrategy());
return serializer.read(type, new StringReader(source));
} catch (Exception e) {
throw new XmlParserException(e);
}
}

/**
* This validate method will validate the contents of the XML document against the specified XML
* class schema.
*/
public static boolean validate(Class type, String source) throws XmlParserException {
try {
Serializer serializer = new Persister(new AnnotationStrategy());
return serializer.validate(type, source);
} catch (Exception e) {
throw new XmlParserException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ErrorResponse errorResponse() {
public String toString() {
Request request = response.request();
return "error occurred\n"
+ errorResponse.getString()
+ errorResponse.toString()
+ "\n"
+ "request={"
+ "method="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,18 @@
* limitations under the License.
*/

package io.minio.messages;
package io.minio.errors;

import com.google.api.client.util.Key;
import java.util.LinkedList;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;
public class XmlParserException extends MinioException {
Exception exception;

/** Helper class to parse Amazon AWS S3 response XML containing list of bucket information. */
@SuppressWarnings("WeakerAccess")
public class Buckets extends XmlEntity {
@Key("Bucket")
private List<Bucket> bucketList = new LinkedList<>();

public Buckets() throws XmlPullParserException {
public XmlParserException(Exception exception) {
super();
super.name = "Buckets";
this.exception = exception;
}

/** Returns List of Buckets. */
public List<Bucket> bucketList() {
return bucketList;
@Override
public String toString() {
return exception.toString();
}
}
24 changes: 10 additions & 14 deletions api/src/main/java/io/minio/messages/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@

package io.minio.messages;

import com.google.api.client.util.Key;
import io.minio.Time;
import java.time.ZonedDateTime;
import org.xmlpull.v1.XmlPullParserException;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/** Helper class to parse Amazon AWS S3 response XML containing bucket information. */
@SuppressWarnings("SameParameterValue")
public class Bucket extends XmlEntity {
@Key("Name")
/** Helper class to denote bucket information for ListAllMyBucketsResult. */
@Root(name = "Bucket", strict = false)
public class Bucket {
@Element(name = "Name")
private String name;

@Key("CreationDate")
private String creationDate;
@Element(name = "CreationDate")
private ResponseDate creationDate;

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

/** Returns bucket name. */
public String name() {
Expand All @@ -42,6 +38,6 @@ public String name() {

/** Returns creation date. */
public ZonedDateTime creationDate() {
return ZonedDateTime.parse(creationDate, Time.RESPONSE_DATE_FORMAT);
return creationDate.zonedDateTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,17 @@

package io.minio.messages;

import com.google.api.client.util.Key;
import java.util.LinkedList;
import java.util.List;
import org.xmlpull.v1.XmlPullParserException;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/** 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")
/** Helper class to denote CloudFunction configuration of NotificationConfiguration. */
@Root(name = "CloudFunctionConfiguration", strict = false)
public class CloudFunctionConfiguration extends NotificationCommonConfiguration {
@Element(name = "CloudFunction")
private String cloudFunction;

@Key("Event")
private List<String> events = new LinkedList<>();

@Key("Filter")
private Filter filter;

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

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

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

/** Returns cloudFunction. */
Expand All @@ -59,24 +38,4 @@ public String cloudFunction() {
public void setCloudFunction(String cloudFunction) {
this.cloudFunction = cloudFunction;
}

/** Returns events. */
public List<EventType> events() throws IllegalArgumentException {
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;
}
}
Loading

0 comments on commit d972526

Please sign in to comment.