Search in sources :

Example 1 with JaxRxException

use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.

the class DatabaseRepresentation method getLastRevision.

/**
 * This method reads the existing database, and offers the last revision id of
 * the database
 *
 * @param resourceName
 *          The name of the existing database.
 * @return The {@link OutputStream} containing the result
 * @throws WebApplicationException
 *           The Exception occurred.
 * @throws SirixException
 */
public long getLastRevision(final String resourceName) throws JaxRxException, SirixException {
    long lastRevision;
    if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
        Database database = Databases.openDatabase(mStoragePath);
        NodeReadTrx rtx = null;
        Session session = null;
        try {
            session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
            lastRevision = session.getMostRecentRevisionNumber();
        } catch (final Exception globExcep) {
            throw new JaxRxException(globExcep);
        } finally {
            WorkerHelper.closeRTX(rtx, session, database);
        }
    } else {
        throw new JaxRxException(404, "Resource not found");
    }
    return lastRevision;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 2 with JaxRxException

use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.

the class DatabaseRepresentation method getResource.

/**
 * This method is responsible to deliver the whole database. Additional
 * parameters can be set (wrap, revision, output) which change the response
 * view.
 *
 * @param resourceName
 *          The name of the requested database.
 * @param queryParams
 *          The optional query parameters.
 * @return The XML database resource, depending on the query parameters.
 * @throws JaxRxException
 *           The exception occurred.
 */
public StreamingOutput getResource(final String resourceName, final Map<QueryParameter, String> queryParams) throws JaxRxException {
    final StreamingOutput streamingOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            final String revision = queryParams.get(QueryParameter.REVISION);
            final String wrap = queryParams.get(QueryParameter.WRAP);
            final String nodeId = queryParams.get(QueryParameter.OUTPUT);
            final boolean wrapResult = (wrap == null) ? false : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (nodeId == null) ? false : nodeId.equalsIgnoreCase(YESSTRING);
            try {
                if (revision == null) {
                    serialize(resourceName, null, nodeid, output, wrapResult);
                } else {
                    // pattern which have to match against the input
                    final Pattern pattern = Pattern.compile("[0-9]+[-]{1}[1-9]+");
                    final Matcher matcher = pattern.matcher(revision);
                    if (matcher.matches()) {
                        getModificHistory(resourceName, revision, nodeid, output, wrapResult);
                    } else {
                        serialize(resourceName, Integer.valueOf(revision), nodeid, output, wrapResult);
                    }
                }
            } catch (final NumberFormatException exce) {
                throw new JaxRxException(400, exce.getMessage());
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    };
    return streamingOutput;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) StreamingOutput(javax.ws.rs.core.StreamingOutput) JaxRxException(org.jaxrx.core.JaxRxException)

Example 3 with JaxRxException

use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.

the class DatabaseRepresentation method shred.

/**
 * This method is responsible to save the XML file, which is in an
 * {@link InputStream}, as a sirix object.
 *
 * @param xmlInput
 *          XML file wrapped in an {@link InputStream}
 * @param resource
 *          name of the resource
 * @return {@code true} if shredding process has been successful,
 *         {@code false} otherwise.
 * @throws SirixException
 *           if any sirix related exception occurs
 */
public final boolean shred(final InputStream xmlInput, final String resource) throws SirixException {
    boolean allOk = false;
    NodeWriteTrx wtx = null;
    Database database = null;
    Session session = null;
    boolean abort = false;
    try {
        final DatabaseConfiguration dbConf = new DatabaseConfiguration(mStoragePath);
        Databases.createDatabase(dbConf);
        database = Databases.openDatabase(dbConf.getFile());
        // Shredding the database to the file as XML
        final ResourceConfiguration resConf = new ResourceConfiguration.Builder(resource, dbConf).revisionsToRestore(1).build();
        if (database.createResource(resConf)) {
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            wtx = session.beginNodeWriteTrx();
            final XMLShredder shredder = new XMLShredder.Builder(wtx, RESTXMLShredder.createReader(xmlInput), Insert.ASFIRSTCHILD).commitAfterwards().build();
            shredder.call();
            allOk = true;
        }
    } catch (final Exception exce) {
        abort = true;
        throw new JaxRxException(exce);
    } finally {
        WorkerHelper.closeWTX(abort, wtx, session, database);
    }
    return allOk;
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) NodeWriteTrx(org.sirix.api.NodeWriteTrx) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) RESTXMLShredder(org.sirix.service.jaxrx.util.RESTXMLShredder) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) JaxRxException(org.jaxrx.core.JaxRxException)

Example 4 with JaxRxException

use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.

the class DatabaseRepresentation method serializIt.

/**
 * The XML serializer to a given tnk file.
 *
 * @param resource
 *          The resource that has to be serialized.
 * @param revision
 *          The revision of the document.
 * @param output
 *          The output stream where we write the XML file.
 * @param nodeid
 *          <code>true</code> when you want the result nodes with node id's.
 *          <code>false</code> otherwise.
 * @throws WebApplicationException
 *           The exception occurred.
 * @throws SirixException
 */
private void serializIt(final String resource, final Integer revision, final OutputStream output, final boolean nodeid) throws JaxRxException, SirixException {
    // Connection to sirix, creating a session
    Database database = null;
    Session session = null;
    // INodeReadTrx rtx = null;
    try {
        database = Databases.openDatabase(mStoragePath);
        session = database.getSession(new SessionConfiguration.Builder(resource).build());
        // and creating a transaction
        // if (revision == null) {
        // rtx = session.beginReadTransaction();
        // } else {
        // rtx = session.beginReadTransaction(revision);
        // }
        final XMLSerializerBuilder builder;
        if (revision == null)
            builder = new XMLSerializerBuilder(session, output);
        else
            builder = new XMLSerializerBuilder(session, output, revision);
        if (nodeid) {
            builder.emitRESTful().emitIDs();
        }
        final XMLSerializer serializer = builder.build();
        serializer.call();
    } catch (final Exception exce) {
        throw new JaxRxException(exce);
    } finally {
        // closing the sirix storage
        WorkerHelper.closeRTX(null, session, database);
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 5 with JaxRxException

use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.

the class NodeIdRepresentation method performQueryOnResource.

/**
 * This method is responsible to perform a XPath query expression on the XML
 * resource which is addressed through a unique node id.
 *
 * @param resourceName
 *          The name of the database, the node id belongs to.
 * @param nodeId
 *          The node id of the requested resource.
 * @param query
 *          The XPath expression.
 * @param queryParams
 *          The optional query parameters (output, wrap, revision).
 * @return The result of the XPath query expression.
 */
public StreamingOutput performQueryOnResource(final String resourceName, final long nodeId, final String query, final Map<QueryParameter, String> queryParams) {
    final StreamingOutput sOutput = new StreamingOutput() {

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

Aggregations

JaxRxException (org.jaxrx.core.JaxRxException)16 SirixException (org.sirix.exception.SirixException)14 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)10 Database (org.sirix.api.Database)10 Session (org.sirix.api.Session)10 IOException (java.io.IOException)8 WebApplicationException (javax.ws.rs.WebApplicationException)5 StreamingOutput (javax.ws.rs.core.StreamingOutput)5 NodeWriteTrx (org.sirix.api.NodeWriteTrx)5 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)4 OutputStream (java.io.OutputStream)3 NodeReadTrx (org.sirix.api.NodeReadTrx)3 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)3 RestXPathProcessor (org.sirix.service.jaxrx.util.RestXPathProcessor)2 XMLSerializerProperties (org.sirix.service.xml.serialize.XMLSerializerProperties)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1