Search in sources :

Example 1 with HttpRequestModifier

use of org.apache.jena.http.sys.HttpRequestModifier in project jena by apache.

the class TestAuthRemote method auth_service_tuning_2_HttpRequestModifer.

@Test
public void auth_service_tuning_2_HttpRequestModifer() {
    // As HttpRequestModifer
    HttpRequestModifier mods = (params, headers) -> headers.put(HttpNames.hAuthorization, HttpLib.basicAuth(user, password));
    ARQ.getContext().put(ARQ.httpRequestModifer, mods);
    try {
        try (RDFLink link = RDFLinkHTTP.newBuilder().destination(env.datasetURL()).build()) {
            link.update("INSERT DATA { <x:s> <x:p> <x:z> }");
            boolean b = link.queryAsk("ASK{ <x:s> <x:p> <x:z> }");
            assertTrue(b);
        }
    } finally {
        // clear up
        ARQ.getContext().remove(ARQ.httpRequestModifer);
    }
}
Also used : DatasetGraphFactory(org.apache.jena.sparql.core.DatasetGraphFactory) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) BeforeClass(org.junit.BeforeClass) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) RDFLinkFactory(org.apache.jena.rdflink.RDFLinkFactory) UpdateExecHTTP(org.apache.jena.sparql.exec.http.UpdateExecHTTP) RDFLink(org.apache.jena.rdflink.RDFLink) DSP(org.apache.jena.sparql.exec.http.DSP) Graph(org.apache.jena.graph.Graph) HttpNames(org.apache.jena.riot.web.HttpNames) QueryExecHTTP(org.apache.jena.sparql.exec.http.QueryExecHTTP) HttpTest.expect401(org.apache.jena.fuseki.test.HttpTest.expect401) EnvTest(org.apache.jena.test.conn.EnvTest) Before(org.junit.Before) AfterClass(org.junit.AfterClass) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Triple(org.apache.jena.graph.Triple) Test(org.junit.Test) GSP(org.apache.jena.sparql.exec.http.GSP) SSE(org.apache.jena.sparql.sse.SSE) ARQ(org.apache.jena.query.ARQ) RegistryRequestModifier(org.apache.jena.http.sys.RegistryRequestModifier) QueryExec(org.apache.jena.sparql.exec.QueryExec) GraphFactory(org.apache.jena.sparql.graph.GraphFactory) RDFLink(org.apache.jena.rdflink.RDFLink) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) EnvTest(org.apache.jena.test.conn.EnvTest) Test(org.junit.Test)

Example 2 with HttpRequestModifier

use of org.apache.jena.http.sys.HttpRequestModifier in project jena by apache.

the class TestService method service_query_modified_registry.

@Test
public void service_query_modified_registry() {
    DatasetGraph dsg = env.dsg();
    dsg.executeWrite(() -> dsg.add(SSE.parseQuad("(_ :s :p :o)")));
    String queryString = "SELECT * { SERVICE <" + SERVICE + "> { ?s ?p ?o }} ";
    RDFLink link = RDFLinkFactory.connect(localDataset());
    // RequestModifer that sets a flag to show it has been run.
    AtomicInteger COUNTER = new AtomicInteger(0);
    HttpRequestModifier testModifier = (Params params, Map<String, String> httpHeaders) -> {
        COUNTER.incrementAndGet();
    };
    runWithModifier(SERVICE, testModifier, () -> {
        // Connect to local, unused, permanently empty dataset
        try (QueryExec qExec = QueryExec.dataset(localDataset()).query(queryString).build()) {
            RowSet rs = qExec.select();
            long x = Iter.count(rs);
            assertEquals(1, x);
        }
    });
    assertEquals("Modifier did not run", 1, COUNTER.get());
}
Also used : QueryExec(org.apache.jena.sparql.exec.QueryExec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowSet(org.apache.jena.sparql.exec.RowSet) RDFLink(org.apache.jena.rdflink.RDFLink) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) EnvTest(org.apache.jena.test.conn.EnvTest) Test(org.junit.Test)

Example 3 with HttpRequestModifier

use of org.apache.jena.http.sys.HttpRequestModifier in project jena by apache.

the class TestService method service_query_modified_cxt.

@Test
public void service_query_modified_cxt() {
    DatasetGraph dsg = env.dsg();
    String queryString = "SELECT * { SERVICE <" + SERVICE + "> { BIND (123 AS ?X) } }";
    // RequestModifer that sets a flag to show it has been run.
    AtomicInteger COUNTER = new AtomicInteger(0);
    HttpRequestModifier testModifier = (Params params, Map<String, String> httpHeaders) -> {
        COUNTER.incrementAndGet();
    };
    DatasetGraph localdsg = localDataset();
    localdsg.getContext().put(ARQ.httpRequestModifer, testModifier);
    try (RDFLink link = RDFLinkFactory.connect(localdsg)) {
        try (QueryExec qExec = link.query(queryString)) {
            RowSet rs = qExec.select();
            long x = Iter.count(rs);
            assertEquals(1, x);
        }
    }
    assertEquals("Modifier did not run", 1, COUNTER.get());
}
Also used : QueryExec(org.apache.jena.sparql.exec.QueryExec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowSet(org.apache.jena.sparql.exec.RowSet) RDFLink(org.apache.jena.rdflink.RDFLink) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) EnvTest(org.apache.jena.test.conn.EnvTest) Test(org.junit.Test)

Example 4 with HttpRequestModifier

use of org.apache.jena.http.sys.HttpRequestModifier in project jena by apache.

