Search in sources :

Example 36 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class NodeIdRepresentationTest method testAddSubResource.

/**
 * Test method for
 * {@link org.sirix.service.jaxrx.implementation.NodeIdRepresentation#addSubResource(java.lang.String, long, java.io.InputStream, org.sirix.service.jaxrx.enums.IDAccessType)}
 * .
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
@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());
    // Test append first child
    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);
    // Test append last child
    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);
    // Test append left sibling
    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);
    // Test append right sibling
    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);
}
Also used : QueryParameter(org.jaxrx.core.QueryParameter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 37 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class NodeIdRepresentationTest method testGetResource.

/**
 * Test method for
 * {@link org.sirix.service.jaxrx.implementation.NodeIdRepresentation#getResource(java.lang.String, long, java.util.Map)}
 * .
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
@Test
public final void testGetResource() throws WebApplicationException, IOException, ParserConfigurationException, SAXException {
    final Map<QueryParameter, String> queryParams = new HashMap<QueryParameter, String>();
    queryParams.put(QueryParameter.OUTPUT, LITERALSTRUE);
    queryParams.put(QueryParameter.WRAP, LITERALSTRUE);
    queryParams.put(QueryParameter.REVISION, "0");
    ByteArrayOutputStream outputStream;
    StreamingOutput result;
    Document doc;
    NodeList list;
    Node node;
    Node resultNode;
    Attr attribute;
    // Test for fist child
    result = ridWorker.getResource(RESOURCENAME, NODEIDGETRESOURCE, queryParams);
    outputStream = new ByteArrayOutputStream();
    result.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName("city");
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    final NodeList resultList = doc.getElementsByTagName(RESULTNAME);
    resultNode = resultList.item(0);
    outputStream.close();
    assertNotNull("Test if node exist 1a", node);
    assertNotNull("Test if node exist 1b", resultNode);
    assertNotNull("Test if node exist 1c", attribute);
    queryParams.clear();
    queryParams.put(QueryParameter.OUTPUT, LITERALSFALSE);
    queryParams.put(QueryParameter.QUERY, null);
    queryParams.put(QueryParameter.WRAP, LITERALSFALSE);
    queryParams.put(QueryParameter.REVISION, null);
    result = ridWorker.getResource(RESOURCENAME, NODEIDGETRESOURCE, queryParams);
    outputStream = new ByteArrayOutputStream();
    result.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName(COUNTRYNAME);
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    outputStream.close();
    assertNotNull("Test if node exist 2a", node);
    assertNull("Test if node exist 2b", resultNode);
    assertNull("Test if node exist 2c", attribute);
    queryParams.clear();
    queryParams.put(QueryParameter.OUTPUT, LITERALSFALSE);
    queryParams.put(QueryParameter.WRAP, LITERALSTRUE);
    queryParams.put(QueryParameter.REVISION, null);
    result = ridWorker.getResource(RESOURCENAME, NODEIDGETRESOURCE, queryParams);
    outputStream = new ByteArrayOutputStream();
    result.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName("city");
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    outputStream.close();
    assertNotNull("Test if node exist 3a", node);
    assertNotNull("Test if node exist 3b", resultNode);
    assertNull("Test if node exist 3c", attribute);
    queryParams.clear();
    queryParams.put(QueryParameter.OUTPUT, LITERALSFALSE);
    queryParams.put(QueryParameter.WRAP, LITERALSTRUE);
    queryParams.put(QueryParameter.REVISION, "0");
    result = ridWorker.getResource(RESOURCENAME, NODEIDGETRESOURCE, queryParams);
    outputStream = new ByteArrayOutputStream();
    result.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName(COUNTRYNAME);
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    outputStream.close();
    assertNotNull("Test if node exist 4a", node);
    assertNotNull("Test if node exist 4b", resultNode);
    assertNull("Test if node exist 4c", attribute);
}
Also used : QueryParameter(org.jaxrx.core.QueryParameter) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test)

Example 38 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class RESTResponseHelperTest method testBuildResponseOfDomLR.

/**
 * Test method for
 * {@link org.sirix.service.jaxrx.util.RESTResponseHelper#buildResponseOfDomLR(java.util.Map)}
 * .
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws SirixException
 * @throws InterruptedException
 */
