Search in sources :

Example 1 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class SPARQL_ServletBase method doCommon.

// Common framework for handling HTTP requests
/**
     * Handles GET and POST requests.
     * @param request HTTP request
     * @param response HTTP response
     */
protected void doCommon(HttpServletRequest request, HttpServletResponse response) //throws ServletException, IOException
{
    try {
        long id = allocRequestId(request, response);
        // Lifecycle
        HttpAction action = allocHttpAction(id, request, response);
        // then add to doCommonWorker
        // work with HttpServletResponseTracker
        printRequest(action);
        action.setStartTime();
        response = action.response;
        initResponse(request, response);
        Context cxt = ARQ.getContext();
        try {
            execCommonWorker(action);
        } catch (QueryCancelledException ex) {
            // Also need the per query info ...
            String message = String.format("The query timed out (restricted to %s ms)", cxt.get(ARQ.queryTimeout));
            // Possibility :: response.setHeader("Retry-after", "600") ;    // 5 minutes
            responseSendError(response, HttpSC.SERVICE_UNAVAILABLE_503, message);
        } catch (ActionErrorException ex) {
            if (ex.exception != null)
                ex.exception.printStackTrace(System.err);
            // Log message done by printResponse in a moment.
            if (ex.message != null)
                responseSendError(response, ex.rc, ex.message);
            else
                responseSendError(response, ex.rc);
        } catch (RuntimeIOException ex) {
            log.warn(format("[%d] Runtime IO Exception (client left?) RC = %d", id, HttpSC.INTERNAL_SERVER_ERROR_500));
            responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage());
        } catch (Throwable ex) {
            // This should not happen.
            //ex.printStackTrace(System.err) ;
            log.warn(format("[%d] RC = %d : %s", id, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage()), ex);
            responseSendError(response, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage());
        }
        action.setFinishTime();
        printResponse(action);
        archiveHttpAction(action);
    } catch (Throwable th) {
        log.error("Internal error", th);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) QueryCancelledException(org.apache.jena.query.QueryCancelledException)

Example 2 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TupleTable method iterator.

public QueryIterator iterator() {
    SDBRequest request = new SDBRequest(store, null);
    String tableName = desc.getTableName();
    SQLBridge b = store.getSQLBridgeFactory().create(request, sqlTable, vars);
    b.build();
    try {
        String sqlStr = store.getSQLGenerator().generateSQL(request, b.getSqlNode());
        //System.out.println(sqlStr) ;
        ResultSetJDBC tableData = store.getConnection().execQuery(sqlStr);
        ExecutionContext execCxt = new ExecutionContext(new Context(), null, null, null);
        return b.assembleResults(tableData, BindingRoot.create(), execCxt);
    } catch (SQLException ex) {
        throw new SDBExceptionSQL(ex);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) SDBExceptionSQL(org.apache.jena.sdb.sql.SDBExceptionSQL) SQLException(java.sql.SQLException) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC) SDBRequest(org.apache.jena.sdb.core.SDBRequest)

Example 3 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class UpdateProcessRemoteBase method applyServiceConfig.

/**
     * <p>
     * Helper method which applies configuration from the Context to the query
     * engine if a service context exists for the given URI
     * </p>
     * <p>
     * Based off proposed patch for JENA-405 but modified to apply all relevant
     * configuration, this is in part also based off of the private
     * {@code configureQuery()} method of the {@link Service} class though it
     * omits parameter merging since that will be done automatically whenever
     * the {@link QueryEngineHTTP} instance makes a query for remote submission.
     * </p>
     * 
     * @param serviceURI
     *            Service URI
     */