the class HttpLib method modifyByService.

/**
 * Allow setting additional/optional query parameters on a per remote service (including for SERVICE).
 * <ul>
 * <li>ARQ.httpRequestModifer - the specific modifier</li>
 * <li>ARQ.httpRegistryRequestModifer - the registry, keyed by service URL.</li>
 * </ul>
 */
/*package*/
public static void modifyByService(String serviceURI, Context context, Params params, Map<String, String> httpHeaders) {
    HttpRequestModifier modifier = context.get(ARQ.httpRequestModifer);
    if (modifier != null) {
        modifier.modify(params, httpHeaders);
        return;
    }
    RegistryRequestModifier modifierRegistry = context.get(ARQ.httpRegistryRequestModifer);
    if (modifierRegistry == null)
        modifierRegistry = RegistryRequestModifier.get();
    if (modifierRegistry != null) {
        HttpRequestModifier mods = modifierRegistry.find(serviceURI);
        if (mods != null)
            mods.modify(params, httpHeaders);
    }
}
Also used : RegistryRequestModifier(org.apache.jena.http.sys.RegistryRequestModifier) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier)

Example 5 with HttpRequestModifier

use of org.apache.jena.http.sys.HttpRequestModifier in project jena by apache.

the class TestService method service_query_extra_params_oldstyle_by_context_1.

// Uses a HttpRequestModifier to check the changes.
@Test
public void service_query_extra_params_oldstyle_by_context_1() {
    Map<String, Map<String, List<String>>> testServiceParams = new HashMap<>();
    Map<String, List<String>> settings = new HashMap<>();
    settings.put("apikey", List.of("BristolCalling"));
    testServiceParams.put(SERVICE, settings);
    DatasetGraph clientDGS = localDataset();
    clientDGS.getContext().set(ARQ.serviceParams, testServiceParams);
    AtomicBoolean seen = new AtomicBoolean(false);
    HttpRequestModifier inspector = (params, header) -> {
        seen.set(params.containsParam("apikey"));
    };
    logOnlyErrors(Fuseki.class, () -> {
        runWithModifier(SERVICE, inspector, () -> {
            String queryString = "ASK { SERVICE <" + SERVICE + "> { BIND(now() AS ?now) } }";
            try (QueryExec qExec = QueryExec.dataset(clientDGS).query(queryString).build()) {
                boolean b = qExec.ask();
                assertTrue(b);
            }
        });
    });
    assertTrue(seen.get());
}
Also used : ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Context(org.apache.jena.sparql.util.Context) Binding(org.apache.jena.sparql.engine.binding.Binding) LogCtl(org.apache.jena.atlas.logging.LogCtl) BeforeClass(org.junit.BeforeClass) NodeFactory(org.apache.jena.graph.NodeFactory) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) RDFLinkFactory(org.apache.jena.rdflink.RDFLinkFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BasicPattern(org.apache.jena.sparql.core.BasicPattern) HashMap(java.util.HashMap) RowSet(org.apache.jena.sparql.exec.RowSet) RDFLink(org.apache.jena.rdflink.RDFLink) StrUtils(org.apache.jena.atlas.lib.StrUtils) Algebra(org.apache.jena.sparql.algebra.Algebra) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) OpService(org.apache.jena.sparql.algebra.op.OpService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Op(org.apache.jena.sparql.algebra.Op) Fuseki(org.apache.jena.fuseki.Fuseki) Iter(org.apache.jena.atlas.iterator.Iter) DatasetGraphZero(org.apache.jena.sparql.core.DatasetGraphZero) EnvTest(org.apache.jena.test.conn.EnvTest) Before(org.junit.Before) Element(org.apache.jena.sparql.syntax.Element) AfterClass(org.junit.AfterClass) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) org.apache.jena.query(org.apache.jena.query) Test(org.junit.Test) QueryIterService(org.apache.jena.sparql.engine.main.iterator.QueryIterService) ElementService(org.apache.jena.sparql.syntax.ElementService) List(java.util.List) SSE(org.apache.jena.sparql.sse.SSE) Node(org.apache.jena.graph.Node) Assert(org.junit.Assert) RegistryRequestModifier(org.apache.jena.http.sys.RegistryRequestModifier) QueryExec(org.apache.jena.sparql.exec.QueryExec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryExec(org.apache.jena.sparql.exec.QueryExec) HashMap(java.util.HashMap) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) EnvTest(org.apache.jena.test.conn.EnvTest) Test(org.junit.Test)

Aggregations

HttpRequestModifier (org.apache.jena.http.sys.HttpRequestModifier)8 RDFLink (org.apache.jena.rdflink.RDFLink)7 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)7 RegistryRequestModifier (org.apache.jena.http.sys.RegistryRequestModifier)6 QueryExec (org.apache.jena.sparql.exec.QueryExec)6 EnvTest (org.apache.jena.test.conn.EnvTest)6 Test (org.junit.Test)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RDFLinkFactory (org.apache.jena.rdflink.RDFLinkFactory)4 RowSet (org.apache.jena.sparql.exec.RowSet)4 SSE (org.apache.jena.sparql.sse.SSE)4 AfterClass (org.junit.AfterClass)4 Before (org.junit.Before)4 BeforeClass (org.junit.BeforeClass)4 RDFLinkHTTP (org.apache.jena.rdflink.RDFLinkHTTP)3 DatasetGraphFactory (org.apache.jena.sparql.core.DatasetGraphFactory)3 GSP (org.apache.jena.sparql.exec.http.GSP)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2