use of org.exist.dom.memtree.DocumentImpl in project exist by eXist-db.
the class ArrayListValueSequence method expand.
/**
* Scan the sequence and check all in-memory documents.
* They may contains references to nodes stored in the database.
* Expand those references to get a pure in-memory DOM tree.
*/
private void expand() {
final Set<DocumentImpl> docs = new HashSet<>();
for (final Item value : values) {
final NodeImpl node = (NodeImpl) value;
final DocumentImpl ownerDoc = node.getNodeType() == Node.DOCUMENT_NODE ? (DocumentImpl) node : node.getOwnerDocument();
if (ownerDoc.hasReferenceNodes()) {
docs.add(ownerDoc);
}
}
for (final DocumentImpl doc : docs) {
doc.expand();
}
}
use of org.exist.dom.memtree.DocumentImpl in project exist by eXist-db.
the class RestXqServiceImpl method extractRequestBody.
@Override
protected Sequence extractRequestBody(final HttpRequest request) throws RestXqServiceException {
// TODO don't use close shield input stream and move parsing of form parameters from HttpServletRequestAdapter into RequestBodyParser
InputStream is;
FilterInputStreamCache cache = null;
try {
// first, get the content of the request
is = new CloseShieldInputStream(request.getInputStream());
if (is.available() <= 0) {
return null;
}
// if marking is not supported, we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
if (!is.markSupported()) {
cache = FilterInputStreamCacheFactory.getCacheInstance(() -> {
final Configuration configuration = getBrokerPool().getConfiguration();
return (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
}, is);
is = new CachingFilterInputStream(cache);
}
is.mark(Integer.MAX_VALUE);
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
Sequence result = null;
try {
// was there any POST content?
if (is != null && is.available() > 0) {
String contentType = request.getContentType();
// 1) determine if exists mime database considers this binary data
if (contentType != null) {
// strip off any charset encoding info
if (contentType.contains(";")) {
contentType = contentType.substring(0, contentType.indexOf(";"));
}
MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
if (mimeType != null && !mimeType.isXMLType()) {
// binary data
try {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
if (binaryValue != null) {
result = new SequenceImpl<>(new BinaryTypedValue(binaryValue));
}
} catch (final XPathException xpe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, xpe);
}
}
}
if (result == null) {
// 2) not binary, try and parse as an XML document
final DocumentImpl doc = parseAsXml(is);
if (doc != null) {
result = new SequenceImpl<>(new DocumentTypedValue(doc));
}
}
if (result == null) {
String encoding = request.getCharacterEncoding();
// 3) not a valid XML document, return a string representation of the document
if (encoding == null) {
encoding = "UTF-8";
}
try {
// reset the stream, as we need to reuse for string parsing
is.reset();
final StringValue str = parseAsString(is, encoding);
if (str != null) {
result = new SequenceImpl<>(new StringTypedValue(str));
}
} catch (final IOException ioe) {
throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
}
}
}
} catch (IOException e) {
throw new RestXqServiceException(e.getMessage());
} finally {
if (cache != null) {
try {
cache.invalidate();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
if (is != null) {
/*
* Do NOT close the stream if its a binary value,
* because we will need it later for serialization
*/
boolean isBinaryType = false;
if (result != null) {
try {
final Type type = result.head().getType();
isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
} catch (final IndexOutOfBoundsException ioe) {
LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
}
}
if (!isBinaryType) {
try {
is.close();
} catch (final IOException ioe) {
LOG.error(ioe.getMessage(), ioe);
}
}
}
}
if (result != null) {
return result;
} else {
return Sequence.EMPTY_SEQUENCE;
}
}
use of org.exist.dom.memtree.DocumentImpl in project exist by eXist-db.
the class RestXqServiceImpl method parseAsXml.
private DocumentImpl parseAsXml(final InputStream is) {
DocumentImpl result = null;
XMLReader reader = null;
try {
// try and construct xml document from input stream, we use eXist's in-memory DOM implementation
// we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread
final InputSource src = new InputSource(new CloseShieldInputStream(is));
reader = getBrokerPool().getParserPool().borrowXMLReader();
final MemTreeBuilder builder = new MemTreeBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
reader.setContentHandler(receiver);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
reader.parse(src);
builder.endDocument();
final Document doc = receiver.getDocument();
result = (DocumentImpl) doc;
} catch (final SAXException | IOException saxe) {
// do nothing, we will default to trying to return a string below
} finally {
if (reader != null) {
getBrokerPool().getParserPool().returnXMLReader(reader);
}
}
return result;
}
Aggregations