LinkedDataServlet extends AbstractCumulusServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RDFFormat format = Rio.getWriterFormatForMIMEType(parseContentTypeHeader(req)); if (format == null) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID + " Content-type '" + parseContentTypeHeader(req) + "' not supported."); sendError(req, resp, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID + " Content '" + parseContentTypeHeader(req) + "' not supported."); return; } try { final Store crdf = ((Store) getServletContext().getAttribute(ConfigParams.STORE)); if ((crdf == null) || !crdf.isOpen()) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG + " Store was null or not initialized."); sendError( req, resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } RDFParser parser = Rio.createParser(format); parser.setRDFHandler(new RDFHandler() { @Override public void endRDF() throws RDFHandlerException { } @Override public void handleComment(String comment) throws RDFHandlerException { } @Override public void handleNamespace(String prefix, String uri) throws RDFHandlerException { } @Override public void handleStatement(Statement st) throws RDFHandlerException { try { crdf.addData(st); } catch (CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } } @Override public void startRDF() throws RDFHandlerException { } }); try { parser.parse(req.getInputStream(), parseBaseURI(req)); } catch (RuntimeException exception) { throw new RDFParseException(exception); } resp.setStatus(HttpServletResponse.SC_CREATED); } catch (RDFParseException exception) { _log.debug(MessageCatalog._00029_RDF_PARSE_FAILURE, exception); sendError(req, resp, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00029_RDF_PARSE_FAILURE, exception); } catch (RDFHandlerException exception) { _log.debug(MessageCatalog._00029_RDF_PARSE_FAILURE, exception); sendError(req, resp, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00029_RDF_PARSE_FAILURE, exception); } catch (IOException exception) { _log.debug(MessageCatalog._00029_RDF_PARSE_FAILURE, exception); sendError(req, resp, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00029_RDF_PARSE_FAILURE, exception); } catch (Exception exception) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, exception); sendError(req, resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, exception); } }  @Override void doDelete(HttpServletRequest req, HttpServletResponse resp); @Override void doGet(HttpServletRequest req, HttpServletResponse resp); @Override void doPost(HttpServletRequest req, HttpServletResponse resp); @Override void doPut(HttpServletRequest req, HttpServletResponse resp); @Override void init();  }
@Test public void post() throws IOException, CumulusStoreException, RDFHandlerException, ServletException { for (int i = 0; i < TRIPLES_NT.size(); i++) { for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) { TRIPLE_STORE.clear(); assertTrue("store should be empty", !TRIPLE_STORE.query(new Value[] { null, null, null }).hasNext()); Model model = new LinkedHashModel(parseNX(TRIPLES_NT.get(i))); final ByteArrayOutputStream out = new ByteArrayOutputStream(); Rio.write(model, out, RDFFormat.forMIMEType(mime_type)); when(_request.getHeader(Headers.CONTENT_TYPE)).thenReturn(mime_type); when(_request.getInputStream()).thenReturn(new ServletInputStream() { final InputStream _inputStream = new ByteArrayInputStream(out.toByteArray()); @Override public int read() throws IOException { return _inputStream.read(); } }); _ld_servlet.doPost(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_CREATED); for (Statement stmt : model) { assertTrue("statement '" + stmt + "' has not been been added correctly for serialization '" + mime_type + "'", TRIPLE_STORE.query(Util.toValueArray(stmt)) .hasNext()); } } } }
@Test public void postInvalidNT() throws IOException, ServletException { when(_request.getHeader(Parameters.CONTENT_TYPE)).thenReturn(MimeTypes.N_TRIPLES); for (int i = 0; i < TRIPLES_INVALID_NT.size(); i++) { final String triples = TRIPLES_INVALID_NT.get(i); when(_request.getInputStream()).thenReturn(new ServletInputStream() { private InputStream _delegate = new ByteArrayInputStream(triples.getBytes()); @Override public int read() throws IOException { return _delegate.read(); } }); _ld_servlet.doPost(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_BAD_REQUEST); } }
@Test public void postInvalidXML() throws IOException, URISyntaxException, ServletException { when(_request.getHeader(Parameters.CONTENT_TYPE)).thenReturn(MimeTypes.RDF_XML); for (int i = 0; i < TRIPLES_INVALID_XML.size(); i++) { final String triples = TRIPLES_INVALID_XML.get(i); when(_request.getInputStream()).thenReturn(new ServletInputStream() { private InputStream _delegate = new ByteArrayInputStream(triples.getBytes()); @Override public int read() throws IOException { return _delegate.read(); } }); _ld_servlet.doPost(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_BAD_REQUEST); } }