使用Jena ARQ访问具有基本身份验证的远程仓库 - 抢占式

时间:2014-01-15 12:06:52

标签: java jena apache-commons-httpclient

我正在尝试将Jena ARQ与PreemptiveBasicAuthenticator一起使用,但没有成功,任何人都可以帮忙吗?

我总是得到401,虽然通过休息客户端的相同请求,或使用openrdf工作。它可能与apache http客户端和必须设置AuthScope有关吗?

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import org.apache.jena.atlas.web.auth.HttpAuthenticator;
import org.apache.jena.atlas.web.auth.PreemptiveBasicAuthenticator;
import org.apache.jena.atlas.web.auth.ScopedAuthenticator;

import java.net.URI;

public class JenaConnect {

    private final static String SPARQLR_ENDPOINT = "https://repo-eh-01.sparqlr.com/repositories/examples-repo";
    private final static String SPARQLR_USERNAME = "examples-repo";
    private final static String SPARQLR_PASSWORD = "XXXX";

    public static void main(String[] args) throws Exception {

        String queryString = "SELECT * WHERE {?s ?p ?o}";
        Query query = QueryFactory.create(queryString);
        HttpAuthenticator authenticator = new PreemptiveBasicAuthenticator(
                new ScopedAuthenticator(new URI(SPARQLR_ENDPOINT), SPARQLR_USERNAME, SPARQLR_PASSWORD.toCharArray())
        );
        QueryExecution queryExecution = QueryExecutionFactory.sparqlService(SPARQLR_ENDPOINT, query, authenticator);
        try {
            ResultSet results = queryExecution.execSelect();
            int i = 0;
            while(results.hasNext()) {
                results.next();
                i++;
            }
            System.out.println(i);
        } finally {
            queryExecution.close();
        }
    }
}
相关问题