LinkedDataServlet extends AbstractCumulusServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final RDFFormat format = Rio.getWriterFormatForMIMEType(parseAcceptHeader(req)); if (format == null) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID + " Mime-type '" + parseAcceptHeader(req) + "' not supported."); sendError(req, resp, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID + " Mime-type '" + parseAcceptHeader(req) + "' not supported."); return; } SailRepositoryConnection connection = null; RepositoryResult<Statement> repo_res1 = null, repo_res2 = null; try { SailRepository repository = (SailRepository) getServletContext().getAttribute(ConfigParams.SESAME_REPO); if ((repository == null) || !repository.isInitialized()) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG + " Repository was null or not initialized."); sendError( req, resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); URI entity = _valueFactory.createURI(req.getRequestURL().toString()); final Iterator<Statement> res1 = toResultIterator(repo_res1 = connection.getStatements(entity, null, null, false)); final Iterator<Statement> res2 = toResultIterator(repo_res2 = connection.getStatements(null, null, entity, false)); if (!res1.hasNext() && !res2.hasNext()) { sendError(req, resp, HttpServletResponse.SC_NOT_FOUND, MessageCatalog._00033_RESOURCE_NOT_FOUND_MSG); } else { resp.setCharacterEncoding(Environment.CHARSET_UTF8.name()); resp.setContentType(format.getDefaultMIMEType()); resp.setStatus(HttpServletResponse.SC_OK); RDFWriter writer = Rio.createWriter(format, resp.getOutputStream()); Rio.write(new Iterable<Statement>() { @Override public Iterator<Statement> iterator() { return Iterators.concat(res1, res2); } }, writer); } } 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); } finally { try { if (repo_res1 != null) { repo_res1.close(); } if (repo_res2 != null) { repo_res2.close(); } if (connection != null) { connection.close(); } } 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 get() throws IOException, Exception { for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) { when(_request.getHeader(Parameters.ACCEPT)).thenReturn(mime_type); for (int i = 0; i < URIS.size(); i++) { File tmp = tmpFile(); when(_response.getOutputStream()).thenReturn(new StubServletOutputStream(tmp)); when(_request.getRequestURL()).thenReturn(new StringBuffer(URIS.get(i))); _ld_servlet.doGet(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_OK); assertTrue(parseAsList(new FileInputStream(tmp), RDFFormat.forMIMEType(mime_type)).size() > 0); } } }
@Test public void getHTML() throws IOException, Exception { when(_request.getHeader(Parameters.ACCEPT)).thenReturn(MimeTypes.TEXT_HTML); for (int i = 0; i < URIS.size(); i++) { when(_response.getOutputStream()).thenReturn(new StubServletOutputStream(tmpFile())); when(_request.getRequestURL()).thenReturn(new StringBuffer(URIS.get(i))); _ld_servlet.doGet(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_OK); } }
@Test public void getInvalid() throws IOException, ServletException { for (String mime_type : MimeTypes.RDF_SERIALIZATIONS) { when(_request.getHeader(Parameters.ACCEPT)).thenReturn(mime_type); for (int i = 0; i < URIS_INVALID.size(); i++) { when(_request.getRequestURL()).thenReturn(new StringBuffer(URIS_INVALID.get(i))); _ld_servlet.doGet(_request, _response); verify(_response, atLeastOnce()).setStatus(HttpServletResponse.SC_NOT_FOUND); } } }