@Override protected final void perform(HttpAction action) { // OPTIONS if (action.request.getMethod().equals(HttpNames.METHOD_OPTIONS)) { // Share with update via SPARQL_Protocol. doOptions(action); return; } // GET if (action.request.getMethod().equals(HttpNames.METHOD_GET)) { executeWithParameter(action); return; } ContentType ct = FusekiLib.getContentType(action); // POST application/x-www-form-url // POST ?query= and no Content-Type if (ct == null || isHtmlForm(ct)) { // validation checked that if no Content-type, then its a POST with ?query= executeWithParameter(action); return; } // POST application/sparql-query if (matchContentType(ct, ctSPARQLQuery)) { executeBody(action); return; } ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Bad content type: " + ct.getContentType()); }
@Override protected void validate(HttpAction action) { HttpServletRequest request = action.request; if (HttpNames.METHOD_OPTIONS.equals(request.getMethod())) return; if (!HttpNames.METHOD_POST.equalsIgnoreCase(request.getMethod())) ServletOps.errorMethodNotAllowed("SPARQL Update : use POST"); ContentType ct = FusekiLib.getContentType(action); if (ct == null) ct = ctSPARQLUpdate; if (matchContentType(ctSPARQLUpdate, ct)) { String charset = request.getCharacterEncoding(); if (charset != null && !charset.equalsIgnoreCase(charsetUTF8)) ServletOps.errorBadRequest("Bad charset: " + charset); validate(action, paramsPOST); return; } if (isHtmlForm(ct)) { int x = countParamOccurences(request, paramUpdate) + countParamOccurences(request, paramRequest); if (x == 0) ServletOps.errorBadRequest("SPARQL Update: No 'update=' parameter"); if (x != 1) ServletOps.errorBadRequest("SPARQL Update: Multiple 'update=' parameters"); String requestStr = request.getParameter(paramUpdate); if (requestStr == null) requestStr = request.getParameter(paramRequest); if (requestStr == null) ServletOps.errorBadRequest("SPARQL Update: No update= in HTML form"); validate(action, paramsForm); return; } ServletOps.error( HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Must be " + contentTypeSPARQLUpdate + " or " + contentTypeHTMLForm + " (got " + ct.getContentType() + ")"); }
/** * Helper method for validating request. * * @param request HTTP request * @param params parameters in a collection of Strings */ protected void validateParams(HttpAction action, Collection<String> params) { HttpServletRequest request = action.request; ContentType ct = FusekiLib.getContentType(request); boolean mustHaveQueryParam = true; if (ct != null) { String incoming = ct.getContentType(); if (matchContentType(ctSPARQLQuery, ct)) { mustHaveQueryParam = false; // Drop through. } else if (matchContentType(ctHTMLForm, ct)) { // Nothing specific to do } else ServletOps.error(HttpSC.UNSUPPORTED_MEDIA_TYPE_415, "Unsupported: " + incoming); } // GET/POST of a form at this point. if (mustHaveQueryParam) { int N = countParamOccurences(request, paramQuery); if (N == 0) ServletOps.errorBadRequest("SPARQL Query: No 'query=' parameter"); if (N > 1) ServletOps.errorBadRequest("SPARQL Query: Multiple 'query=' parameters"); // application/sparql-query does not use a query param. String queryStr = request.getParameter(HttpNames.paramQuery); if (queryStr == null) ServletOps.errorBadRequest("SPARQL Query: No query specified (no 'query=' found)"); if (queryStr.isEmpty()) ServletOps.errorBadRequest("SPARQL Query: Empty query string"); } if (params != null) { Enumeration<String> en = request.getParameterNames(); for (; en.hasMoreElements(); ) { String name = en.nextElement(); if (!params.contains(name)) ServletOps.warning( action, "SPARQL Query: Unrecognize request parameter (ignored): " + name); } } }