Ejemplo n.º 1
0
  protected void execute(String queryString, HttpAction action) {
    String queryStringLog = ServletOps.formatForLog(queryString);
    if (action.verbose) action.log.info(format("[%d] Query = \n%s", action.id, queryString));
    else action.log.info(format("[%d] Query = %s", action.id, queryStringLog));

    Query query = null;
    try {
      // NB syntax is ARQ (a superset of SPARQL)
      query = QueryFactory.create(queryString, QueryParseBase, Syntax.syntaxARQ);
      queryStringLog = formatForLog(query);
      validateQuery(action, query);
    } catch (ActionErrorException ex) {
      throw ex;
    } catch (QueryParseException ex) {
      ServletOps.errorBadRequest(
          "Parse error: \n" + queryString + "\n\r" + messageForQueryException(ex));
    }
    // Should not happen.
    catch (QueryException ex) {
      ServletOps.errorBadRequest("Error: \n" + queryString + "\n\r" + ex.getMessage());
    }

    // Assumes finished whole thing by end of sendResult.
    try {
      action.beginRead();
      Dataset dataset = decideDataset(action, query, queryStringLog);
      try (QueryExecution qExec = createQueryExecution(query, dataset); ) {
        SPARQLResult result = executeQuery(action, qExec, query, queryStringLog);
        // Deals with exceptions itself.
        sendResults(action, result, query.getPrologue());
      }
    } catch (QueryParseException ex) {
      // Late stage static error (e.g. bad fixed Lucene query string).
      ServletOps.errorBadRequest(
          "Query parse error: \n" + queryString + "\n\r" + messageForQueryException(ex));
    } catch (QueryCancelledException ex) {
      // Additional counter information.
      incCounter(action.getEndpoint().getCounters(), QueryTimeouts);
      throw ex;
    } finally {
      action.endRead();
    }
  }
Ejemplo n.º 2
0
  @Override
  protected void runTestForReal() throws Throwable {
    Query query = null;
    try {
      try {
        query = queryFromTestItem(testItem);
      } catch (QueryException qEx) {
        query = null;
        qEx.printStackTrace(System.err);
        fail("Parse failure: " + qEx.getMessage());
        throw qEx;
      }

      Dataset dataset = setUpDataset(query, testItem);
      if (dataset == null && !doesQueryHaveDataset(query)) fail("No dataset for query");

      QueryExecution qe = null;

      if (dataset == null) qe = QueryExecutionFactory.create(query, queryFileManager);
      else qe = QueryExecutionFactory.create(query, dataset);

      try {
        if (query.isSelectType()) runTestSelect(query, qe);
        else if (query.isConstructType()) runTestConstruct(query, qe);
        else if (query.isDescribeType()) runTestDescribe(query, qe);
        else if (query.isAskType()) runTestAsk(query, qe);
      } finally {
        qe.close();
      }
    } catch (IOException ioEx) {
      // log.debug("IOException: ",ioEx) ;
      fail("IOException: " + ioEx.getMessage());
      throw ioEx;
    } catch (NullPointerException ex) {
      throw ex;
    } catch (Exception ex) {
      ex.printStackTrace(System.err);
      fail("Exception: " + ex.getClass().getName() + ": " + ex.getMessage());
    }
  }
Ejemplo n.º 3
0
  /**
   * Tests if errors are thrown when some mandatory attributes are missing in a <http:request/>,
   * <http:body/> or <http:multipart/>.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void errors() throws IOException {

    // Incorrect requests
    final List<byte[]> falseReqs = new ArrayList<>();

    // Request without method
    final byte[] falseReq1 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'/>");
    falseReqs.add(falseReq1);

    // Request with send-authorization and no credentials
    final byte[] falseReq2 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='GET' href='"
                + REST_ROOT
                + "' "
                + "send-authorization='true'/>");
    falseReqs.add(falseReq2);

    // Request with send-authorization and only username
    final byte[] falseReq3 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='GET' href='"
                + REST_ROOT
                + "' "
                + "send-authorization='true' username='******'/>");
    falseReqs.add(falseReq3);

    // Request with body that has no media-type
    final byte[] falseReq4 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='POST' href='"
                + REST_ROOT
                + "'>"
                + "<http:body>"
                + "</http:body>"
                + "</http:request>");
    falseReqs.add(falseReq4);

    // Request with multipart that has no media-type
    final byte[] falseReq5 =
        token(
            "<http:request method='POST' "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'>"
                + "<http:multipart boundary='xxx'>"
                + "</http:multipart>"
                + "</http:request>");
    falseReqs.add(falseReq5);

    // Request with multipart with part that has a body without media-type
    final byte[] falseReq6 =
        token(
            "<http:request method='POST' "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='"
                + REST_ROOT
                + "'>"
                + "<http:multipart boundary='xxx'>"
                + "<http:header name='hdr1' value-='val1'/>"
                + "<http:body media-type='text/plain'>"
                + "Part1"
                + "</http:body>"
                + "<http:header name='hdr1' value-='val1'/>"
                + "<http:body>"
                + "Part1"
                + "</http:body>"
                + "</http:multipart>"
                + "</http:request>");
    falseReqs.add(falseReq6);

    // Request with schema different from http
    final byte[] falseReq7 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "href='ftp://basex.org'/>");
    falseReqs.add(falseReq7);

    // Request with content and method which must be empty
    final byte[] falseReq8 =
        token(
            "<http:request "
                + "xmlns:http='http://expath.org/ns/http-client' "
                + "method='DELETE' href='"
                + REST_ROOT
                + "'>"
                + "<http:body media-type='text/plain'>"
                + "</http:body>"
                + "</http:request>");
    falseReqs.add(falseReq8);

    for (final byte[] falseReq : falseReqs) {
      final DBNode dbNode = new DBNode(new IOContent(falseReq));
      try {
        final HttpRequestParser rp = new HttpRequestParser(null);
        rp.parse(dbNode.children().next(), null);
        fail("Exception not thrown");
      } catch (final QueryException ex) {
        assertTrue(ex.getMessage().contains(ErrType.HC.toString()));
      }
    }
  }