@Test public void validSelectQueriesWithAcceptParam() throws Exception { for (final String query : _sparql_select) { for (final String accept : _accept_select_header) { final File tmp = tmpFile(); final ServletOutputStream servletOutputStream = new StubServletOutputStream(tmp); final HttpServletRequest request = mock(HttpServletRequest.class); when(request.getParameter(Parameters.QUERY)).thenReturn(query); when(request.getParameter(Parameters.ACCEPT)).thenReturn(accept); when(request.getMethod()).thenReturn(Methods.POST); final HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(servletOutputStream); _classUnderTest.service(request, response); servletOutputStream.close(); final TupleQueryResult res = QueryResultIO.parse(new FileInputStream(tmp), TupleQueryResultFormat.forMIMEType(accept)); assertTrue("query: " + query + ", accept: " + accept, _queries2count.get(query) > 0 ? res.hasNext() : !res.hasNext()); } } }
@Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final String accept = parseAcceptHeader(request); final String query = getParameterValue(request, Parameters.QUERY), update = getParameterValue(request, Parameters.UPDATE); if (isNullOrEmptyString(query) && isNullOrEmptyString(update)) { sendError( request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00045_MISSING_QUERY_OR_UPDATE_PARAM); return; } RepositoryConnection connection = null; @SuppressWarnings("rawtypes") QueryResult resultset = null; try { final 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( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); if (query != null) { final Query parsedQuery = connection.prepareQuery(QueryLanguage.SPARQL, query, parseBaseURI(request, null)); if (accept.equals(MimeTypes.TEXT_HTML)) { if (parsedQuery instanceof BooleanQuery) { request.setAttribute("isThereAResult", true); request.setAttribute("booleanQuery", true); request.setAttribute("query", query); request.setAttribute("result", ((BooleanQuery) parsedQuery).evaluate()); forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof TupleQuery) { resultset = ((TupleQuery) parsedQuery).evaluate(); final TupleQueryResult res = (TupleQueryResult) resultset; request.setAttribute("tupleQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { request.setAttribute("isThereAResult", true); request.setAttribute("tupleResult", resultset); request.setAttribute("wrappedResult", new Iterator<BindingSet>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public BindingSet next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof GraphQuery) { resultset = ((GraphQuery) parsedQuery).evaluate(); request.setAttribute("graphQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { final GraphQueryResult res = (GraphQueryResult) resultset; request.setAttribute("isThereAResult", true); request.setAttribute("graphResult", resultset); request.setAttribute("wrappedResult", new Iterator<Statement>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public Statement next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } } else { final Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 7); response.setHeader("Cache-Control", "public"); response.setHeader("Expires", _rfc822Formatters.get().format(c.getTime())); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Vary", "Accept"); if (parsedQuery instanceof BooleanQuery) { BooleanQueryResultFormat format = BooleanQueryResultFormat.forMIMEType(accept, BooleanQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); QueryResultIO.writeBoolean( ((BooleanQuery) parsedQuery).evaluate(), format, response.getOutputStream()); } else if (parsedQuery instanceof TupleQuery) { TupleQueryResultFormat format = TupleQueryResultFormat.forMIMEType(accept, TupleQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); resultset = ((TupleQuery) parsedQuery).evaluate(); QueryResultIO.write( (TupleQueryResult) resultset, format, response.getOutputStream()); } else if (parsedQuery instanceof GraphQuery) { RDFFormat format = RDFFormat.forMIMEType(accept, RDFFormat.RDFXML); response.setContentType(format.getDefaultMIMEType()); resultset = ((GraphQuery) parsedQuery).evaluate(); QueryResultIO.write( (GraphQueryResult) resultset, format, response.getOutputStream()); } response.setStatus(HttpServletResponse.SC_OK); } } else if (update != null) { connection.prepareUpdate(QueryLanguage.SPARQL, update, parseBaseURI(request)).execute(); response.setStatus(HttpServletResponse.SC_OK); } } catch (final QueryResultHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final RDFHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final IOException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (QueryEvaluationException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (RepositoryException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (MalformedQueryException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (UpdateExecutionException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (final Exception e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); e.printStackTrace(); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } finally { if (resultset != null) { try { resultset.close();} catch (final Exception ignore) {}}; if (connection != null) { try { connection.close();} catch (final Exception ignore) {}}; } }
SPARQLServlet extends AbstractCumulusServlet { @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final String accept = parseAcceptHeader(request); final String query = getParameterValue(request, Parameters.QUERY), update = getParameterValue(request, Parameters.UPDATE); if (isNullOrEmptyString(query) && isNullOrEmptyString(update)) { sendError( request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00045_MISSING_QUERY_OR_UPDATE_PARAM); return; } RepositoryConnection connection = null; @SuppressWarnings("rawtypes") QueryResult resultset = null; try { final 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( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); if (query != null) { final Query parsedQuery = connection.prepareQuery(QueryLanguage.SPARQL, query, parseBaseURI(request, null)); if (accept.equals(MimeTypes.TEXT_HTML)) { if (parsedQuery instanceof BooleanQuery) { request.setAttribute("isThereAResult", true); request.setAttribute("booleanQuery", true); request.setAttribute("query", query); request.setAttribute("result", ((BooleanQuery) parsedQuery).evaluate()); forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof TupleQuery) { resultset = ((TupleQuery) parsedQuery).evaluate(); final TupleQueryResult res = (TupleQueryResult) resultset; request.setAttribute("tupleQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { request.setAttribute("isThereAResult", true); request.setAttribute("tupleResult", resultset); request.setAttribute("wrappedResult", new Iterator<BindingSet>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public BindingSet next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof GraphQuery) { resultset = ((GraphQuery) parsedQuery).evaluate(); request.setAttribute("graphQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { final GraphQueryResult res = (GraphQueryResult) resultset; request.setAttribute("isThereAResult", true); request.setAttribute("graphResult", resultset); request.setAttribute("wrappedResult", new Iterator<Statement>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public Statement next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } } else { final Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 7); response.setHeader("Cache-Control", "public"); response.setHeader("Expires", _rfc822Formatters.get().format(c.getTime())); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Vary", "Accept"); if (parsedQuery instanceof BooleanQuery) { BooleanQueryResultFormat format = BooleanQueryResultFormat.forMIMEType(accept, BooleanQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); QueryResultIO.writeBoolean( ((BooleanQuery) parsedQuery).evaluate(), format, response.getOutputStream()); } else if (parsedQuery instanceof TupleQuery) { TupleQueryResultFormat format = TupleQueryResultFormat.forMIMEType(accept, TupleQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); resultset = ((TupleQuery) parsedQuery).evaluate(); QueryResultIO.write( (TupleQueryResult) resultset, format, response.getOutputStream()); } else if (parsedQuery instanceof GraphQuery) { RDFFormat format = RDFFormat.forMIMEType(accept, RDFFormat.RDFXML); response.setContentType(format.getDefaultMIMEType()); resultset = ((GraphQuery) parsedQuery).evaluate(); QueryResultIO.write( (GraphQueryResult) resultset, format, response.getOutputStream()); } response.setStatus(HttpServletResponse.SC_OK); } } else if (update != null) { connection.prepareUpdate(QueryLanguage.SPARQL, update, parseBaseURI(request)).execute(); response.setStatus(HttpServletResponse.SC_OK); } } catch (final QueryResultHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final RDFHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final IOException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (QueryEvaluationException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (RepositoryException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (MalformedQueryException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (UpdateExecutionException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (final Exception e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); e.printStackTrace(); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } finally { if (resultset != null) { try { resultset.close();} catch (final Exception ignore) {}}; if (connection != null) { try { connection.close();} catch (final Exception ignore) {}}; } } }
SPARQLServlet extends AbstractCumulusServlet { @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final String accept = parseAcceptHeader(request); final String query = getParameterValue(request, Parameters.QUERY), update = getParameterValue(request, Parameters.UPDATE); if (isNullOrEmptyString(query) && isNullOrEmptyString(update)) { sendError( request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00045_MISSING_QUERY_OR_UPDATE_PARAM); return; } RepositoryConnection connection = null; @SuppressWarnings("rawtypes") QueryResult resultset = null; try { final 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( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); if (query != null) { final Query parsedQuery = connection.prepareQuery(QueryLanguage.SPARQL, query, parseBaseURI(request, null)); if (accept.equals(MimeTypes.TEXT_HTML)) { if (parsedQuery instanceof BooleanQuery) { request.setAttribute("isThereAResult", true); request.setAttribute("booleanQuery", true); request.setAttribute("query", query); request.setAttribute("result", ((BooleanQuery) parsedQuery).evaluate()); forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof TupleQuery) { resultset = ((TupleQuery) parsedQuery).evaluate(); final TupleQueryResult res = (TupleQueryResult) resultset; request.setAttribute("tupleQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { request.setAttribute("isThereAResult", true); request.setAttribute("tupleResult", resultset); request.setAttribute("wrappedResult", new Iterator<BindingSet>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public BindingSet next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof GraphQuery) { resultset = ((GraphQuery) parsedQuery).evaluate(); request.setAttribute("graphQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { final GraphQueryResult res = (GraphQueryResult) resultset; request.setAttribute("isThereAResult", true); request.setAttribute("graphResult", resultset); request.setAttribute("wrappedResult", new Iterator<Statement>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public Statement next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } } else { final Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 7); response.setHeader("Cache-Control", "public"); response.setHeader("Expires", _rfc822Formatters.get().format(c.getTime())); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Vary", "Accept"); if (parsedQuery instanceof BooleanQuery) { BooleanQueryResultFormat format = BooleanQueryResultFormat.forMIMEType(accept, BooleanQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); QueryResultIO.writeBoolean( ((BooleanQuery) parsedQuery).evaluate(), format, response.getOutputStream()); } else if (parsedQuery instanceof TupleQuery) { TupleQueryResultFormat format = TupleQueryResultFormat.forMIMEType(accept, TupleQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); resultset = ((TupleQuery) parsedQuery).evaluate(); QueryResultIO.write( (TupleQueryResult) resultset, format, response.getOutputStream()); } else if (parsedQuery instanceof GraphQuery) { RDFFormat format = RDFFormat.forMIMEType(accept, RDFFormat.RDFXML); response.setContentType(format.getDefaultMIMEType()); resultset = ((GraphQuery) parsedQuery).evaluate(); QueryResultIO.write( (GraphQueryResult) resultset, format, response.getOutputStream()); } response.setStatus(HttpServletResponse.SC_OK); } } else if (update != null) { connection.prepareUpdate(QueryLanguage.SPARQL, update, parseBaseURI(request)).execute(); response.setStatus(HttpServletResponse.SC_OK); } } catch (final QueryResultHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final RDFHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final IOException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (QueryEvaluationException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (RepositoryException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (MalformedQueryException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (UpdateExecutionException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (final Exception e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); e.printStackTrace(); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } finally { if (resultset != null) { try { resultset.close();} catch (final Exception ignore) {}}; if (connection != null) { try { connection.close();} catch (final Exception ignore) {}}; } }  }
SPARQLServlet extends AbstractCumulusServlet { @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final String accept = parseAcceptHeader(request); final String query = getParameterValue(request, Parameters.QUERY), update = getParameterValue(request, Parameters.UPDATE); if (isNullOrEmptyString(query) && isNullOrEmptyString(update)) { sendError( request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00045_MISSING_QUERY_OR_UPDATE_PARAM); return; } RepositoryConnection connection = null; @SuppressWarnings("rawtypes") QueryResult resultset = null; try { final 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( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); if (query != null) { final Query parsedQuery = connection.prepareQuery(QueryLanguage.SPARQL, query, parseBaseURI(request, null)); if (accept.equals(MimeTypes.TEXT_HTML)) { if (parsedQuery instanceof BooleanQuery) { request.setAttribute("isThereAResult", true); request.setAttribute("booleanQuery", true); request.setAttribute("query", query); request.setAttribute("result", ((BooleanQuery) parsedQuery).evaluate()); forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof TupleQuery) { resultset = ((TupleQuery) parsedQuery).evaluate(); final TupleQueryResult res = (TupleQueryResult) resultset; request.setAttribute("tupleQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { request.setAttribute("isThereAResult", true); request.setAttribute("tupleResult", resultset); request.setAttribute("wrappedResult", new Iterator<BindingSet>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public BindingSet next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof GraphQuery) { resultset = ((GraphQuery) parsedQuery).evaluate(); request.setAttribute("graphQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { final GraphQueryResult res = (GraphQueryResult) resultset; request.setAttribute("isThereAResult", true); request.setAttribute("graphResult", resultset); request.setAttribute("wrappedResult", new Iterator<Statement>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public Statement next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } } else { final Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 7); response.setHeader("Cache-Control", "public"); response.setHeader("Expires", _rfc822Formatters.get().format(c.getTime())); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Vary", "Accept"); if (parsedQuery instanceof BooleanQuery) { BooleanQueryResultFormat format = BooleanQueryResultFormat.forMIMEType(accept, BooleanQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); QueryResultIO.writeBoolean( ((BooleanQuery) parsedQuery).evaluate(), format, response.getOutputStream()); } else if (parsedQuery instanceof TupleQuery) { TupleQueryResultFormat format = TupleQueryResultFormat.forMIMEType(accept, TupleQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); resultset = ((TupleQuery) parsedQuery).evaluate(); QueryResultIO.write( (TupleQueryResult) resultset, format, response.getOutputStream()); } else if (parsedQuery instanceof GraphQuery) { RDFFormat format = RDFFormat.forMIMEType(accept, RDFFormat.RDFXML); response.setContentType(format.getDefaultMIMEType()); resultset = ((GraphQuery) parsedQuery).evaluate(); QueryResultIO.write( (GraphQueryResult) resultset, format, response.getOutputStream()); } response.setStatus(HttpServletResponse.SC_OK); } } else if (update != null) { connection.prepareUpdate(QueryLanguage.SPARQL, update, parseBaseURI(request)).execute(); response.setStatus(HttpServletResponse.SC_OK); } } catch (final QueryResultHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final RDFHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final IOException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (QueryEvaluationException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (RepositoryException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (MalformedQueryException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (UpdateExecutionException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (final Exception e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); e.printStackTrace(); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } finally { if (resultset != null) { try { resultset.close();} catch (final Exception ignore) {}}; if (connection != null) { try { connection.close();} catch (final Exception ignore) {}}; } }  @Override void service(final HttpServletRequest request, final HttpServletResponse response); }
SPARQLServlet extends AbstractCumulusServlet { @Override public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { final String accept = parseAcceptHeader(request); final String query = getParameterValue(request, Parameters.QUERY), update = getParameterValue(request, Parameters.UPDATE); if (isNullOrEmptyString(query) && isNullOrEmptyString(update)) { sendError( request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00045_MISSING_QUERY_OR_UPDATE_PARAM); return; } RepositoryConnection connection = null; @SuppressWarnings("rawtypes") QueryResult resultset = null; try { final 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( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG); return; } connection = repository.getConnection(); if (query != null) { final Query parsedQuery = connection.prepareQuery(QueryLanguage.SPARQL, query, parseBaseURI(request, null)); if (accept.equals(MimeTypes.TEXT_HTML)) { if (parsedQuery instanceof BooleanQuery) { request.setAttribute("isThereAResult", true); request.setAttribute("booleanQuery", true); request.setAttribute("query", query); request.setAttribute("result", ((BooleanQuery) parsedQuery).evaluate()); forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof TupleQuery) { resultset = ((TupleQuery) parsedQuery).evaluate(); final TupleQueryResult res = (TupleQueryResult) resultset; request.setAttribute("tupleQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { request.setAttribute("isThereAResult", true); request.setAttribute("tupleResult", resultset); request.setAttribute("wrappedResult", new Iterator<BindingSet>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public BindingSet next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } else if (parsedQuery instanceof GraphQuery) { resultset = ((GraphQuery) parsedQuery).evaluate(); request.setAttribute("graphQuery", true); request.setAttribute("query", query); if (resultset.hasNext()) { final GraphQueryResult res = (GraphQueryResult) resultset; request.setAttribute("isThereAResult", true); request.setAttribute("graphResult", resultset); request.setAttribute("wrappedResult", new Iterator<Statement>() { @Override public boolean hasNext() { try { return res.hasNext(); } catch (Exception exception) { return false; } } @Override public Statement next() { try { return res.next(); } catch (Exception exception) { return null; } } @Override public void remove() { } }); } forwardTo(request, response, "query.vm"); } } else { final Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 7); response.setHeader("Cache-Control", "public"); response.setHeader("Expires", _rfc822Formatters.get().format(c.getTime())); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Vary", "Accept"); if (parsedQuery instanceof BooleanQuery) { BooleanQueryResultFormat format = BooleanQueryResultFormat.forMIMEType(accept, BooleanQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); QueryResultIO.writeBoolean( ((BooleanQuery) parsedQuery).evaluate(), format, response.getOutputStream()); } else if (parsedQuery instanceof TupleQuery) { TupleQueryResultFormat format = TupleQueryResultFormat.forMIMEType(accept, TupleQueryResultFormat.SPARQL); response.setContentType(format.getDefaultMIMEType()); resultset = ((TupleQuery) parsedQuery).evaluate(); QueryResultIO.write( (TupleQueryResult) resultset, format, response.getOutputStream()); } else if (parsedQuery instanceof GraphQuery) { RDFFormat format = RDFFormat.forMIMEType(accept, RDFFormat.RDFXML); response.setContentType(format.getDefaultMIMEType()); resultset = ((GraphQuery) parsedQuery).evaluate(); QueryResultIO.write( (GraphQueryResult) resultset, format, response.getOutputStream()); } response.setStatus(HttpServletResponse.SC_OK); } } else if (update != null) { connection.prepareUpdate(QueryLanguage.SPARQL, update, parseBaseURI(request)).execute(); response.setStatus(HttpServletResponse.SC_OK); } } catch (final QueryResultHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final RDFHandlerException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final IOException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (QueryEvaluationException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (RepositoryException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (MalformedQueryException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (UpdateExecutionException e) { _log.debug(MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, MessageCatalog._00115_WEB_MODULE_REQUEST_NOT_VALID, e); } catch (final CumulusStoreException e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE_MSG, e); } catch (final Exception e) { _log.error(MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); e.printStackTrace(); sendError( request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageCatalog._00025_CUMULUS_SYSTEM_INTERNAL_FAILURE, e); } finally { if (resultset != null) { try { resultset.close();} catch (final Exception ignore) {}}; if (connection != null) { try { connection.close();} catch (final Exception ignore) {}}; } }  @Override void service(final HttpServletRequest request, final HttpServletResponse response);  }