Search in sources :

Example 1 with HttpClient

use of java.net.http.HttpClient in project jena by apache.

the class RemoteEndpointDriver method connect.

@Override
protected JenaConnection connect(Properties props, int compatibilityLevel) throws SQLException {
    String queryEndpoint = props.getProperty(PARAM_QUERY_ENDPOINT);
    String updateEndpoint = props.getProperty(PARAM_UPDATE_ENDPOINT);
    // Validate at least one endpoint present
    if (queryEndpoint == null && updateEndpoint == null)
        throw new SQLException("At least one of the " + PARAM_QUERY_ENDPOINT + " or " + PARAM_UPDATE_ENDPOINT + " connection parameters must be specified to make a remote connection");
    // Gather dataset related parameters
    List<String> defaultGraphs = this.getValues(props, PARAM_DEFAULT_GRAPH_URI);
    List<String> namedGraphs = this.getValues(props, PARAM_NAMED_GRAPH_URI);
    List<String> usingGraphs = this.getValues(props, PARAM_USING_GRAPH_URI);
    List<String> usingNamedGraphs = this.getValues(props, PARAM_USING_NAMED_GRAPH_URI);
    // Authentication settings
    HttpClient client = this.configureClient(props);
    // Result Types
    String selectResultsType = props.getProperty(PARAM_SELECT_RESULTS_TYPE, null);
    String modelResultsType = props.getProperty(PARAM_MODEL_RESULTS_TYPE, null);
    // Create connection
    return openConnection(queryEndpoint, updateEndpoint, defaultGraphs, namedGraphs, usingGraphs, usingNamedGraphs, client, JenaConnection.DEFAULT_HOLDABILITY, compatibilityLevel, selectResultsType, modelResultsType);
}
Also used : SQLException(java.sql.SQLException) HttpClient(java.net.http.HttpClient)

Example 2 with HttpClient

use of java.net.http.HttpClient in project jena by apache.

the class ExecHTTPBuilder method build.

public final X build() {
    Objects.requireNonNull(serviceURL, "No service URL");
    if (queryString == null && query == null)
        throw new QueryException("No query for QueryExecHTTP");
    HttpClient hClient = HttpEnv.getHttpClient(serviceURL, httpClient);
    Query queryActual = query;
    String queryStringActual = queryString;
    if (substitutionMap != null && !substitutionMap.isEmpty()) {
        if (query == null)
            throw new QueryException("Substitution only supported if a Query object was provided");
        queryActual = QueryTransformOps.transform(query, substitutionMap);
        queryStringActual = queryActual.toString();
    }
    Context cxt = contextAcc.context();
    return buildX(hClient, queryActual, queryStringActual, cxt);
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClient(java.net.http.HttpClient)

Example 3 with HttpClient

use of java.net.http.HttpClient in project jena by apache.

the class ExecUpdateHTTPBuilder method build.

public X build() {
    Objects.requireNonNull(serviceURL, "No service URL");
    if (updateOperations == null && updateString == null)
        throw new QueryException("No update for UpdateExecutionHTTP");
    if (updateOperations != null && updateString != null)
        throw new InternalErrorException("UpdateRequest and update string");
    HttpClient hClient = HttpEnv.getHttpClient(serviceURL, httpClient);
    UpdateRequest updateActual = updateOperations;
    if (substitutionMap != null && !substitutionMap.isEmpty()) {
        if (updateActual == null)
            throw new UpdateException("Substitution only supported if an UpdateRequest object was provided");
        updateActual = UpdateTransformOps.transform(updateActual, substitutionMap);
    }
    Context cxt = (context != null) ? context : ARQ.getContext().copy();
    return buildX(hClient, updateActual, cxt);
}
Also used : Context(org.apache.jena.sparql.util.Context) QueryException(org.apache.jena.query.QueryException) UpdateRequest(org.apache.jena.update.UpdateRequest) HttpClient(java.net.http.HttpClient) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException) UpdateException(org.apache.jena.update.UpdateException)

Example 4 with HttpClient

use of java.net.http.HttpClient in project jena by apache.

the class ExAuth02_QueryExecutionPW method exampleQueryAuthWithHttpClient.

// HttpClient
public static void exampleQueryAuthWithHttpClient() {
    System.out.println();
    System.out.println("HttpClient + QueryExecutionHTTP");
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    try (QueryExecution qexec = QueryExecutionHTTP.service(dataURL).httpClient(httpClient).endpoint(dataURL).queryString("ASK{}").build()) {
        qexec.execAsk();
    }
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator) QueryExecution(org.apache.jena.query.QueryExecution)

Example 5 with HttpClient

use of java.net.http.HttpClient in project jena by apache.

the class ExAuth04_ServicePW method exampleServiceByHttpClient.

private static void exampleServiceByHttpClient() {
    System.out.println();
    System.out.println("Custom HttpClient + SERVICE call");
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    Context cxt = ContextAccumulator.newBuilder().set(ARQ.httpQueryClient, httpClient).context();
    Query query = QueryFactory.create("SELECT * { SERVICE <" + dataURL + "> { ?s ?p ?o } }");
    Dataset emptyLocal = DatasetFactory.empty();
    try (QueryExecution qExec = QueryExecution.create().query(query).dataset(emptyLocal).context(cxt).build()) {
        System.out.println("Call using SERVICE...");
        ResultSet rs = qExec.execSelect();
        ResultSetFormatter.out(rs);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Aggregations

HttpClient (java.net.http.HttpClient)35 Authenticator (java.net.Authenticator)11 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)5 RDFFormat (org.apache.jena.riot.RDFFormat)4 Context (org.apache.jena.sparql.util.Context)4 URISyntaxException (java.net.URISyntaxException)3 HttpRequest (java.net.http.HttpRequest)3 QueryExecution (org.apache.jena.query.QueryExecution)3 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpResponse (java.net.http.HttpResponse)2 SQLException (java.sql.SQLException)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 HttpException (org.apache.jena.atlas.web.HttpException)2 RegistryHttpClient (org.apache.jena.http.RegistryHttpClient)2 Gson (com.google.gson.Gson)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1