Search in sources :

Example 41 with Context

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

the class ActionBase method doCommon.

/**
     * Common framework for handling HTTP requests.
     * @param request
     * @param response
     */
protected void doCommon(HttpServletRequest request, HttpServletResponse response) {
    try {
        long id = allocRequestId(request, response);
        // Lifecycle
        HttpAction action = allocHttpAction(id, request, response);
        printRequest(action);
        action.setStartTime();
        // The response may be changed to a HttpServletResponseTracker
        response = action.response;
        initResponse(request, response);
        Context cxt = ARQ.getContext();
        try {
            execCommonWorker(action);
        } catch (QueryCancelledException ex) {
            // To put in the action timeout, need (1) global, (2) dataset and (3) protocol settings.
            // See
            //    global -- cxt.get(ARQ.queryTimeout) 
            //    dataset -- dataset.getContect(ARQ.queryTimeout)
            //    protocol -- SPARQL_Query.setAnyTimeouts
            String message = String.format("Query timed out");
            // Possibility :: response.setHeader("Retry-after", "600") ;    // 5 minutes
            ServletOps.responseSendError(response, HttpSC.SERVICE_UNAVAILABLE_503, message);
        } catch (ActionErrorException ex) {
            if (ex.getCause() != null)
                ex.getCause().printStackTrace(System.err);
            // Log message done by printResponse in a moment.
            if (ex.getMessage() != null)
                ServletOps.responseSendError(response, ex.getRC(), ex.getMessage());
            else
                ServletOps.responseSendError(response, ex.getRC());
        } catch (RuntimeIOException ex) {
            log.warn(format("[%d] Runtime IO Exception (client left?) RC = %d : %s", id, HttpSC.INTERNAL_SERVER_ERROR_500, ex.getMessage()), ex);
            ServletOps.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);
            ServletOps.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 42 with Context

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

the class TestJsonLDReader method overrideAtContextByURITest.

/** Not really useful, but one can replace the @context by a URI: in this case, this URI is used when expanding the json
     * (letting JSON-LD java API taking care of downloading the context. */
// well, as of this writing, it doesn't work, as we get a "loading remote context failed"
// But it is about the replacing URI, not the replaced one, showing that the mechanism does work
@Test
public final void overrideAtContextByURITest() throws JsonGenerationException, IOException {
    // some jsonld using a (fake) pseudo.schema.org's URI as "@context"
    String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://pseudo.schema.org/\"}";
    // a subset of schema.org that can be used as @context for jsonld
    String jsonldContext = "\"http://schema.org\"";
    // pass the jsonldContext to the read using a jena Context
    Context jenaCtx = new Context();
    Object jsonldContextAsObject = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8)));
    jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsObject);
    try {
        // read the jsonld, replacing its "@context"
        Dataset ds = jsonld2dataset(jsonld, jenaCtx);
        // check ds is correct
        assertJohnDoeIsOK(ds.getDefaultModel());
    } catch (RiotException e) {
    // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/
    // There's a line printed anyway
    // e.printStackTrace();
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) ByteArrayInputStream(java.io.ByteArrayInputStream) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test)

Example 43 with Context

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

the class TestAuth method query_with_auth_11.

@Test
public void query_with_auth_11() {
    QueryEngineHTTP qe = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(authServiceQuery, "ASK { }");
    // Auth credentials for valid user with correct password and scoped
    // to base URI of the actual service URL
    // Provided via Service Context and its associated authenticator
    Map<String, Context> serviceContext = new HashMap<>();
    Context authContext = new Context();
    HttpClient client = withBasicAuth(ANY, "allowed", "password");
    authContext.put(Service.queryClient, client);
    serviceContext.put(authServiceQuery, authContext);
    qe.getContext().put(Service.serviceContext, serviceContext);
    Assert.assertTrue(qe.execAsk());
}
Also used : Context(org.apache.jena.sparql.util.Context) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) HashMap(java.util.HashMap) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Example 44 with Context

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

the class TestAuth method query_with_auth_10.

@Test
public void query_with_auth_10() {
    QueryEngineHTTP qe = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(authServiceQuery, "ASK { }");
    // Auth credentials for valid user with correct password and scoped
    // to correct URI
    // Provided via Service Context and its associated authenticator
    Map<String, Context> serviceContext = new HashMap<>();
    Context authContext = new Context();
    HttpClient client = withBasicAuth(ANY, "allowed", "password");
    authContext.put(Service.queryClient, client);
    serviceContext.put(authServiceQuery, authContext);
    qe.getContext().put(Service.serviceContext, serviceContext);
    Assert.assertTrue(qe.execAsk());
}
Also used : Context(org.apache.jena.sparql.util.Context) QueryEngineHTTP(org.apache.jena.sparql.engine.http.QueryEngineHTTP) HashMap(java.util.HashMap) HttpClient(org.apache.http.client.HttpClient) Test(org.junit.Test)

Example 45 with Context

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

the class ProcEval method build.

// ----
public static Procedure build(Node procId, PropFuncArg subjArg, PropFuncArg objArg, ExecutionContext execCxt) {
    Context context = execCxt.getContext();
    PropertyFunctionRegistry reg = PropertyFunctionRegistry.chooseRegistry(context);
    PropertyFunctionFactory f = reg.get(procId.getURI());
    PropertyFunction pf = f.create(procId.getURI());
    pf.build(subjArg, procId, objArg, execCxt);
    //Make wrapper
    return new ProcedurePF(pf, subjArg, procId, objArg);
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext)

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