Part 2: OSLC CM only with Jena Framework (simplyfied)

In Part 1 we extend the OSLC Examples with some Jena Code. The reason for this was the problem with
the self signed Certificate of the Jazz Server.
In Part 2 we will change the Trustmanager to accept everything. Details about this at http://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html
This is only for testing and not for a productive environment.

package net.jazz.oslc.consumer.examples;

/*******************************************************************************
* Thanks to http://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html
*
********************************************************************************/

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

/**
* This first example describes how to access to the Root Services document
* (https://localhost:9443/jazz/rootservices) using the Apache HttpClient APIs
*
* A Jazz Team server must be started.
*/
public class ConnectHttps {

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

//============== Code to adapt to your own configuration =============//
String server = "https://localhost:9443/ccm"; // Set the Public URI of your RTC server

String rootServices = server + "/rootservices";

TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) { }

public void checkServerTrusted(X509Certificate[] certs, String authType) { }

}
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
/*
* end of the fix
*/

//create Jena Model/Graph
Model model = ModelFactory.createDefaultModel();
model.read(rootServices);
//Print RDF as N3
//model.write(System.out, "N3");

StmtIterator statements = model.listStatements((Resource)null, model.createProperty("http://open-services.net/xmlns/cm/1.0/cmServiceProviders"), (RDFNode)null );

//Get workitem catalog
while(statements.hasNext()){
RDFNode object = statements.next().getObject();
System.out.println(object.toString());
}

}

}

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: