Using Protégé through an authenticated proxy

I recently struggled with Protégé in order to make it work through an authenticated enterprise proxy, since I wanted to import / extend ontologies from the Web.

When running a Java application, you can pass it the URL of a proxy, but it seems the only way to setup authentication parameters is to modify original source code. I agree a plugin is certainly better, but I don’t have time to invest it, so here’s a simple hack to make Protégé-OWL work with such a proxy.

 mkdir protege-owl
cd protege-owl
svn checkout http://smi-protege.stanford.edu/repos/protege/owl/trunk  . 

You’ll also need to get the protege.jar file, that can be downloaded here, and move it to the /libs folder.

Add the following import:

 import java.util.Properties; 

In getInputStream method, replace

 conn.addRequestProperty("Accept", "*/*");
return conn.getInputStream(); 

By

 conn.addRequestProperty("Accept", "*/*");
// Set proxy settings only if the file is outside our domain
if(url.getHost().indexOf("company.host.tld")==-1) {
  Properties systemSettings = System.getProperties();
  systemSettings.put("http.proxyHost","proxy_URL");
  systemSettings.put("http.proxyPort", "proxy_Port");
  sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
  String encodedUserPwd = encoder.encode("proxy_User:proxy_Pass".getBytes());
  conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
}
return conn.getInputStream(); 

Add the following imports:

 import java.util.Properties;
import java.io.InputStream;
import java.net.URLConnection; 

In isPossibleToImport() method, replace:

 OntologyNameExtractor extractor = new OntologyNameExtractor(url.openConnection().getInputStream(), url);
URI uri = extractor.getOntologyName(); 

By:

 OntologyNameExtractor extractor;
// Set proxy settings only if the file is outside our domain
if(url.getHost().indexOf("edf.fr")==-1) {
  URLConnection conn = url.openConnection();
  conn.setRequestProperty("Accept", "application/rdf+xml");
  conn.addRequestProperty("Accept", "text/xml");
  conn.addRequestProperty("Accept", "*/*");
  Properties systemSettings = System.getProperties();
  systemSettings.put("http.proxyHost","proxy_URL");
  systemSettings.put("http.proxyPort", "proxy_Port");
  sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
  String encodedUserPwd = encoder.encode("proxy_User:proxy_Pass".getBytes());
  conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
  InputStream stream = conn.getInputStream();
  extractor = new OntologyNameExtractor(stream, url);
} else {
  extractor = new OntologyNameExtractor(url.openConnection().getInputStream(), url);
}
URI uri = extractor.getOntologyName(); 

Comments

Leave a Reply

You must be logged in to post a comment.