Search in sources :

Example 6 with Session

use of org.sirix.api.Session in project sirix by sirixdb.

the class NodeIdRepresentation method serializeAT.

/**
 * This method serializes requested resource with an access type.
 *
 * @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.
 * @param accessType
 *          The {@link IDAccessType} which indicates the access to a special
 *          node.
 */
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        NodeReadTrx rtx = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (revision == null) {
                rtx = session.beginNodeReadTrx();
            } else {
                rtx = session.beginNodeReadTrx(revision);
            }
            if (rtx.moveTo(nodeId).hasMoved()) {
                switch(accessType) {
                    case FIRSTCHILD:
                        if (!rtx.moveToFirstChild().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LASTCHILD:
                        if (rtx.moveToFirstChild().hasMoved()) {
                            long last = rtx.getNodeKey();
                            while (rtx.moveToRightSibling().hasMoved()) {
                                last = rtx.getNodeKey();
                            }
                            rtx.moveTo(last);
                        } else {
                            throw new JaxRxException(404, NOTFOUND);
                        }
                        break;
                    case RIGHTSIBLING:
                        if (!rtx.moveToRightSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LEFTSIBLING:
                        if (!rtx.moveToLeftSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    // nothing to do;
                    default:
                }
                if (wrapResult) {
                    output.write(BEGINRESULT);
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), 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, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                }
            } else {
                throw new JaxRxException(404, NOTFOUND);
            }
        } 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(rtx, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) IOException(java.io.IOException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 7 with Session

use of org.sirix.api.Session in project sirix by sirixdb.

the class NodeIdRepresentation method modifyResource.

/**
 * This method is responsible to modify the XML resource, which is addressed
 * through a unique node id.
 *
 * @param resourceName
 *          The name of the database, where the node id belongs to.
 * @param nodeId
 *          The node id.
 * @param newValue
 *          The new value of the node that has to be replaced.
 * @throws JaxRxException
 *           The exception occurred.
 */
public void modifyResource(final String resourceName, final long nodeId, final InputStream newValue) throws JaxRxException {
    synchronized (resourceName) {
        Session session = null;
        Database database = null;
        NodeWriteTrx wtx = null;
        boolean abort = false;
        if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
            try {
                database = Databases.openDatabase(mStoragePath);
                // Creating a new session
                session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
                // Creating a write transaction
                wtx = session.beginNodeWriteTrx();
                if (wtx.moveTo(nodeId).hasMoved()) {
                    final long parentKey = wtx.getParentKey();
                    wtx.remove();
                    wtx.moveTo(parentKey);
                    WorkerHelper.shredInputStream(wtx, newValue, Insert.ASFIRSTCHILD);
                } else {
                    // workerHelper.closeWTX(abort, wtx, session, database);
                    throw new JaxRxException(404, NOTFOUND);
                }
            } catch (final SirixException exc) {
                abort = true;
                throw new JaxRxException(exc);
            } finally {
                try {
                    WorkerHelper.closeWTX(abort, wtx, session, database);
                } catch (final SirixException exce) {
                    throw new JaxRxException(exce);
                }
            }
        } else {
            throw new JaxRxException(404, "Requested resource not found");
        }
    }
}
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 8 with Session

use of org.sirix.api.Session in project sirix by sirixdb.

the class TestNodeWrapper method testGetBaseURI.

@Test
public void testGetBaseURI() throws Exception {
    // Test with xml:base specified.
    final File source = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "data" + File.separator + "testBaseURI.xml");
    final Session session = generateSession();
    final NodeWriteTrx wtx = session.beginNodeWriteTrx();
    final XMLEventReader reader = XMLShredder.createFileReader(source);
    final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    final Processor proc = new Processor(false);
    final NodeInfo doc = new DocumentWrapper(session, proc.getUnderlyingConfiguration());
    doc.getNamePool().allocate("xml", "http://www.w3.org/XML/1998/namespace", "base");
    doc.getNamePool().allocate("", "", "baz");
    final NameTest test = new NameTest(Type.ELEMENT, "", "baz", doc.getNamePool());
    final AxisIterator iterator = doc.iterateAxis(Axis.DESCENDANT, test);
    final NodeInfo baz = (NodeInfo) iterator.next();
    assertEquals("http://example.org", baz.getBaseURI());
    session.close();
    mDatabase.close();
}
Also used : NameTest(net.sf.saxon.pattern.NameTest) Processor(net.sf.saxon.s9api.Processor) NodeInfo(net.sf.saxon.om.NodeInfo) NodeWriteTrx(org.sirix.api.NodeWriteTrx) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) File(java.io.File) AxisIterator(net.sf.saxon.tree.iter.AxisIterator) Session(org.sirix.api.Session) NameTest(net.sf.saxon.pattern.NameTest) Test(org.junit.Test)

Example 9 with Session

use of org.sirix.api.Session in project sirix by sirixdb.

the class WorkerHelperTest method testClose.

/**
 * This method tests
 * {@link WorkerHelper#closeWTX(boolean, NodeWriteTrx, Session, Database)}
 */
@Test(expected = IllegalStateException.class)
public void testClose() throws SirixException {
    Database database = Databases.openDatabase(DBFILE.getParentFile());
    Session session = database.getSession(new SessionConfiguration.Builder(DBFILE.getName()).build());
    final NodeWriteTrx wtx = session.beginNodeWriteTrx();
    WorkerHelper.closeWTX(false, wtx, session, database);
    wtx.commit();
    database = Databases.openDatabase(DBFILE.getParentFile());
    session = database.getSession(new SessionConfiguration.Builder(DBFILE.getName()).build());
    final NodeReadTrx rtx = session.beginNodeReadTrx();
    WorkerHelper.closeRTX(rtx, session, database);
    rtx.moveTo(11);
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) Test(org.junit.Test)

Example 10 with Session

use of org.sirix.api.Session in project sirix by sirixdb.

the class WorkerHelperTest method testSerializeXML.

/**
 * This method tests
 * {@link WorkerHelper#serializeXML(Session, OutputStream, boolean, boolean,Long)}
 */
@Test
public void testSerializeXML() throws SirixException, IOException {
    final Database database = Databases.openDatabase(DBFILE.getParentFile());
    final Session session = database.getSession(new SessionConfiguration.Builder(DBFILE.getName()).build());
    final OutputStream out = new ByteArrayOutputStream();
    assertNotNull("test serialize xml", WorkerHelper.serializeXML(session, out, true, true, null));
    session.close();
    database.close();
    out.close();
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Database(org.sirix.api.Database) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(org.sirix.api.Session) Test(org.junit.Test)

Aggregations

Session (org.sirix.api.Session)18 Database (org.sirix.api.Database)16 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)14 SirixException (org.sirix.exception.SirixException)12 JaxRxException (org.jaxrx.core.JaxRxException)10 NodeWriteTrx (org.sirix.api.NodeWriteTrx)10 IOException (java.io.IOException)9 WebApplicationException (javax.ws.rs.WebApplicationException)6 NodeReadTrx (org.sirix.api.NodeReadTrx)6 Test (org.junit.Test)5 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)3 Axis (org.sirix.api.Axis)3 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)3 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)3 XPathAxis (org.sirix.service.xml.xpath.XPathAxis)3 XMLEventReader (javax.xml.stream.XMLEventReader)2 NodeInfo (net.sf.saxon.om.NodeInfo)2 NameTest (net.sf.saxon.pattern.NameTest)2 Processor (net.sf.saxon.s9api.Processor)2