Search in sources :

Example 31 with StreamingOutput

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

the class RESTResponseHelper method buildResponseOfDomLR.

/**
 * This method builds the overview for the resources and collection we offer
 * in our implementation.
 *
 * @param storagePath
 *          path to the storage
 * @param availableRes
 *          a list of available resources or collections
 * @return the streaming output for the HTTP response body
 */
public static StreamingOutput buildResponseOfDomLR(final File storagePath, final List<String> availableRes) {
    final StreamingOutput sOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {
            Document document;
            try {
                document = createSurroundingXMLResp();
                final Element resElement = RESTResponseHelper.createResultElement(document);
                List<Element> collections;
                try {
                    collections = RESTResponseHelper.createCollectionElementDBs(storagePath, availableRes, document);
                } catch (final SirixException exce) {
                    throw new WebApplicationException(exce);
                }
                for (final Element resource : collections) {
                    resElement.appendChild(resource);
                }
                document.appendChild(resElement);
                final DOMSource domSource = new DOMSource(document);
                final StreamResult streamResult = new StreamResult(output);
                final Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform(domSource, streamResult);
            } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
                throw new WebApplicationException(e);
            }
        }
    };
    return sOutput;
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) WebApplicationException(javax.ws.rs.WebApplicationException) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) StreamingOutput(javax.ws.rs.core.StreamingOutput) Document(org.w3c.dom.Document) SirixException(org.sirix.exception.SirixException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 32 with StreamingOutput

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

the class DatabaseRepresentationTest method performQueryOnResource.

/**
 * This method tests
 * {@link DatabaseRepresentation#performQueryOnResource(String, String, Map)}
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
@Test
public void performQueryOnResource() throws WebApplicationException, IOException, ParserConfigurationException, SAXException {
    final Map<QueryParameter, String> params = new HashMap<QueryParameter, String>();
    params.put(QueryParameter.OUTPUT, LITERALTRUE);
    params.put(QueryParameter.WRAP, LITERALTRUE);
    final StreamingOutput sOutput = sirix.performQueryOnResource(RESOURCENAME, "//continent", params);
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    sOutput.write(output);
    final Document doc = DOMHelper.buildDocument(output);
    Node node = doc.getElementsByTagName("continent").item(0);
    assertNotNull("check if continent exists", node);
    node = doc.getElementsByTagName("country").item(0);
    assertNull("check for null country object", node);
    output.close();
}
Also used : QueryParameter(org.jaxrx.core.QueryParameter) HashMap(java.util.HashMap) 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 33 with StreamingOutput

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

the class DatabaseRepresentationTest method addResource.

/**
 * This method tests {@link DatabaseRepresentation#add(InputStream, String)}
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws SirixException
 */
@Test
public void addResource() throws WebApplicationException, IOException, ParserConfigurationException, SAXException, SirixException {
    final InputStream input = DatabaseRepresentationTest.class.getResourceAsStream("/books.xml");
    sirix.deleteResource(RESOURCENAME);
    assertTrue(sirix.add(input, RESOURCENAME));
    final Map<QueryParameter, String> params = new HashMap<QueryParameter, String>();
    params.put(QueryParameter.WRAP, LITERALTRUE);
    final StreamingOutput sOutput = sirix.performQueryOnResource(RESOURCENAME, ".", params);
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    sOutput.write(output);
    final Document doc = DOMHelper.buildDocument(output);
    Node node = doc.getElementsByTagName("books").item(0);
    assertNotNull("check if books has been added to factbook", node);
    // node = doc.getElementsByTagName("mondial").item(0);
    // assertNotNull("check if mondial still exists", node);
    output.close();
}
Also used : QueryParameter(org.jaxrx.core.QueryParameter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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 34 with StreamingOutput

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

the class DatabaseRepresentationTest method getResource.

/**
 * This method tests
 * {@link DatabaseRepresentation#getResource(String, java.util.Map)}
 *
 * @throws SirixException
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
@Test
public void getResource() throws SirixException, WebApplicationException, IOException, ParserConfigurationException, SAXException {
    final Map<QueryParameter, String> queryParams = new HashMap<QueryParameter, String>();
    Document doc;
    Node node;
    Node resultNode;
    Attr attribute;
    StreamingOutput sOutput = sirix.getResource(RESOURCENAME, queryParams);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    sOutput.write(output);
    doc = DOMHelper.buildDocument(output);
    node = doc.getElementsByTagName("mondial").item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    assertNotNull("mondial does exist", node);
    assertNull("test if result node exists - null", resultNode);
    assertNull("test if id element exists - null", attribute);
    output.close();
    queryParams.put(QueryParameter.WRAP, LITERALTRUE);
    sOutput = sirix.getResource(RESOURCENAME, queryParams);
    output = new ByteArrayOutputStream();
    sOutput.write(output);
    doc = DOMHelper.buildDocument(output);
    node = doc.getElementsByTagName("country").item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    assertNotNull("test if country exists", node);
    assertNotNull("test if result node exists", resultNode);
    assertNull("test if id element exists", attribute);
    output.close();
    queryParams.put(QueryParameter.OUTPUT, LITERALTRUE);
    sOutput = sirix.getResource(RESOURCENAME, queryParams);
    output = new ByteArrayOutputStream();
    sOutput.write(output);
    doc = DOMHelper.buildDocument(output);
    node = doc.getElementsByTagName(NAME).item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    assertNotNull("test if country exists2", node);
    assertNotNull("test if result node exists2", resultNode);
    assertNotNull("test if id element exists2", attribute);
    output.close();
    sOutput = sirix.getResource(RESOURCENAME, queryParams);
    output = new ByteArrayOutputStream();
    sOutput.write(output);
    doc = DOMHelper.buildDocument(output);
    node = doc.getElementsByTagName("city").item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(IDNAME);
    resultNode = doc.getElementsByTagName(RESULTNAME).item(0);
    assertNotNull("test if city exists2", node);
    assertNotNull("test if result node exists2", resultNode);
    assertNotNull("test if id element exists2", attribute);
    output.close();
}
Also used : QueryParameter(org.jaxrx.core.QueryParameter) HashMap(java.util.HashMap) 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 35 with StreamingOutput

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

the class NodeIdRepresentationTest method testGetResourceByAT.

/**
 * Test method for
 * {@link org.sirix.service.jaxrx.implementation.NodeIdRepresentation#getResourceByAT(java.lang.String, long, java.util.Map, org.sirix.service.jaxrx.enums.IDAccessType)}
 * .
 *
 * @throws IOException
 * @throws WebApplicationException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
@Test
public final void testGetResourceByAT() 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, "1");
    ByteArrayOutputStream outputStream;
    Document doc;
    NodeList list;
    String textContent = null;
    Node node;
    Attr attribute;
    // Test for fist child
    final StreamingOutput fChildOutput = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParams, IDAccessType.FIRSTCHILD);
    outputStream = new ByteArrayOutputStream();
    fChildOutput.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName(NAME);
    node = list.item(0);
    textContent = node.getTextContent();
    outputStream.close();
    assertEquals("Test expected name ", "Albania", textContent);
    // Test for last child final StreamingOutput lChildOutput =
    final StreamingOutput lChildOutput = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParams, IDAccessType.LASTCHILD);
    outputStream = new ByteArrayOutputStream();
    lChildOutput.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName("border");
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem("length");
    outputStream.close();
    assertEquals("Test expected border value ", "287", attribute.getValue());
    // Test for rightSibling final StreamingOutput rSiblingOutput =
    final StreamingOutput rSiblingOutput = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParams, IDAccessType.RIGHTSIBLING);
    outputStream = new ByteArrayOutputStream();
    rSiblingOutput.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName("country");
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    outputStream.close();
    assertEquals("Test expected right sibling name ", "Andorra", attribute.getValue());
    // Test for leftSibling final StreamingOutput lSiblingOutput =
    final StreamingOutput lSiblingOutput = ridWorker.getResourceByAT(RESOURCENAME, NODEIDGETRESOURCE, queryParams, IDAccessType.LEFTSIBLING);
    outputStream = new ByteArrayOutputStream();
    lSiblingOutput.write(outputStream);
    doc = DOMHelper.buildDocument(outputStream);
    list = doc.getElementsByTagName("continent");
    node = list.item(0);
    attribute = (Attr) node.getAttributes().getNamedItem(NAME);
    outputStream.close();
    assertEquals("Test expected right sibling name ", "Africa", attribute.getValue());
}
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)

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