Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -7,7 +7,6 @@
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import com.marklogic.client.io.Format;
Expand Down Expand Up @@ -49,13 +48,9 @@ private static class FailedRequestXMLParser implements FailedRequestParser {
@Override
public FailedRequest parseFailedRequest(int httpStatus, InputStream is) {
FailedRequest failure = new FailedRequest();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
builder = XmlFactories.makeNewDocumentBuilder();
Document doc = builder.parse(is);
String statusCode = null;
NodeList statusCodes = doc.getElementsByTagNameNS(ERROR_NS, "status-code");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.FactoryConfigurationError;
Expand Down Expand Up @@ -127,6 +128,13 @@ private static DocumentBuilderFactory makeNewDocumentBuilderFactory() {
return factory;
}

/**
* Creates a {@link DocumentBuilder} from a new factory configured with secure defaults.
*/
static DocumentBuilder makeNewDocumentBuilder() throws ParserConfigurationException {
return makeNewDocumentBuilderFactory().newDocumentBuilder();
}

/**
* Returns a shared {@link DocumentBuilderFactory} configured with secure defaults.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.QueryOptionsListReadHandle;
import com.marklogic.client.query.QueryOptionsListBuilder;
Expand Down Expand Up @@ -89,9 +92,13 @@ protected Class<InputStream> receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal(
XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader(
new InputStreamReader(content, StandardCharsets.UTF_8)
);
optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal(reader);
} catch (XMLStreamException e) {
logger.error("Failed to parse query options list", e);
throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall query options list",e);
throw new MarkLogicIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.TuplesBuilder;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.TuplesReadHandle;
import com.marklogic.client.query.AggregateResult;
Expand Down Expand Up @@ -92,9 +95,13 @@ protected Class<InputStream> receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal(
XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader(
new InputStreamReader(content, StandardCharsets.UTF_8)
);
tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal(reader);
} catch (XMLStreamException e) {
logger.error("Failed to parse tuples", e);
throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall tuples",e);
throw new MarkLogicIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.ValuesBuilder;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.ValuesReadHandle;
import com.marklogic.client.query.AggregateResult;
Expand Down Expand Up @@ -97,9 +100,13 @@ protected Class<InputStream> receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal(
XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader(
new InputStreamReader(content, StandardCharsets.UTF_8)
);
valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal(reader);
} catch (XMLStreamException e) {
logger.error("Failed to parse values", e);
throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall values",e);
throw new MarkLogicIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.ValuesListBuilder;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.ValuesListReadHandle;
import com.marklogic.client.query.ValuesListResults;
Expand Down Expand Up @@ -110,9 +113,13 @@ protected Class<InputStream> receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal(
XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader(
new InputStreamReader(content, StandardCharsets.UTF_8)
);
valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal(reader);
} catch (XMLStreamException e) {
logger.error("Failed to parse values list", e);
throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall values list",e);
throw new MarkLogicIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import com.marklogic.client.admin.QueryOptionsManager;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.impl.FailedRequest;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.Format;
import com.marklogic.client.io.StringHandle;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -112,4 +116,26 @@ public void testNullFailedRequestInFailedRequestException() {
assertNull(e.getServerMessage());
assertNull(e.getServerStackTrace());
}

@Test
public void testFailedRequestParsingIsIndependentOfSharedFactoryConfiguration() {
DocumentBuilderFactory sharedFactory = XmlFactories.getDocumentBuilderFactory();
boolean namespaceAware = sharedFactory.isNamespaceAware();
try {
sharedFactory.setNamespaceAware(false);
String xml = "<error xmlns=\"http://marklogic.com/xdmp/error\">" +
"<status-code>400</status-code><status>Bad Request</status>" +
"<message-code>RESTAPI-INVALIDCONTENT</message-code><message>Invalid content</message></error>";

FailedRequest failure = FailedRequest.getFailedRequest(400, "application/xml",
new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

assertEquals(400, failure.getStatusCode());
assertEquals("Bad Request", failure.getStatus());
assertEquals("RESTAPI-INVALIDCONTENT", failure.getMessageCode());
assertEquals("Invalid content", failure.getMessage());
} finally {
sharedFactory.setNamespaceAware(namespaceAware);
}
}
}
Loading