@Test
public final void testBuildResponseOfDomLR() throws WebApplicationException, IOException, ParserConfigurationException, SAXException, SirixException, InterruptedException {
    final List<String> availResources = new ArrayList<String>();
    availResources.add(FACT);
    availResources.add(EBAY);
    availResources.add(BOOK);
    availResources.add(SHAKE);
    final DatabaseRepresentation sirix = new DatabaseRepresentation(TestHelper.PATHS.PATH1.getFile());
    InputStream input = NodeIdRepresentationTest.class.getClass().getResourceAsStream(RESPATH);
    sirix.shred(input, FACT);
    input.close();
    input = NodeIdRepresentationTest.class.getClass().getResourceAsStream(RESPATH);
    sirix.shred(input, EBAY);
    input.close();
    input.close();
    input = NodeIdRepresentationTest.class.getClass().getResourceAsStream(RESPATH);
    sirix.add(input, BOOK);
    input = NodeIdRepresentationTest.class.getClass().getResourceAsStream(RESPATH);
    sirix.shred(input, SHAKE);
    input.close();
    Node node;
    Attr attribute;
    final StreamingOutput result = RESTResponseHelper.buildResponseOfDomLR(TestHelper.PATHS.PATH1.getFile(), availResources);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    result.write(outputStream);
    final Document doc = DOMHelper.buildDocument(outputStream);
    final NodeList listRes = doc.getElementsByTagName("resource");
    assertEquals("Test for the length of resource", 4, listRes.getLength());
    node = listRes.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    assertEquals("test for name factbook", FACT, attribute.getTextContent());
    attribute = (Attr) node.getAttributes().getNamedItem(LREV);
    assertNotNull("test for existence of revision attribute", attribute);
    node = listRes.item(1);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    assertEquals("test for name ebay", EBAY, attribute.getTextContent());
    attribute = (Attr) node.getAttributes().getNamedItem(LREV);
    assertNotNull("test for existence of revision attribute", attribute);
    node = listRes.item(2);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    assertEquals("test for name shakespeare", BOOK, attribute.getTextContent());
    attribute = (Attr) node.getAttributes().getNamedItem(LREV);
    assertNotNull("test for existence of revision attribute in collection", attribute);
    node = listRes.item(3);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    assertEquals("test for name books", SHAKE, attribute.getTextContent());
    attribute = (Attr) node.getAttributes().getNamedItem(LREV);
    assertNotNull("test for existence of revision attribute in collection", attribute);
    outputStream.close();
    sirix.deleteResource(EBAY);
    sirix.deleteResource(FACT);
    sirix.deleteResource(BOOK);
    sirix.deleteResource(SHAKE);
}
Also used : NodeIdRepresentationTest(org.sirix.service.jaxrx.implementation.NodeIdRepresentationTest) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) StreamingOutput(javax.ws.rs.core.StreamingOutput) DatabaseRepresentation(org.sirix.service.jaxrx.implementation.DatabaseRepresentation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Test(org.junit.Test) NodeIdRepresentationTest(org.sirix.service.jaxrx.implementation.NodeIdRepresentationTest)

Example 39 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput 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 40 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class NodeIdRepresentation method getResourceByAT.

/**
 * This method is responsible to deliver the whole XML resource addressed by a
 * unique node id.
 *
 * @param resourceName
 *          The name of the database, where the node id belongs.
 * @param nodeId
 *          The unique node id of the requested resource.
 * @param queryParams
 *          The optional query parameters.
 * @param accessType
 *          The id access type to access a resource by a relative method type
 *          defined in {@link IDAccessType}.
 * @return The whole XML resource addressed by a unique node id.
 * @throws JaxRxException
 *           The exception occurred.
 */
public StreamingOutput getResourceByAT(final String resourceName, final long nodeId, final Map<QueryParameter, String> queryParams, final IDAccessType accessType) throws JaxRxException {
    final StreamingOutput sOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            // final String xPath = queryParams.get(QueryParameter.QUERY);
            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) ? false : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (doNodeId == null) ? false : doNodeId.equalsIgnoreCase(YESSTRING);
            final Integer rev = revision == null ? null : Integer.valueOf(revision);
            serializeAT(resourceName, nodeId, rev, nodeid, output, wrapResult, accessType);
        }
    };
    return sOutput;
}
Also used : OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)191 OutputStream (java.io.OutputStream)85 Response (javax.ws.rs.core.Response)76 Path (javax.ws.rs.Path)53 Produces (javax.ws.rs.Produces)52 IOException (java.io.IOException)51 GET (javax.ws.rs.GET)50 File (java.io.File)45 InputStream (java.io.InputStream)45 Test (org.junit.Test)44 WebApplicationException (javax.ws.rs.WebApplicationException)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 List (java.util.List)26 MediaType (javax.ws.rs.core.MediaType)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ArrayList (java.util.ArrayList)20 Consumes (javax.ws.rs.Consumes)20 HashMap (java.util.HashMap)19 POST (javax.ws.rs.POST)19 FileOutputStream (java.io.FileOutputStream)17