Search in sources :

Example 66 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class DatabaseRepresentation method revertToRevision.

/**
 * This method reverts the latest revision data to the requested.
 *
 * @param resourceName
 *          The name of the XML resource.
 * @param backToRevision
 *          The revision value, which has to be set as the latest.
 * @throws WebApplicationException
 * @throws SirixException
 */
public void revertToRevision(final String resourceName, final int backToRevision) throws JaxRxException, SirixException {
    Database database = null;
    Session session = null;
    NodeWriteTrx wtx = null;
    boolean abort = false;
    try {
        database = Databases.openDatabase(mStoragePath);
        session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
        wtx = session.beginNodeWriteTrx();
        wtx.revertTo(backToRevision);
        wtx.commit();
    } catch (final SirixException exce) {
        abort = true;
        throw new JaxRxException(exce);
    } finally {
        WorkerHelper.closeWTX(abort, wtx, session, database);
    }
}
Also used : Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 67 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class DatabaseRepresentation method performQueryOnResource.

/**
 * This method is responsible to perform queries on a special database. (XPath
 * queries).
 *
 * @param resource
 *          The name of the database instance.
 * @param query
 *          The XPath expression.
 * @param otherParams
 *          Further query parameters (output, wrap, revision) which change the
 *          response.
 * @return The result of the XPath query expression.
 */
public StreamingOutput performQueryOnResource(final String resource, final String query, final Map<QueryParameter, String> otherParams) {
    final StreamingOutput streamingOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            final String revision = otherParams.get(QueryParameter.REVISION);
            final String wrap = otherParams.get(QueryParameter.WRAP);
            final String nodeId = otherParams.get(QueryParameter.OUTPUT);
            final boolean wrapResult = (wrap == null) ? true : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (nodeId == null) ? false : nodeId.equalsIgnoreCase(YESSTRING);
            final Integer rev = revision == null ? null : Integer.valueOf(revision);
            final RestXPathProcessor xpathProcessor = new RestXPathProcessor(mStoragePath);
            try {
                xpathProcessor.getXpathResource(resource, query, nodeid, rev, output, wrapResult);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    };
    return streamingOutput;
}
Also used : RestXPathProcessor(org.sirix.service.jaxrx.util.RestXPathProcessor) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) StreamingOutput(javax.ws.rs.core.StreamingOutput) JaxRxException(org.jaxrx.core.JaxRxException)

Example 68 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class DatabaseRepresentation method getModificHistory.

/**
 * This method reads the existing database, and offers all modifications of
 * the two given revisions
 *
 * @param resourceName
 *          The name of the existing database.
 * @param revisionRange
 *          Contains the range of revisions
 * @param nodeid
 *          To response the resource with a restid for each node (
 *          <code>true</code>) or without ( <code>false</code>).
 * @param output
 *          The OutputStream reference which have to be modified and returned
 * @param wrap
 *          <code>true</code> if the results have to be wrapped.
 *          <code>false</code> otherwise.
 * @return The {@link OutputStream} containing the result
 * @throws SirixException
 * @throws WebApplicationException
 *           The Exception occurred.
 */
public OutputStream getModificHistory(// NOPMD this method needs alls these
final String resourceName, // functions
final String revisionRange, final boolean nodeid, final OutputStream output, final boolean wrap) throws JaxRxException, SirixException {
    // extract both revision from given String value
    final StringTokenizer tokenizer = new StringTokenizer(revisionRange, "-");
    final int revision1 = Integer.valueOf(tokenizer.nextToken());
    final int revision2 = Integer.valueOf(tokenizer.nextToken());
    if (revision1 < revision2 && revision2 <= getLastRevision(resourceName)) {
        // variables for highest rest-id in respectively revision
        long maxRestidRev1 = 1;
        long maxRestidRev2 = 1;
        // Connection to sirix, creating a session
        Database database = null;
        Axis axis = null;
        NodeReadTrx rtx = null;
        Session session = null;
        // List for all restIds of modifications
        final List<Long> modificRestids = new LinkedList<Long>();
        // List of all restIds of revision 1
        final List<Long> restIdsRev1 = new LinkedList<Long>();
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
            // get highest rest-id from given revision 1
            rtx = session.beginNodeReadTrx(revision1);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                if (rtx.getNodeKey() > maxRestidRev1) {
                    maxRestidRev1 = rtx.getNodeKey();
                }
                // stores all restids from revision 1 into a list
                restIdsRev1.add(rtx.getNodeKey());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            // get highest rest-id from given revision 2
            rtx = session.beginNodeReadTrx(revision2);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                final Long nodeKey = rtx.getNodeKey();
                if (nodeKey > maxRestidRev2) {
                    maxRestidRev2 = rtx.getNodeKey();
                }
                if (nodeKey > maxRestidRev1) {
                    /*
						 * writes all restids of revision 2 higher than the highest restid
						 * of revision 1 into the list
						 */
                    modificRestids.add(nodeKey);
                }
                /*
					 * removes all restids from restIdsRev1 that appears in revision 2 all
					 * remaining restids in the list can be seen as deleted nodes
					 */
                restIdsRev1.remove(nodeKey);
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            rtx = session.beginNodeReadTrx(revision1);
            // linked list for holding unique restids from revision 1
            final List<Long> restIdsRev1New = new LinkedList<Long>();
            /*
				 * Checks if a deleted node has a parent node that was deleted too. If
				 * so, only the parent node is stored in new list to avoid double print
				 * of node modification
				 */
            for (Long nodeKey : restIdsRev1) {
                rtx.moveTo(nodeKey);
                final long parentKey = rtx.getParentKey();
                if (!restIdsRev1.contains(parentKey)) {
                    restIdsRev1New.add(nodeKey);
                }
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            if (wrap) {
                output.write(beginResult.getBytes());
            }
            /*
				 * Shred modified restids from revision 2 to xml fragment Just
				 * modifications done by post commands
				 */
            rtx = session.beginNodeReadTrx(revision2);
            for (Long nodeKey : modificRestids) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision2).call();
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            /*
				 * Shred modified restids from revision 1 to xml fragment Just
				 * modifications done by put and deletes
				 */
            rtx = session.beginNodeReadTrx(revision1);
            for (Long nodeKey : restIdsRev1New) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision1).call();
            }
            if (wrap) {
                output.write(endResult.getBytes());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
        } catch (final Exception globExcep) {
            throw new JaxRxException(globExcep);
        } finally {
            WorkerHelper.closeRTX(rtx, session, database);
        }
    } else {
        throw new JaxRxException(400, "Bad user request");
    }
    return output;
}
Also used : LinkedList(java.util.LinkedList) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) StringTokenizer(java.util.StringTokenizer) NodeReadTrx(org.sirix.api.NodeReadTrx) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 69 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class NodeIdRepresentation method serialize.

