Skip to content

Commit

Permalink
move xml parsing using SimpleXML
Browse files Browse the repository at this point in the history
This breaks backward compatibility
  • Loading branch information
balamurugana committed Mar 14, 2020
1 parent 24d788d commit 345dbfe
Show file tree
Hide file tree
Showing 75 changed files with 1,459 additions and 1,862 deletions.
29 changes: 23 additions & 6 deletions api/src/main/java/io/minio/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@

package io.minio;

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


/**
* 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 @@ -143,21 +151,30 @@ public String message() {
return this.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());
}
}
}
619 changes: 275 additions & 344 deletions api/src/main/java/io/minio/MinioClient.java

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions api/src/main/java/io/minio/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import org.xmlpull.v1.XmlPullParserException;

import io.minio.errors.ErrorResponseException;
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;


/**
Expand All @@ -38,8 +36,14 @@ public class Result<T> {
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;
}

Expand All @@ -50,7 +54,7 @@ public Result(T type, Exception ex) {
public T get()
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException,
JsonParseException, JsonMappingException,IOException,
InvalidKeyException, XmlPullParserException, ErrorResponseException,
InvalidKeyException, XmlParserException, ErrorResponseException,
InternalException {
if (ex == null) {
return type;
Expand All @@ -72,8 +76,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
7 changes: 3 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 io.minio.errors.InternalException;
import io.minio.errors.MinioException;
import io.minio.messages.Stats;
import io.minio.messages.Progress;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
Expand Down Expand Up @@ -140,15 +141,13 @@ private boolean populate()
ByteArrayInputStream payloadStream = 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
86 changes: 86 additions & 0 deletions api/src/main/java/io/minio/Xml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.Serializer;


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 @@ -55,7 +55,7 @@ public ErrorResponse errorResponse() {
public String toString() {
Request request = response.request();
return "error occurred\n"
+ errorResponse.getString() + "\n"
+ errorResponse.toString() + "\n"
+ "request={"
+ "method=" + request.method() + ", "
+ "url=" + request.url() + ", "
Expand Down
34 changes: 34 additions & 0 deletions api/src/main/java/io/minio/errors/XmlParserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2015 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.errors;


public class XmlParserException extends MinioException {
Exception exception;


public XmlParserException(Exception exception) {
super();
this.exception = exception;
}


@Override
public String toString() {
return exception.toString();
}
}
23 changes: 10 additions & 13 deletions api/src/main/java/io/minio/messages/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@

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.
* Helper class to denote bucket information for ListAllMyBucketsResult.
*/
@SuppressWarnings("SameParameterValue")
public class Bucket extends XmlEntity {
@Key("Name")
@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() {
}


Expand All @@ -51,6 +48,6 @@ public String name() {
* Returns creation date.
*/
public ZonedDateTime creationDate() {
return ZonedDateTime.parse(creationDate, Time.RESPONSE_DATE_FORMAT);
return creationDate.zonedDateTime();
}
}
Loading

0 comments on commit 345dbfe

Please sign in to comment.