1

i am trying to connect to DBpedia to run a sparql query using apache jena. I am behind a proxy server, problem is i am getting a error with my code when connecting using apache jena but i can make a connection using direct url. This code is working.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author user
 */
public class NewClass {
     public static void main(String[] args) throws Exception {
         System.setProperty("http.proxyHost", "10.25.0.42");
    System.setProperty("http.proxyPort", "3128");
     Authenticator.setDefault(new Authenticator()
    {
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication("asiddh-me-13","*****".toCharArray());
    }
    });
        URL oracle = new URL("http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select++%3Fx+%3Fy+%3Fc+%3Fp%0D%0Awhere%7B%0D%0A%3Fx+dbpedia-owl%3AwikiPageDisambiguates+dbpedia%3ASOAP%3B%0D%0A+dbpedia-owl%3AwikiPageDisambiguates+%3Fy.%0D%0A%3Fy+dbpedia-owl%3Aabstract+%3Fc.%0D%0A%3Fy+dbpedia-owl%3Athumbnail+%3Fp.%0D%0Afilter%28langmatches%28lang%28%3Fc%29%2C%22en%22%29%29%0D%0A%7D&format=text%2Fhtml&timeout=30000&debug=on");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

But when i try to connect using Jena api it's giving me Error.

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.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import java.net.Authenticator;
import java.net.PasswordAuthentication;


public class Sparqldbpedia {
    public static void main(String[] args) {


System.setProperty("http.proxyHost", "10.25.0.42");
System.setProperty("http.proxyPort", "3128");
 Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("asiddh-me-13","****".toCharArray());
}
});


 String keyword="";
 keyword="tank";




 String sparqlQueryString = "PREFIX p: <http://dbpedia.org/property/>"+
"PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>"+
"PREFIX dbpedia: <http://dbpedia.org/resource/>"+
"PREFIX category: <http://dbpedia.org/resource/Category:>"+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
                "select  ?x ?y ?c ?p\n" +
"where{\n" +
"?x dbpedia-owl:wikiPageDisambiguates dbpedia:"+keyword+ ";\n" +
" dbpedia-owl:wikiPageDisambiguates ?y.\n" +
"?y dbpedia-owl:abstract ?c.\n" +
"?y dbpedia-owl:thumbnail ?p.\n" +
"filter(langmatches(lang(?c),\"en\"))\n" +
"}";
        Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

    System.out.println("try block");
    try {
        ResultSet results = qexec.execSelect();
        for ( ; results.hasNext() ; )
    {
        QuerySolution soln = results.nextSolution() ;
        String x = soln.get("?x").toString();
        String y = soln.get("?y").toString();
        String c = soln.get("?c").toString();
        String p = soln.get("?p").toString();

        System.out.print(x +"\t"+y+"\t"+c+"\t"+p+"\n");
    }

    }catch(Exception e){System.out.println("catch error"+e.getMessage());}
    finally { qexec.close() ; }




}

}

Error is :

HTTP 407 error making the query: Proxy Authentication Required
user2400582
  • 137
  • 1
  • 3
  • 9

2 Answers2

0

There's a version of sparqlService that accepts an HttpAuthenticator. Perhaps you can use that to handle whatever authentication is needed?

QueryExecution sparqlService(String service, Query query, HttpAuthenticator authenticator)

Create a QueryExecution that will access a SPARQL service over HTTP

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • I don't know how to use that,can you show me the correct way? – user1583465 Apr 15 '15 at 18:25
  • @user1583465 I've never used it myself. I just guessed that since your error is "Proxy Authentication Required", an object with a name like "HttpAuthenticator" might be a solution. This is just a guess, though. – Joshua Taylor Apr 15 '15 at 18:49
0

(for whom who comes here to find a solution for: Connecting to DBpedia endpoint behind a Proxy)

Your code has the solution itself. Add the following rows before you call .execSelect() :

System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
    }
});

Tested and working method:

public static void main(String[] args) {
        final String proxyUsername = "myUser";
        final String proxyPassword = "myPwd";
        final String proxyHost = "hostName";
        final String proxyPort = "port";    

        String sparqlQueryString = "PREFIX p: <http://dbpedia.org/property/>" +
                "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>" +
                "PREFIX dbpedia: <http://dbpedia.org/resource/>" +
                "PREFIX category: <http://dbpedia.org/resource/Category:>" +
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
                "select  ?x ?y ?c ?p\n" +
                "where{\n" +
                "?x dbpedia-owl:wikiPageDisambiguates dbpedia:tank;\n" +
                " dbpedia-owl:wikiPageDisambiguates ?y.\n" +
                "?y dbpedia-owl:abstract ?c.\n" +
                "?y dbpedia-owl:thumbnail ?p.\n" +
                "filter(langmatches(lang(?c),\"en\"))\n" +
                "}";
        Query query = QueryFactory.create(sparqlQueryString);
        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
            }
        });
        ResultSet results = qexec.execSelect();
    }
Ermal
  • 441
  • 5
  • 19