use of org.exist.xquery.XPathException in project exist by eXist-db.
the class SystemExport method exportDocument.
private void exportDocument(final BackupHandler bh, final BackupWriter output, final Date date, final BackupDescriptor prevBackup, final SAXSerializer serializer, final int docsCount, final int count, final DocumentImpl doc) throws IOException, SAXException, TerminatedException {
if (callback != null) {
callback.startDocument(doc.getFileURI().toString(), count, docsCount);
}
if ((monitor != null) && !monitor.proceed()) {
throw (new TerminatedException("system export terminated by db"));
}
final boolean needsBackup = (prevBackup == null) || (date.getTime() < doc.getLastModified());
if (needsBackup) {
// Note: do not auto-close the output stream or the zip will be closed!
try {
final OutputStream os = output.newEntry(Backup.encode(URIUtils.urlDecodeUtf8(doc.getFileURI())));
if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
broker.readBinaryResource((BinaryDocument) doc, os);
} else {
final SAXSerializer contentSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
final Writer writer = new BufferedWriter(new OutputStreamWriter(os, UTF_8));
try {
// write resource to contentSerializer
contentSerializer.setOutput(writer, defaultOutputProperties);
final Receiver receiver;
if (chainFactory != null) {
chainFactory.getLast().setNextInChain(contentSerializer);
receiver = chainFactory.getFirst();
} else {
receiver = contentSerializer;
}
writeXML(doc, receiver);
} finally {
SerializerPool.getInstance().returnObject(contentSerializer);
writer.flush();
}
}
} catch (final Exception e) {
reportError("A write error occurred while exporting document: '" + doc.getFileURI() + "'. Continuing with next document.", e);
return;
} finally {
output.closeEntry();
}
}
final Permission perms = doc.getPermissions();
// store permissions
final AttributesImpl attr = new AttributesImpl();
attr.addAttribute(Namespaces.EXIST_NS, "type", "type", "CDATA", (doc.getResourceType() == DocumentImpl.BINARY_FILE) ? "BinaryResource" : "XMLResource");
attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", doc.getFileURI().toString());
attr.addAttribute(Namespaces.EXIST_NS, "skip", "skip", "CDATA", (needsBackup ? "no" : "yes"));
Backup.writeUnixStylePermissionAttributes(attr, perms);
try {
final String created = new DateTimeValue(new Date(doc.getCreated())).getStringValue();
final String modified = new DateTimeValue(new Date(doc.getLastModified())).getStringValue();
attr.addAttribute(Namespaces.EXIST_NS, "created", "created", "CDATA", created);
attr.addAttribute(Namespaces.EXIST_NS, "modified", "modified", "CDATA", modified);
} catch (final XPathException e) {
LOG.warn(e.getMessage(), e);
}
attr.addAttribute(Namespaces.EXIST_NS, "filename", "filename", "CDATA", Backup.encode(URIUtils.urlDecodeUtf8(doc.getFileURI())));
String mimeType = "application/xml";
if (doc.getMimeType() != null) {
mimeType = Backup.encode(doc.getMimeType());
}
attr.addAttribute(Namespaces.EXIST_NS, "mimetype", "mimetype", "CDATA", mimeType);
// output by serializer
// if( ( doc.getResourceType() == DocumentImpl.XML_FILE ) && ( metadata != null ) && ( doc.getDoctype() != null ) ) {
//
// if( doc.getDoctype().getName() != null ) {
// attr.addAttribute( Namespaces.EXIST_NS, "namedoctype", "namedoctype", "CDATA", doc.getDoctype().getName() );
// }
//
// if( doc.getDoctype().getPublicId() != null ) {
// attr.addAttribute( Namespaces.EXIST_NS, "publicid", "publicid", "CDATA", doc.getDoctype().getPublicId() );
// }
//
// if( doc.getDoctype().getSystemId() != null ) {
// attr.addAttribute( Namespaces.EXIST_NS, "systemid", "systemid", "CDATA", doc.getDoctype().getSystemId() );
// }
// }
bh.backup(doc, attr);
serializer.startElement(Namespaces.EXIST_NS, "resource", "resource", attr);
if (perms instanceof ACLPermission) {
Backup.writeACLPermission(serializer, (ACLPermission) perms);
}
bh.backup(doc, serializer);
serializer.endElement(Namespaces.EXIST_NS, "resource", "resource");
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class SystemExport method export.
/**
* Export a collection. Write out the collection metadata and save the resources stored in the collection.
*
* @param current the collection
* @param output the output writer
* @param date
* @param prevBackup DOCUMENT ME!
* @param errorList a list of {@link org.exist.backup.ErrorReport} objects as returned by methods in {@link org.exist.backup.ConsistencyCheck}
* @param docs a document set to keep track of all written documents.
* @throws IOException
* @throws SAXException
* @throws TerminatedException DOCUMENT ME!
*/
private void export(final BackupHandler bh, final Collection current, final BackupWriter output, final Date date, final BackupDescriptor prevBackup, final List<ErrorReport> errorList, final MutableDocumentSet docs) throws IOException, SAXException, TerminatedException, PermissionDeniedException {
if ((monitor != null) && !monitor.proceed()) {
throw (new TerminatedException("system export terminated by db"));
}
// if( !current.getURI().equalsInternal( XmldbURI.ROOT_COLLECTION_URI ) ) {
output.newCollection(Backup.encode(URIUtils.urlDecodeUtf8(current.getURI())));
// }
final SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
try {
final Writer contents = output.newContents();
// serializer writes to __contents__.xml
serializer.setOutput(contents, contentsOutputProps);
final Permission perm = current.getPermissionsNoLock();
serializer.startDocument();
serializer.startPrefixMapping("", Namespaces.EXIST_NS);
final XmldbURI uri = current.getURI();
final AttributesImpl attr = new AttributesImpl();
attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", uri.toString());
attr.addAttribute(Namespaces.EXIST_NS, "version", "version", "CDATA", String.valueOf(currVersion));
Backup.writeUnixStylePermissionAttributes(attr, perm);
try {
attr.addAttribute(Namespaces.EXIST_NS, "created", "created", "CDATA", new DateTimeValue(new Date(current.getCreated())).getStringValue());
} catch (final XPathException e) {
e.printStackTrace();
}
bh.backup(current, attr);
serializer.startElement(Namespaces.EXIST_NS, "collection", "collection", attr);
if (perm instanceof ACLPermission) {
Backup.writeACLPermission(serializer, (ACLPermission) perm);
}
bh.backup(current, serializer);
final int docsCount = current.getDocumentCountNoLock(broker);
int count = 0;
for (final Iterator<DocumentImpl> i = current.iteratorNoLock(broker); i.hasNext(); count++) {
final DocumentImpl doc = i.next();
if (isDamaged(doc, errorList)) {
reportError("Skipping damaged document " + doc.getFileURI(), null);
continue;
}
if (doc.getFileURI().equalsInternal(CONTENTS_URI) || doc.getFileURI().equalsInternal(LOST_URI)) {
// skip __contents__.xml documents
continue;
}
exportDocument(bh, output, date, prevBackup, serializer, docsCount, count, doc);
docs.add(doc, false);
}
for (final Iterator<XmldbURI> i = current.collectionIteratorNoLock(broker); i.hasNext(); ) {
final XmldbURI childUri = i.next();
if (childUri.equalsInternal(TEMP_COLLECTION)) {
continue;
}
if (isDamagedChild(childUri, errorList)) {
reportError("Skipping damaged child collection " + childUri, null);
continue;
}
attr.clear();
attr.addAttribute(Namespaces.EXIST_NS, "name", "name", "CDATA", childUri.toString());
attr.addAttribute(Namespaces.EXIST_NS, "filename", "filename", "CDATA", Backup.encode(URIUtils.urlDecodeUtf8(childUri.toString())));
serializer.startElement(Namespaces.EXIST_NS, "subcollection", "subcollection", attr);
serializer.endElement(Namespaces.EXIST_NS, "subcollection", "subcollection");
}
if (prevBackup != null) {
// Check which collections and resources have been deleted since
// the
// last backup
final CheckDeletedHandler check = new CheckDeletedHandler(current, serializer);
try {
prevBackup.parse(broker.getBrokerPool().getParserPool(), check);
} catch (final Exception e) {
LOG.error("Caught exception while trying to parse previous backup descriptor: {}", prevBackup.getSymbolicPath(), e);
}
}
// close <collection>
serializer.endElement(Namespaces.EXIST_NS, "collection", "collection");
serializer.endPrefixMapping("");
serializer.endDocument();
output.closeContents();
} finally {
SerializerPool.getInstance().returnObject(serializer);
// if( !current.getURI().equalsInternal( XmldbURI.ROOT_COLLECTION_URI ) ) {
output.closeCollection();
// }
}
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class SystemImportHandler method restoreResourceEntry.
private DeferredPermission restoreResourceEntry(final Attributes atts) throws SAXException {
@Nullable final String skip = atts.getValue("skip");
// Don't process entries which should be skipped
if (skip != null && !"no".equals(skip)) {
return new SkippedEntryDeferredPermission();
}
@Nullable final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("Resource requires a name attribute");
}
final boolean xmlType = Optional.ofNullable(atts.getValue("type")).filter(s -> s.equals("XMLResource")).isPresent();
final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
final String perms = getAttr(atts, "mode", "644");
final String filename = getAttr(atts, "filename", name);
@Nullable final String mimeTypeStr = atts.getValue("mimetype");
@Nullable final String dateCreatedStr = atts.getValue("created");
@Nullable final String dateModifiedStr = atts.getValue("modified");
@Nullable final String publicId = atts.getValue("publicid");
@Nullable final String systemId = atts.getValue("systemid");
@Nullable final String nameDocType = atts.getValue("namedoctype");
MimeType mimeType = null;
if (mimeTypeStr != null) {
mimeType = MimeTable.getInstance().getContentType(mimeTypeStr);
}
if (mimeType == null) {
mimeType = xmlType ? MimeType.XML_TYPE : MimeType.BINARY_TYPE;
}
Date dateCreated = null;
if (dateCreatedStr != null) {
try {
dateCreated = new DateTimeValue(dateCreatedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal creation date. Ignoring date...");
}
}
Date dateModified = null;
if (dateModifiedStr != null) {
try {
dateModified = new DateTimeValue(dateModifiedStr).getDate();
} catch (final XPathException xpe) {
listener.warn("Illegal modification date. Ignoring date...");
}
}
final DocumentType docType;
if (publicId != null || systemId != null) {
docType = new DocumentTypeImpl(nameDocType, publicId, systemId);
} else {
docType = null;
}
final XmldbURI docUri;
if (version >= STRICT_URI_VERSION) {
docUri = XmldbURI.create(name);
} else {
try {
docUri = URIUtils.encodeXmldbUriFor(name);
} catch (final URISyntaxException e) {
final String msg = "Could not parse document name into a URI: " + e.getMessage();
listener.error(msg);
LOG.error(msg, e);
return new SkippedEntryDeferredPermission();
}
}
try (final EXistInputSource is = descriptor.getInputSource(filename)) {
if (is == null) {
final String msg = "Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: Unable to obtain its EXistInputSource";
listener.warn(msg);
throw new RuntimeException(msg);
}
try (final Txn transaction = beginTransaction()) {
broker.storeDocument(transaction, docUri, is, mimeType, dateCreated, dateModified, null, docType, null, currentCollection);
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.startDocumentRestore(doc.getDocument(), atts);
}
transaction.commit();
final DeferredPermission deferredPermission;
if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
// prevents restore of a backup from changing system collection resource ownership
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(perms, 8));
} else {
deferredPermission = new ResourceDeferredPermission(listener, currentCollection.getURI().append(name), owner, group, Integer.parseInt(perms, 8));
}
try (final LockedDocument doc = currentCollection.getDocumentWithLock(broker, docUri, Lock.LockMode.READ_LOCK)) {
rh.endDocumentRestore(doc.getDocument());
}
listener.restoredResource(name);
return deferredPermission;
} catch (final Exception e) {
throw new IOException(e);
}
} catch (final Exception e) {
listener.warn("Failed to restore resource '" + name + "'\nfrom file '" + descriptor.getSymbolicPath(name, false) + "'.\nReason: " + e.getMessage());
LOG.error(e.getMessage(), e);
return new SkippedEntryDeferredPermission();
}
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class Launcher method checkInstalledApps.
private void checkInstalledApps() {
try {
final BrokerPool pool = BrokerPool.getInstance();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence pkgs = xquery.execute(broker, "repo:list()", null);
for (final SequenceIterator i = pkgs.iterate(); i.hasNext(); ) {
final ExistRepository.Notification notification = new ExistRepository.Notification(ExistRepository.Action.INSTALL, i.nextItem().getStringValue());
final Optional<ExistRepository> expathRepo = pool.getExpathRepo();
if (expathRepo.isPresent()) {
update(expathRepo.get(), notification);
utilityPanel.update(expathRepo.get(), notification);
}
expathRepo.orElseThrow(() -> new EXistException("EXPath repository is not available."));
}
}
} catch (final EXistException | XPathException | PermissionDeniedException e) {
System.err.println("Failed to check installed packages: " + e.getMessage());
e.printStackTrace();
}
}
use of org.exist.xquery.XPathException in project exist by eXist-db.
the class ValueIndexFactory method deserialize.
public static final Indexable deserialize(final byte[] data, final int start, final int len) throws EXistException {
final int type = data[start];
/* xs:string */
if (Type.subTypeOf(type, Type.STRING)) {
final String s = new String(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE), len - (ValueIndexFactory.LENGTH_VALUE_TYPE), UTF_8);
return new StringValue(s);
} else /* xs:dateTime */
if (Type.subTypeOf(type, Type.DATE_TIME)) {
try {
final XMLGregorianCalendar xmlutccal = DatatypeFactory.newInstance().newXMLGregorianCalendar(ByteConversion.byteToIntH(data, start + 1), data[start + 5], data[start + 6], data[start + 7], data[start + 8], data[start + 9], ByteConversion.byteToShortH(data, start + 10), 0);
return new DateTimeValue(xmlutccal);
} catch (final DatatypeConfigurationException dtce) {
throw new EXistException("Could not deserialize xs:dateTime data type" + "for range index key: " + Type.getTypeName(type) + " - " + dtce.getMessage());
}
} else /* xs:date */
if (Type.subTypeOf(type, Type.DATE)) {
try {
final XMLGregorianCalendar xmlutccal = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(ByteConversion.byteToIntH(data, start + 1), data[start + 5], data[start + 6], 0);
return new DateValue(xmlutccal);
} catch (final DatatypeConfigurationException | XPathException dtce) {
throw new EXistException("Could not deserialize xs:date data type" + " for range index key: " + Type.getTypeName(type) + " - " + dtce.getMessage());
}
} else /* xs:integer */
if (Type.subTypeOf(type, Type.INTEGER)) {
return new IntegerValue(ByteConversion.byteToLong(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x8000000000000000L);
} else /* xs:double */
if (type == Type.DOUBLE) {
final long bits = ByteConversion.byteToLong(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x8000000000000000L;
final double d = Double.longBitsToDouble(bits);
return new DoubleValue(d);
} else /* xs:float */
if (type == Type.FLOAT) {
final int bits = ByteConversion.byteToInt(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE)) ^ 0x80000000;
final float f = Float.intBitsToFloat(bits);
return new FloatValue(f);
} else /* xs:decimal */
if (type == Type.DECIMAL) {
// actually loaded from string data due to the uncertain length
final String s = new String(data, start + (ValueIndexFactory.LENGTH_VALUE_TYPE), len - (ValueIndexFactory.LENGTH_VALUE_TYPE), UTF_8);
return new DecimalValue(new BigDecimal(s));
} else /* xs:boolean */
if (type == Type.BOOLEAN) {
return new BooleanValue(data[start + (ValueIndexFactory.LENGTH_VALUE_TYPE)] == 1);
} else /* unknown! */
{
throw new EXistException("Unknown data type for deserialization: " + Type.getTypeName(type));
}
}
Aggregations