/**
 * This method serializes requested resource
 *
 * @param resource
 *          The requested resource
 * @param nodeId
 *          The node id of the requested resource.
 * @param revision
 *          The revision of the requested resource.
 * @param doNodeId
 *          Specifies whether the node id's have to be shown in the result.
 * @param output
 *          The output stream to be written.
 * @param wrapResult
 *          Specifies whether the result has to be wrapped with a result
 *          element.
 */
private void serialize(final String resource, final long nodeId, final Long revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (wrapResult) {
                output.write(BEGINRESULT);
                final XMLSerializerProperties props = new XMLSerializerProperties();
                final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
                if (doNodeId) {
                    builder.emitRESTful().emitIDs();
                }
                final XMLSerializer serializer = builder.build();
                serializer.call();
                output.write(ENDRESULT);
            } else {
                final XMLSerializerProperties props = new XMLSerializerProperties();
                final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
                if (doNodeId) {
                    builder.emitRESTful().emitIDs();
                }
                final XMLSerializer serializer = builder.build();
                serializer.call();
            }
        } catch (final SirixException ttExcep) {
            throw new JaxRxException(ttExcep);
        } catch (final IOException ioExcep) {
            throw new JaxRxException(ioExcep);
        } catch (final Exception globExcep) {
            if (globExcep instanceof JaxRxException) {
                // types
                throw (JaxRxException) globExcep;
            } else {
                throw new JaxRxException(globExcep);
            }
        } finally {
            try {
                WorkerHelper.closeRTX(null, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 70 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class XSLTEvaluator method call.

@Override
public OutputStream call() {
    final Processor proc = new Processor(false);
    final XsltCompiler comp = proc.newXsltCompiler();
    XsltExecutable exp;
    XdmNode source;
    try {
        final Configuration config = proc.getUnderlyingConfiguration();
        final NodeInfo doc = new DocumentWrapper(mSession, config);
        exp = comp.compile(new StreamSource(mStylesheet));
        source = proc.newDocumentBuilder().build(doc);
        if (mSerializer == null) {
            final Serializer out = new Serializer();
            out.setOutputProperty(Serializer.Property.METHOD, "xml");
            out.setOutputProperty(Serializer.Property.INDENT, "yes");
            out.setOutputStream(mOut);
            mSerializer = out;
        } else {
            mSerializer.setOutputStream(mOut);
        }
        final XsltTransformer trans = exp.load();
        trans.setInitialContextNode(source);
        trans.setDestination(mSerializer);
        trans.transform();
    } catch (final SaxonApiException e) {
        LOGGER.error("Saxon exception: " + e.getMessage(), e);
    } catch (final SirixException e) {
        LOGGER.error("TT exception: " + e.getMessage(), e);
    }
    return mOut;
}
Also used : DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) NodeInfo(net.sf.saxon.om.NodeInfo) StreamSource(javax.xml.transform.stream.StreamSource) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) SirixException(org.sirix.exception.SirixException) XsltCompiler(net.sf.saxon.s9api.XsltCompiler) XdmNode(net.sf.saxon.s9api.XdmNode) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) XsltExecutable(net.sf.saxon.s9api.XsltExecutable) Serializer(net.sf.saxon.s9api.Serializer)

Aggregations

SirixException (org.sirix.exception.SirixException)74 DocumentException (org.brackit.xquery.xdm.DocumentException)25 IOException (java.io.IOException)18 Database (org.sirix.api.Database)17 JaxRxException (org.jaxrx.core.JaxRxException)14 NodeReadTrx (org.sirix.api.NodeReadTrx)13 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)12 Session (org.sirix.api.Session)12 ResourceManager (org.sirix.api.ResourceManager)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 Path (java.nio.file.Path)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 QNm (org.brackit.xquery.atomic.QNm)7 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)6 OutputStream (java.io.OutputStream)5 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)5 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)5 StreamingOutput (javax.ws.rs.core.StreamingOutput)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)4