예제 #1
0
  @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());
  }
예제 #2
0
  /**
   * 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);
      }
    }
  }