use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class Source method exec.
@Override
public void exec() {
if (fileURI == null) {
return;
}
InputStream is = null;
try {
if (fileURI.toLowerCase().startsWith("dbgp://")) {
String uri = fileURI.substring(7);
if (uri.toLowerCase().startsWith("file:/")) {
uri = fileURI.substring(5);
is = Files.newInputStream(Paths.get(uri));
} else {
XmldbURI pathUri = XmldbURI.create(URLDecoder.decode(fileURI.substring(15), "UTF-8"));
Database db = getJoint().getContext().getDatabase();
try (final DBBroker broker = db.getBroker();
final LockedDocument resource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
if (resource.getDocument().getResourceType() == DocumentImpl.BINARY_FILE) {
is = broker.getBinaryResource((BinaryDocument) resource.getDocument());
} else {
// TODO: xml source???
return;
}
} catch (EXistException e) {
exception = e;
}
}
} else {
URL url = new URL(fileURI);
URLConnection conn = url.openConnection();
is = conn.getInputStream();
}
UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
byte[] buf = new byte[256];
int c;
while ((c = is.read(buf)) > -1) {
// TODO: begin & end line should affect
baos.write(buf, 0, c);
}
source = baos.toByteArray();
success = true;
} catch (PermissionDeniedException | IOException e) {
exception = e;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
if (exception == null) {
exception = e;
}
}
}
}
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class RestoreHandler method restoreDeletedEntry.
private void restoreDeletedEntry(final Attributes atts) {
final String name = atts.getValue("name");
final String type = atts.getValue("type");
if ("collection".equals(type)) {
try {
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK)) {
if (collection != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
broker.removeCollection(transaction, collection);
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
}
} catch (final PermissionDeniedException | IOException | TriggerException | TransactionException e) {
listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
}
} else if ("resource".equals(type)) {
final XmldbURI docName = XmldbURI.create(name);
try (final Txn transaction = beginTransaction();
final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK);
final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, docName, Lock.LockMode.WRITE_LOCK)) {
// Check that the document exists
if (lockedDocument != null) {
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
broker.setTriggersEnabled(false);
final boolean xmlType = !(lockedDocument.getDocument() instanceof BinaryDocument);
if (xmlType) {
collection.removeXMLResource(transaction, broker, docName);
} else {
collection.removeBinaryResource(transaction, broker, docName);
}
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
transaction.commit();
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
} catch (final PermissionDeniedException | TransactionException | TriggerException | LockException | IOException e) {
listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
}
}
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class XIncludeFilter method processXInclude.
/**
* @param href The resource to be xincluded
* @param xpointer The xpointer
* @return Optionally a ResourceError if it was not possible to retrieve the resource
* to be xincluded
* @throws SAXException If a SAX processing error occurs
*/
protected Optional<ResourceError> processXInclude(final String href, String xpointer) throws SAXException {
if (href == null) {
throw new SAXException("No href attribute found in XInclude include element");
}
// save some settings
DocumentImpl prevDoc = document;
boolean createContainerElements = serializer.createContainerElements;
serializer.createContainerElements = false;
// The following comments are the basis for possible external documents
XmldbURI docUri = null;
try {
docUri = XmldbURI.xmldbUriFor(href);
/*
if(!stylesheetUri.toCollectionPathURI().equals(stylesheetUri)) {
externalUri = stylesheetUri.getXmldbURI();
}
*/
} catch (final URISyntaxException e) {
// could be an external URI!
}
// parse the href attribute
LOG.debug("found href=\"{}\"", href);
// String xpointer = null;
// String docName = href;
Map<String, String> params = null;
DocumentImpl doc = null;
org.exist.dom.memtree.DocumentImpl memtreeDoc = null;
boolean xqueryDoc = false;
if (docUri != null) {
final String fragment = docUri.getFragment();
if (!(fragment == null || fragment.length() == 0)) {
throw new SAXException("Fragment identifiers must not be used in an xinclude href attribute. To specify an xpointer, use the xpointer attribute.");
}
// extract possible parameters in the URI
params = null;
final String paramStr = docUri.getQuery();
if (paramStr != null) {
params = processParameters(paramStr);
// strip query part
docUri = XmldbURI.create(docUri.getRawCollectionPath());
}
// Patch 1520454 start
if (!docUri.isAbsolute() && document != null) {
final String base = document.getCollection().getURI() + "/";
final String child = "./" + docUri.toString();
final URI baseUri = URI.create(base);
final URI childUri = URI.create(child);
final URI uri = baseUri.resolve(childUri);
docUri = XmldbURI.create(uri);
}
// retrieve the document
try {
doc = serializer.broker.getResource(docUri, Permission.READ);
} catch (final PermissionDeniedException e) {
return Optional.of(new ResourceError("Permission denied to read XInclude'd resource", e));
}
/* Check if the document is a stored XQuery */
if (doc != null && doc.getResourceType() == DocumentImpl.BINARY_FILE) {
xqueryDoc = "application/xquery".equals(doc.getMimeType());
}
}
// The document could not be found: check if it points to an external resource
if (docUri == null || (doc == null && !docUri.isAbsolute())) {
try {
URI externalUri = new URI(href);
final String scheme = externalUri.getScheme();
// XQuery context.
if (scheme == null && moduleLoadPath != null) {
final String path = externalUri.getSchemeSpecificPart();
Path f = Paths.get(path);
if (!f.isAbsolute()) {
if (moduleLoadPath.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
final XmldbURI parentUri = XmldbURI.create(moduleLoadPath);
docUri = parentUri.append(path);
doc = (DocumentImpl) serializer.broker.getXMLResource(docUri);
if (doc != null && !doc.getPermissions().validate(serializer.broker.getCurrentSubject(), Permission.READ)) {
throw new PermissionDeniedException("Permission denied to read XInclude'd resource");
}
} else {
f = Paths.get(moduleLoadPath, path);
externalUri = f.toUri();
}
}
}
if (doc == null) {
final Either<ResourceError, org.exist.dom.memtree.DocumentImpl> external = parseExternal(externalUri);
if (external.isLeft()) {
return Optional.of(external.left().get());
} else {
memtreeDoc = external.right().get();
}
}
} catch (final PermissionDeniedException e) {
return Optional.of(new ResourceError("Permission denied on XInclude'd resource", e));
} catch (final ParserConfigurationException | URISyntaxException e) {
throw new SAXException("XInclude: failed to parse document at URI: " + href + ": " + e.getMessage(), e);
}
}
/* if document has not been found and xpointer is
* null, throw an exception. If xpointer != null
* we retry below and interpret docName as
* a collection.
*/
if (doc == null && memtreeDoc == null && xpointer == null) {
return Optional.of(new ResourceError("document " + docUri + " not found"));
}
if (xpointer == null && !xqueryDoc) {
// no xpointer found - just serialize the doc
if (memtreeDoc == null) {
serializer.serializeToReceiver(doc, false);
} else {
serializer.serializeToReceiver(memtreeDoc, false);
}
} else {
// process the xpointer or the stored XQuery
Source source = null;
final XQueryPool pool = serializer.broker.getBrokerPool().getXQueryPool();
CompiledXQuery compiled = null;
try {
if (xpointer == null) {
source = new DBSource(serializer.broker, (BinaryDocument) doc, true);
} else {
xpointer = checkNamespaces(xpointer);
source = new StringSource(xpointer);
}
final XQuery xquery = serializer.broker.getBrokerPool().getXQueryService();
XQueryContext context;
compiled = pool.borrowCompiledXQuery(serializer.broker, source);
if (compiled == null) {
context = new XQueryContext(serializer.broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.declareNamespaces(namespaces);
context.declareNamespace("xinclude", Namespaces.XINCLUDE_NS);
// setup the http context if known
if (serializer.httpContext != null) {
context.setHttpContext(serializer.httpContext);
}
// TODO: change these to putting the XmldbURI in, but we need to warn users!
if (document != null) {
context.declareVariable("xinclude:current-doc", document.getFileURI().toString());
context.declareVariable("xinclude:current-collection", document.getCollection().getURI().toString());
}
if (xpointer != null) {
if (doc != null) {
context.setStaticallyKnownDocuments(new XmldbURI[] { doc.getURI() });
} else if (docUri != null) {
context.setStaticallyKnownDocuments(new XmldbURI[] { docUri });
}
}
// pass parameters as variables
if (params != null) {
for (final Map.Entry<String, String> entry : params.entrySet()) {
context.declareVariable(entry.getKey(), entry.getValue());
}
}
if (compiled == null) {
try {
compiled = xquery.compile(context, source, xpointer != null);
} catch (final IOException e) {
throw new SAXException("I/O error while reading query for xinclude: " + e.getMessage(), e);
}
} else {
compiled.getContext().updateContext(context);
context.getWatchDog().reset();
}
LOG.info("xpointer query: {}", ExpressionDumper.dump((Expression) compiled));
Sequence contextSeq = null;
if (memtreeDoc != null) {
contextSeq = memtreeDoc;
}
try {
final Sequence seq = xquery.execute(serializer.broker, compiled, contextSeq);
if (Type.subTypeOf(seq.getItemType(), Type.NODE)) {
if (LOG.isDebugEnabled()) {
LOG.debug("xpointer found: {}", seq.getItemCount());
}
NodeValue node;
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
node = (NodeValue) i.nextItem();
serializer.serializeToReceiver(node, false);
}
} else {
String val;
for (int i = 0; i < seq.getItemCount(); i++) {
val = seq.itemAt(i).getStringValue();
characters(val);
}
}
} finally {
context.runCleanupTasks();
}
} catch (final XPathException | PermissionDeniedException e) {
LOG.warn("xpointer error", e);
throw new SAXException("Error while processing XInclude expression: " + e.getMessage(), e);
} finally {
if (compiled != null) {
pool.returnCompiledXQuery(source, compiled);
}
}
}
// restore settings
document = prevDoc;
serializer.createContainerElements = createContainerElements;
return Optional.empty();
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class STXTransformerTrigger method configure.
@Override
public void configure(DBBroker broker, Txn transaction, Collection parent, Map<String, List<?>> parameters) throws TriggerException {
super.configure(broker, transaction, parent, parameters);
final String stylesheet = (String) parameters.get("src").get(0);
if (stylesheet == null) {
throw new TriggerException("STXTransformerTrigger requires an attribute 'src'");
}
/*
String origProperty = System.getProperty("javax.xml.transform.TransformerFactory");
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.joost.trax.TransformerFactoryImpl");
factory = (SAXTransformerFactory)TransformerFactory.newInstance();
// reset property to previous setting
if(origProperty != null) {
System.setProperty("javax.xml.transform.TransformerFactory", origProperty);
}
*/
/*ServiceLoader<TransformerFactory> loader = ServiceLoader.load(TransformerFactory.class);
for(TransformerFactory transformerFactory : loader) {
if(transformerFactory.getClass().getName().equals("net.sf.joost.trax.TransformerFactoryImpl")) {
factory = transformerFactory.ne
}
}*/
XmldbURI stylesheetUri = null;
try {
stylesheetUri = XmldbURI.xmldbUriFor(stylesheet);
} catch (final URISyntaxException e) {
}
// TODO: allow full XmldbURIs to be used as well.
if (stylesheetUri == null || stylesheet.indexOf(':') == Constants.STRING_NOT_FOUND) {
stylesheetUri = parent.getURI().resolveCollectionPath(stylesheetUri);
DocumentImpl doc;
try {
doc = (DocumentImpl) broker.getXMLResource(stylesheetUri);
if (doc == null) {
throw new TriggerException("stylesheet " + stylesheetUri + " not found in database");
}
if (doc instanceof BinaryDocument) {
throw new TriggerException("stylesheet " + stylesheetUri + " must be stored as an xml document and not a binary document!");
}
handler = factory.newTransformerHandler(STXTemplatesCache.getInstance().getOrUpdateTemplate(broker, doc));
} catch (final TransformerConfigurationException | PermissionDeniedException | SAXException | LockException e) {
throw new TriggerException(e.getMessage(), e);
}
} else {
try {
LOG.debug("compiling stylesheet {}", stylesheet);
final Templates template = factory.newTemplates(new StreamSource(stylesheet));
handler = factory.newTransformerHandler(template);
} catch (final TransformerConfigurationException e) {
throw new TriggerException(e.getMessage(), e);
}
}
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromXmldb_noContext.
@Test
public void getSourceFromXmldb_noContext() throws IOException, PermissionDeniedException {
final String contextPath = null;
final String location = "xmldb:exist:///db/library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
Aggregations