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;
}
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();
}
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();
}
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();
}
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());
}
Aggregations