Search in sources :

Example 6 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_2.

// Uses a HttpRequestModifier to check the changes.
@Test
public void service_query_extra_params_oldstyle_by_context_2() {
    Map<String, Map<String, List<String>>> testServiceParams = new HashMap<>();
    Map<String, List<String>> settings = new HashMap<>();
    settings.put("apikey", List.of("BristolCallingToTheFarawayTowns"));
    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)

Example 7 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 8 with HttpRequestModifier

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

the class ExModification01 method exampleMod.

private static void exampleMod() {
    AtomicLong counter = new AtomicLong(0);
    HttpRequestModifier modifier = (params, headers) -> {
        long x = counter.incrementAndGet();
        headers.put("X-Tracker", "Call=" + x);
    };
    RegistryRequestModifier.get().addPrefix(serverURL, modifier);
    // RegistryRequestModifier.get().add(dataURL, modifier);
    // GSP : NO MODIFICATION no call to modifyByService
    // GSP : calls HttpRDF
    // Pass in request or at least request type.
    /*
org.apache.jena.http.sys.RegistryRequestModifier.get()
auth.examples.ExModification01.exampleMod()

org.apache.jena.http.HttpLib.modifyByService(String, Context, Params, Map<String, String>)
  org.apache.jena.sparql.exec.http.UpdateExecHTTP.execute()
  org.apache.jena.sparql.exec.http.QueryExecHTTP.query(String)
  ==> SERVICE via QueryExecHTTP
  ==> GSP ?

org.apache.jena.sparql.exec.http.TestService.runWithModifier(String, HttpRequestModifier, Runnable)

         */
    GSP.service(dataURL).defaultGraph().GET();
    try (RDFLink link = RDFLinkHTTP.service(dataURL).build()) {
        boolean b = link.queryAsk("ASK{}");
    }
}
Also used : GSP(org.apache.jena.sparql.exec.http.GSP) AtomicLong(java.util.concurrent.atomic.AtomicLong) DatasetGraphFactory(org.apache.jena.sparql.core.DatasetGraphFactory) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) FusekiLogging(org.apache.jena.fuseki.system.FusekiLogging) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) RDFLink(org.apache.jena.rdflink.RDFLink) RegistryRequestModifier(org.apache.jena.http.sys.RegistryRequestModifier) AtomicLong(java.util.concurrent.atomic.AtomicLong) RDFLink(org.apache.jena.rdflink.RDFLink) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier)

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