All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.amazonaws.neptune.client.jena.NeptuneJenaSigV4Example Maven / Gradle / Ivy

Go to download

A SPARQL client for Amazon Neptune that includes AWS Signature Version 4 signing. Implemented as an RDF4J repository.

The newest version!
/*
 *   Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License").
 *   You may not use this file except in compliance with the License.
 *   A copy of the License is located at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   or in the "license" file accompanying this file. This file is distributed
 *   on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 *   express or implied. See the License for the specific language governing
 *   permissions and limitations under the License.
 */

package com.amazonaws.neptune.client.jena;

import com.amazonaws.neptune.auth.NeptuneApacheHttpSigV4Signer;
import com.amazonaws.neptune.auth.NeptuneSigV4SignerException;
import software.amazon.awssdk.utils.StringUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.rdfconnection.RDFConnectionRemote;
import org.apache.jena.rdfconnection.RDFConnectionRemoteBuilder;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;

/*
 * Example of a building a remote connection
 */
public class NeptuneJenaSigV4Example {

    public static void main(String... args) throws NeptuneSigV4SignerException {

        if (args.length == 0 || StringUtils.isEmpty(args[0])) {
            System.err.println("Please specify your endpoint as program argument "
                    + "(e.g.: http://:)");
            System.exit(1);
        }

        final String endpoint = args[0];

        final String regionName = "us-east-1";
        final AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.create();

        final NeptuneApacheHttpSigV4Signer v4Signer = new NeptuneApacheHttpSigV4Signer(regionName, awsCredentialsProvider);

        final HttpClient v4SigningClient = HttpClientBuilder.create().addInterceptorLast(new HttpRequestInterceptor() {

            @Override
            public void process(final HttpRequest req, final HttpContext ctx) throws HttpException {

                if (req instanceof HttpUriRequest) {

                    final HttpUriRequest httpUriReq = (HttpUriRequest) req;
                    try {
                        v4Signer.signRequest(httpUriReq);
                    } catch (NeptuneSigV4SignerException e) {
                        throw new HttpException("Problem signing the request: ", e);
                    }

                } else {

                    throw new HttpException("Not an HttpUriRequest"); // this should never happen
                }
            }

        }).build();

        RDFConnectionRemoteBuilder builder = RDFConnectionRemote.create()
                .httpClient(v4SigningClient)
                .destination(endpoint)
                // Query only.
                .queryEndpoint("sparql")
                .updateEndpoint("sparql");

        String query = "SELECT * { ?s ?p ?o } LIMIT 100";

        // Whether the connection can be reused depends on the details of the implementation.
        // See example 5.
        try (RDFConnection conn = builder.build()) {
            System.out.println("> Printing query result: ");
            conn.querySelect(query, System.out::println);
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy