Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,7 @@
public class CompleteMultipartUploadRequestUnmarshaller
implements MessageBodyReader<CompleteMultipartUploadRequest> {

private final JAXBContext context;
private final XMLReader xmlReader;

public CompleteMultipartUploadRequestUnmarshaller() {
try {
context = JAXBContext.newInstance(CompleteMultipartUploadRequest.class);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
xmlReader = saxParserFactory.newSAXParser().getXMLReader();
} catch (Exception ex) {
throw new AssertionError("Can not instantiate " +
"CompleteMultipartUploadRequest parser", ex);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also need to change for PutBucketAclRequestUnmarshaller since it has a similar pattern (although not used as often).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure.

}
@Override
public boolean isReadable(Class<?> aClass, Type type,
Expand All @@ -70,6 +58,10 @@ public CompleteMultipartUploadRequest readFrom(
MultivaluedMap<String, String> multivaluedMap,
InputStream inputStream) throws IOException, WebApplicationException {
try {
JAXBContext context = JAXBContext.newInstance(CompleteMultipartUploadRequest.class);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
UnmarshallerHandler unmarshallerHandler =
context.createUnmarshaller().getUnmarshallerHandler();
XmlNamespaceFilter filter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,7 @@
public class MultiDeleteRequestUnmarshaller
implements MessageBodyReader<MultiDeleteRequest> {

private final JAXBContext context;
private final XMLReader xmlReader;

public MultiDeleteRequestUnmarshaller() {
try {
context = JAXBContext.newInstance(MultiDeleteRequest.class);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
xmlReader = saxParserFactory.newSAXParser().getXMLReader();
} catch (Exception ex) {
throw new AssertionError("Can't instantiate MultiDeleteRequest parser",
ex);
}
}

@Override
Expand All @@ -68,6 +56,10 @@ public MultiDeleteRequest readFrom(Class<MultiDeleteRequest> type,
Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) {
try {
JAXBContext context = JAXBContext.newInstance(MultiDeleteRequest.class);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might not need to instantiate this for every request. Could you help check whether instantiating only XMLReader for each request is enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivandika3 Thanks for the review.
Yes, this modification solves the problem as well, I've done a test, please see the new code.

XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
UnmarshallerHandler unmarshallerHandler =
context.createUnmarshaller().getUnmarshallerHandler();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Class tests Unmarshall logic of {@link CompleteMultipartUploadRequest}.
Expand Down Expand Up @@ -75,6 +79,7 @@ public void fromStreamWithoutNamespace() throws IOException {
}

private void checkContent(CompleteMultipartUploadRequest request) {
assertNotNull(request);
assertEquals(2, request.getPartList().size());

List<CompleteMultipartUploadRequest.Part> parts =
Expand All @@ -89,4 +94,43 @@ private CompleteMultipartUploadRequest unmarshall(
return new CompleteMultipartUploadRequestUnmarshaller()
.readFrom(null, null, null, null, null, inputBody);
}

@Test
public void concurrentParse() {
CompleteMultipartUploadRequestUnmarshaller unmarshaller =
new CompleteMultipartUploadRequestUnmarshaller();
byte[] bytes = ("<CompleteMultipartUpload>" + "<Part><ETag>" + part1 +
"</ETag><PartNumber>1</PartNumber" + "></Part><Part><ETag>" +
part2 + "</ETag><PartNumber>2" +
"</PartNumber></Part></CompleteMultipartUpload>").getBytes(
UTF_8);

List<CompletableFuture<CompleteMultipartUploadRequest>> futures =
new ArrayList<>();
for (int i = 0; i < 20; i++) {
futures.add(CompletableFuture.supplyAsync(() -> {
try {
//GIVEN
ByteArrayInputStream inputBody = new ByteArrayInputStream(bytes);
//WHEN
return unmarshall(unmarshaller, inputBody);
} catch (IOException e) {
return null;
}
}));
}

for (CompletableFuture<CompleteMultipartUploadRequest> future : futures) {
CompleteMultipartUploadRequest request = future.join();
//THEN
checkContent(request);
}
}

private CompleteMultipartUploadRequest unmarshall(
CompleteMultipartUploadRequestUnmarshaller unmarshaller,
ByteArrayInputStream inputBody) throws IOException {
return unmarshaller
.readFrom(null, null, null, null, null, inputBody);
}
}