use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.
the class NodeIdRepresentation method addSubResource.
/**
* This method is responsible to perform a POST request to a node id. This
* method adds a new XML subtree as first child or as right sibling to the
* node which is addressed through a node id.
*
* @param resourceName
* The name of the database, the node id belongs to.
* @param nodeId
* The node id.
* @param input
* The new XML subtree.
* @param type
* The type which indicates if the new subtree has to be inserted as
* right sibling or as first child.
* @throws JaxRxException
* The exception occurred.
*/
public void addSubResource(final String resourceName, final long nodeId, final InputStream input, final IDAccessType type) throws JaxRxException {
Session session = null;
Database database = null;
NodeWriteTrx wtx = null;
synchronized (resourceName) {
boolean abort;
if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
abort = false;
try {
database = Databases.openDatabase(mStoragePath);
// Creating a new session
session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
// Creating a write transaction
wtx = session.beginNodeWriteTrx();
final boolean exist = wtx.moveTo(nodeId).hasMoved();
if (exist) {
if (type == IDAccessType.FIRSTCHILD) {
WorkerHelper.shredInputStream(wtx, input, Insert.ASFIRSTCHILD);
} else if (type == IDAccessType.RIGHTSIBLING) {
WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
} else if (type == IDAccessType.LASTCHILD) {
if (wtx.moveToFirstChild().hasMoved()) {
long last = wtx.getNodeKey();
while (wtx.moveToRightSibling().hasMoved()) {
last = wtx.getNodeKey();
}
wtx.moveTo(last);
WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
} else {
throw new JaxRxException(404, NOTFOUND);
}
} else if (type == IDAccessType.LEFTSIBLING && wtx.moveToLeftSibling().hasMoved()) {
WorkerHelper.shredInputStream(wtx, input, Insert.ASRIGHTSIBLING);
}
} else {
throw new JaxRxException(404, NOTFOUND);
}
} catch (final JaxRxException exce) {
// NOPMD due
// to
// different
// exception
// types
abort = true;
throw exce;
} catch (final Exception exce) {
abort = true;
throw new JaxRxException(exce);
} finally {
try {
WorkerHelper.closeWTX(abort, wtx, session, database);
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
}
}
}
use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.
the class NodeIdRepresentation method deleteResource.
/**
* This method is responsible to delete an XML resource addressed through a
* unique node id (except root node id).
*
* @param resourceName
* The name of the database, which the node id belongs to.
* @param nodeId
* The unique node id.
* @throws JaxRxException
* The exception occurred.
*/
public void deleteResource(final String resourceName, final long nodeId) 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();
// move to node with given rest id and deletes it
if (wtx.moveTo(nodeId).hasMoved()) {
wtx.remove();
wtx.commit();
} else {
// workerHelper.closeWTX(abort, wtx, session, database);
throw new JaxRxException(404, NOTFOUND);
}
} catch (final SirixException exce) {
abort = true;
throw new JaxRxException(exce);
} finally {
try {
WorkerHelper.closeWTX(abort, wtx, session, database);
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
} else {
throw new JaxRxException(404, "DB not found");
}
}
}
use of org.jaxrx.core.JaxRxException 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");
}
}
use of org.jaxrx.core.JaxRxException 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");
}
}
}
use of org.jaxrx.core.JaxRxException in project sirix by sirixdb.
the class DatabaseRepresentation method serialize.
/**
* This method is responsible to build an {@link OutputStream} containing an
* XML file out of the sirix file.
*
* @param resource
* The name of the resource that will be offered as XML.
* @param nodeid
* To response the resource with a restid for each node (
* <code>true</code>) or without ( <code>false</code>).
* @param revision
* The revision of the requested resource. If <code>null</code>, than
* response the latest revision.
* @return The {@link OutputStream} containing the serialized XML file.
* @throws IOException
* @throws SirixException
* @throws WebApplicationException
*/
private final OutputStream serialize(final String resource, final Integer revision, final boolean nodeid, final OutputStream output, final boolean wrapResult) throws IOException, JaxRxException, SirixException {
if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
try {
if (wrapResult) {
output.write(beginResult.getBytes());
serializIt(resource, revision, output, nodeid);
output.write(endResult.getBytes());
} else {
serializIt(resource, revision, output, nodeid);
}
} catch (final Exception exce) {
throw new JaxRxException(exce);
}
} else {
throw new JaxRxException(404, "Not found");
}
return output;
}
Aggregations