private static void applyServiceConfig(String serviceURI, UpdateProcessRemoteBase engine) {
    @SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) engine.context.get(Service.serviceContext);
    if (serviceContextMap != null && serviceContextMap.containsKey(serviceURI)) {
        Context serviceContext = serviceContextMap.get(serviceURI);
        if (log.isDebugEnabled())
            log.debug("Endpoint URI {} has SERVICE Context: {} ", serviceURI, serviceContext);
        // Apply client settings
        HttpClient client = serviceContext.get(Service.queryClient);
        if (client != null) {
            if (log.isDebugEnabled())
                log.debug("Using context-supplied client for endpoint URI {}", serviceURI);
            engine.setClient(client);
        }
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpContext(org.apache.http.protocol.HttpContext) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map)

Example 4 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TestJsonLDReader method overrideAtContextTest.

/** Test using the jena Context mechanism to pass the jsonld "@context" */
@Test
public final void overrideAtContextTest() throws JsonGenerationException, IOException {
    // some jsonld using schema.org's URI as "@context"
    String jsonld = someSchemaDorOrgJsonld();
    // a subset of schema.org that can be used as @context for jsonld
    String jsonldContext = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
    // pass the jsonldContext to the read using a jena Context
    Context jenaCtx = new Context();
    Object jsonldContextAsMap = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8)));
    jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsMap);
    // read the jsonld, replacing its "@context"
    Dataset ds = jsonld2dataset(jsonld, jenaCtx);
    // check ds is correct
    assertJohnDoeIsOK(ds.getDefaultModel());
}
Also used : Context(org.apache.jena.sparql.util.Context) ByteArrayInputStream(java.io.ByteArrayInputStream) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test)

Example 5 with Context

use of org.apache.jena.sparql.util.Context in project jena by apache.

the class TestQueryIterSort method testCancelInterruptsExternalSortAfterStartingIteration.

@Test(expected = QueryCancelledException.class)
public void testCancelInterruptsExternalSortAfterStartingIteration() {
    assertEquals(0, iterator.getReturnedElementCount());
    Context context = new Context();
    context.set(ARQ.spillToDiskThreshold, 10L);
    ExecutionContext executionContext = new ExecutionContext(context, (Graph) null, (DatasetGraph) null, (OpExecutorFactory) null);
    QueryIterSort qIter = new QueryIterSort(iterator, comparator, executionContext);
    try {
        assertEquals(0, iterator.getReturnedElementCount());
        assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
        // throws a QueryCancelledException
        qIter.hasNext();
    } catch (QueryCancelledException e) {
        // expected
        assertEquals(26, iterator.getReturnedElementCount());
        // This is zero because QueryIteratorBase will call close() before throwing the QueryCancelledException.
        // It does this as a failsafe in case the user doesn't close the QueryIterator themselves.
        assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
        throw e;
    } finally {
        qIter.close();
    }
    assertEquals(0, DataBagExaminer.countTemporaryFiles(qIter.db));
}
Also used : Context(org.apache.jena.sparql.util.Context) SerializationContext(org.apache.jena.sparql.serializer.SerializationContext) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryIterSort(org.apache.jena.sparql.engine.iterator.QueryIterSort) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) QueryCancelledException(org.apache.jena.query.QueryCancelledException) Test(org.junit.Test)

Aggregations

Context (org.apache.jena.sparql.util.Context)52 Test (org.junit.Test)36 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)15 Map (java.util.Map)11 BaseTest (org.apache.jena.atlas.junit.BaseTest)11 SerializationContext (org.apache.jena.sparql.serializer.SerializationContext)11 HashMap (java.util.HashMap)10 QueryIterSort (org.apache.jena.sparql.engine.iterator.QueryIterSort)9 HttpClient (org.apache.http.client.HttpClient)7 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)7 Query (org.apache.jena.query.Query)5 QueryCancelledException (org.apache.jena.query.QueryCancelledException)4 SocketException (java.net.SocketException)3 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)3 HttpContext (org.apache.http.protocol.HttpContext)3 JsonObject (org.apache.jena.atlas.json.JsonObject)3 JsonString (org.apache.jena.atlas.json.JsonString)3 Node (org.apache.jena.graph.Node)3 Model (org.apache.jena.rdf.model.Model)3 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)3