@Test public void Select_as_JSON_via_Accept() throws ServletException, IOException { request.setParameter("query", "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"); request.addHeader("Accept", SPARQLServlet.SPARQL_RESULTS_JSON); servlet.service(request, response); assertTrue(response.getContentAsString().contains("head")); assertTrue(response.getContentAsString().contains("results")); assertTrue(response.getContentAsString().contains("literal")); assertEquals(SPARQLServlet.SPARQL_RESULTS_JSON, response.getContentType()); }
@Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; response.setDateHeader("Last-Modified", System.currentTimeMillis()); response.setCharacterEncoding("UTF-8"); String queryString = request.getParameter("query"); if (queryString == null) { response.sendError(400, "No query given"); return; } String normalized = queryString.toLowerCase(Locale.ENGLISH).replaceAll("\\s+", " "); if (!normalized.startsWith("ask") && !normalized.contains(" ask ")) { String limit = request.getParameter("limit"); String offset = request.getParameter("offset"); if (maxLimit != null) { if (limit != null) { limit = String.valueOf(Math.min(Integer.valueOf(limit), maxLimit)); } else { limit = maxLimit.toString(); } } if (limit != null) { queryString += "\nLIMIT " + limit; } if (offset != null) { queryString += "\nOFFSET " + offset; } } try { handleRequest(request, response, queryString); } catch (Exception e) { if (e.getMessage() != null) { logger.error(e.getMessage(), e); response.sendError(400, e.getMessage() + "\n\n" + request.getParameter("query")); } else { logger.error("Caught Exception", e); response.sendError(400); } } }
SPARQLServlet extends HttpServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; response.setDateHeader("Last-Modified", System.currentTimeMillis()); response.setCharacterEncoding("UTF-8"); String queryString = request.getParameter("query"); if (queryString == null) { response.sendError(400, "No query given"); return; } String normalized = queryString.toLowerCase(Locale.ENGLISH).replaceAll("\\s+", " "); if (!normalized.startsWith("ask") && !normalized.contains(" ask ")) { String limit = request.getParameter("limit"); String offset = request.getParameter("offset"); if (maxLimit != null) { if (limit != null) { limit = String.valueOf(Math.min(Integer.valueOf(limit), maxLimit)); } else { limit = maxLimit.toString(); } } if (limit != null) { queryString += "\nLIMIT " + limit; } if (offset != null) { queryString += "\nOFFSET " + offset; } } try { handleRequest(request, response, queryString); } catch (Exception e) { if (e.getMessage() != null) { logger.error(e.getMessage(), e); response.sendError(400, e.getMessage() + "\n\n" + request.getParameter("query")); } else { logger.error("Caught Exception", e); response.sendError(400); } } } }
SPARQLServlet extends HttpServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; response.setDateHeader("Last-Modified", System.currentTimeMillis()); response.setCharacterEncoding("UTF-8"); String queryString = request.getParameter("query"); if (queryString == null) { response.sendError(400, "No query given"); return; } String normalized = queryString.toLowerCase(Locale.ENGLISH).replaceAll("\\s+", " "); if (!normalized.startsWith("ask") && !normalized.contains(" ask ")) { String limit = request.getParameter("limit"); String offset = request.getParameter("offset"); if (maxLimit != null) { if (limit != null) { limit = String.valueOf(Math.min(Integer.valueOf(limit), maxLimit)); } else { limit = maxLimit.toString(); } } if (limit != null) { queryString += "\nLIMIT " + limit; } if (offset != null) { queryString += "\nOFFSET " + offset; } } try { handleRequest(request, response, queryString); } catch (Exception e) { if (e.getMessage() != null) { logger.error(e.getMessage(), e); response.sendError(400, e.getMessage() + "\n\n" + request.getParameter("query")); } else { logger.error("Caught Exception", e); response.sendError(400); } } } SPARQLServlet(Repository repository, Integer limit, Integer maxQueryTime);  SPARQLServlet(Repository repository, Integer limit);  SPARQLServlet(Repository repository);  SPARQLServlet(); }
SPARQLServlet extends HttpServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; response.setDateHeader("Last-Modified", System.currentTimeMillis()); response.setCharacterEncoding("UTF-8"); String queryString = request.getParameter("query"); if (queryString == null) { response.sendError(400, "No query given"); return; } String normalized = queryString.toLowerCase(Locale.ENGLISH).replaceAll("\\s+", " "); if (!normalized.startsWith("ask") && !normalized.contains(" ask ")) { String limit = request.getParameter("limit"); String offset = request.getParameter("offset"); if (maxLimit != null) { if (limit != null) { limit = String.valueOf(Math.min(Integer.valueOf(limit), maxLimit)); } else { limit = maxLimit.toString(); } } if (limit != null) { queryString += "\nLIMIT " + limit; } if (offset != null) { queryString += "\nOFFSET " + offset; } } try { handleRequest(request, response, queryString); } catch (Exception e) { if (e.getMessage() != null) { logger.error(e.getMessage(), e); response.sendError(400, e.getMessage() + "\n\n" + request.getParameter("query")); } else { logger.error("Caught Exception", e); response.sendError(400); } } } SPARQLServlet(Repository repository, Integer limit, Integer maxQueryTime);  SPARQLServlet(Repository repository, Integer limit);  SPARQLServlet(Repository repository);  SPARQLServlet(); @Override void init(ServletConfig config); @Override void service(ServletRequest req, ServletResponse res); }
SPARQLServlet extends HttpServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; response.setDateHeader("Last-Modified", System.currentTimeMillis()); response.setCharacterEncoding("UTF-8"); String queryString = request.getParameter("query"); if (queryString == null) { response.sendError(400, "No query given"); return; } String normalized = queryString.toLowerCase(Locale.ENGLISH).replaceAll("\\s+", " "); if (!normalized.startsWith("ask") && !normalized.contains(" ask ")) { String limit = request.getParameter("limit"); String offset = request.getParameter("offset"); if (maxLimit != null) { if (limit != null) { limit = String.valueOf(Math.min(Integer.valueOf(limit), maxLimit)); } else { limit = maxLimit.toString(); } } if (limit != null) { queryString += "\nLIMIT " + limit; } if (offset != null) { queryString += "\nOFFSET " + offset; } } try { handleRequest(request, response, queryString); } catch (Exception e) { if (e.getMessage() != null) { logger.error(e.getMessage(), e); response.sendError(400, e.getMessage() + "\n\n" + request.getParameter("query")); } else { logger.error("Caught Exception", e); response.sendError(400); } } } SPARQLServlet(Repository repository, Integer limit, Integer maxQueryTime);  SPARQLServlet(Repository repository, Integer limit);  SPARQLServlet(Repository repository);  SPARQLServlet(); @Override void init(ServletConfig config); @Override void service(ServletRequest req, ServletResponse res); static final String SPARQL_RESULTS_JSON; static final String SPARQL_RESULTS_XML; }