Search in sources :

Example 16 with Context

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

the class TestContext method testCxt9.

@Test(expected = ARQException.class)
public void testCxt9() {
    Context cxt = new Context();
    assertEquals(-1L, cxt.getLong(p1, -1L));
    cxt.set(p1, 1L);
    // Bad. Long for Integer.
    cxt.getInt(p1, -2);
}
Also used : Context(org.apache.jena.sparql.util.Context) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 17 with Context

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

the class TestContext method testCxt8.

@Test
public void testCxt8() {
    Context cxt = new Context();
    assertEquals(-1L, cxt.getLong(p1, -1L));
    cxt.set(p1, 1L);
    long x = cxt.getLong(p1, -2L);
    assertEquals(1L, x);
}
Also used : Context(org.apache.jena.sparql.util.Context) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 18 with Context

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

the class QueryEngineHTTP 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, QueryEngineHTTP engine) {
    if (engine.context == null)
        return;
    @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 behavioral options
        engine.setAllowCompression(serviceContext.isTrueOrUndef(Service.queryCompression));
        applyServiceTimeouts(engine, serviceContext);
        // Apply context-supplied client settings
        HttpClient client = serviceContext.get(Service.queryClient);
        if (client != null) {
            if (log.isDebugEnabled())
                log.debug("Using context-supplied HTTP client for endpoint URI {}", serviceURI);
            engine.setClient(client);
        }
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpContext(org.apache.http.protocol.HttpContext) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map)

Example 19 with Context

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

the class QueryEngineHTTP method makeHttpQuery.

private HttpQuery makeHttpQuery() {
    if (closed)
        throw new ARQException("HTTP execution already closed");
    HttpQuery httpQuery = new HttpQuery(service);
    httpQuery.merge(getServiceParams(service, context));
    httpQuery.addParam(HttpParams.pQuery, queryString);
    for (String dft : defaultGraphURIs) {
        httpQuery.addParam(HttpParams.pDefaultGraph, dft);
    }
    for (String name : namedGraphURIs) {
        httpQuery.addParam(HttpParams.pNamedGraph, name);
    }
    if (params != null)
        httpQuery.merge(params);
    httpQuery.setAllowCompression(allowCompression);
    // check for service context overrides
    if (context.isDefined(Service.serviceContext)) {
        Map<String, Context> servicesContext = context.get(Service.serviceContext);
        if (servicesContext.containsKey(service)) {
            Context serviceContext = servicesContext.get(service);
            if (serviceContext.isDefined(Service.queryClient))
                client = serviceContext.get(Service.queryClient);
        }
    }
    httpQuery.setClient(client);
    HttpClientContext hcc = (httpContext == null) ? null : HttpClientContext.adapt(httpContext);
    httpQuery.setContext(hcc);
    // Apply timeouts
    if (connectTimeout > 0)
        httpQuery.setConnectTimeout((int) connectTimeoutUnit.toMillis(connectTimeout));
    if (readTimeout > 0)
        httpQuery.setReadTimeout((int) readTimeoutUnit.toMillis(readTimeout));
    return httpQuery;
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) HttpContext(org.apache.http.protocol.HttpContext) ARQException(org.apache.jena.sparql.ARQException) HttpClientContext(org.apache.http.client.protocol.HttpClientContext)

Example 20 with Context

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

the class Service method configureQuery.

/**
     * Create and configure the HttpQuery object.
     * 
     * The parentContext is not modified but is used to create a new context
     * copy.
     * 
     * @param uri
     *            The uri of the endpoint
     * @param parentContext
     *            The initial context.
     * @param Query
     *            the Query to execute.
     * @return An HttpQuery configured as per the context.
     */
private static HttpQuery configureQuery(String uri, Context parentContext, Query query) {
    HttpQuery httpQuery = new HttpQuery(uri);
    Context context = new Context(parentContext);
    // add the context settings from the service context
    @SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) context.get(serviceContext);
    if (serviceContextMap != null) {
        Context serviceContext = serviceContextMap.get(uri);
        if (serviceContext != null)
            context.putAll(serviceContext);
    }
    // configure the query object.
    httpQuery.merge(QueryEngineHTTP.getServiceParams(uri, context));
    httpQuery.addParam(HttpParams.pQuery, query.toString());
    httpQuery.setAllowCompression(context.isTrueOrUndef(queryCompression));
    HttpClient client = context.get(queryClient);
    if (client != null)
        httpQuery.setClient(client);
    setAnyTimeouts(httpQuery, context);
    return httpQuery;
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClient(org.apache.http.client.HttpClient) HashMap(java.util.HashMap) Map(java.util.Map)

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