NodeIdRepresentation { 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); session = database.getSession(new SessionConfiguration.Builder( resourceName).build()); 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) { 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); } } } } } NodeIdRepresentation(final File storagePath); StreamingOutput getResource(final String resourceName,
			final long nodeId, final Map<QueryParameter, String> queryParams); StreamingOutput getResourceByAT(final String resourceName,
			final long nodeId, final Map<QueryParameter, String> queryParams,
			final IDAccessType accessType); StreamingOutput performQueryOnResource(final String resourceName,
			final long nodeId, final String query,
			final Map<QueryParameter, String> queryParams); void deleteResource(final String resourceName, final long nodeId); void modifyResource(final String resourceName, final long nodeId,
			final InputStream newValue); void addSubResource(final String resourceName, final long nodeId,
			final InputStream input, final IDAccessType type);  }
@Test public final void testAddSubResource() throws WebApplicationException, IOException, ParserConfigurationException, SAXException { final Map<QueryParameter, String> queryParams = new HashMap<QueryParameter, String>(); queryParams.put(QueryParameter.OUTPUT, LITERALSTRUE); queryParams.put(QueryParameter.WRAP, LITERALSTRUE); final Map<QueryParameter, String> queryParamsComp = new HashMap<QueryParameter, String>(); queryParams.put(QueryParameter.OUTPUT, LITERALSTRUE); ByteArrayOutputStream outputStream; StreamingOutput result; Document doc; NodeList list; Node node; final InputStream newNode = new ByteArrayInputStream( "<myNode><test>hello</test></myNode>".getBytes()); ridWorker.addSubResource(RESOURCENAME, NODEIDGETRESOURCE, newNode, IDAccessType.FIRSTCHILD); result = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParamsComp, IDAccessType.FIRSTCHILD); outputStream = new ByteArrayOutputStream(); result.write(outputStream); doc = DOMHelper.buildDocument(outputStream); list = doc.getElementsByTagName(NODENAME); node = list.item(0); outputStream.close(); assertNotNull("Test if first child exist after inserting", node); newNode.reset(); ridWorker.addSubResource(RESOURCENAME, NODEIDGETRESOURCE, newNode, IDAccessType.LASTCHILD); result = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParamsComp, IDAccessType.LASTCHILD); outputStream = new ByteArrayOutputStream(); result.write(outputStream); doc = DOMHelper.buildDocument(outputStream); list = doc.getElementsByTagName(NODENAME); node = list.item(0); outputStream.close(); assertNotNull("Test if last child exist after inserting", node); newNode.reset(); ridWorker.addSubResource(RESOURCENAME, NODEIDGETRESOURCE, newNode, IDAccessType.LEFTSIBLING); result = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParamsComp, IDAccessType.LEFTSIBLING); outputStream = new ByteArrayOutputStream(); result.write(outputStream); doc = DOMHelper.buildDocument(outputStream); list = doc.getElementsByTagName(NODENAME); node = list.item(0); outputStream.close(); assertNotNull("Test if left sibling exist after inserting", node); newNode.reset(); ridWorker.addSubResource(RESOURCENAME, NODEIDGETRESOURCE, newNode, IDAccessType.RIGHTSIBLING); result = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParamsComp, IDAccessType.RIGHTSIBLING); outputStream = new ByteArrayOutputStream(); result.write(outputStream); doc = DOMHelper.buildDocument(outputStream); list = doc.getElementsByTagName(NODENAME); node = list.item(0); outputStream.close(); assertNotNull("Test if right sibling exist after inserting", node); }