Permalink
Browse files

implementation of an fr.grid5000.api package containing a Connection …

…class and G5K objects.
  • Loading branch information...
crohr committed Sep 18, 2009
1 parent c91319a commit e718e5bc87f4670a2c19093308ccb3810ff01f00
View
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/activation.jar"/>
<classpathentry kind="lib" path="lib/jcalendar-1.3.2.jar"/>
@@ -17,6 +17,10 @@
<classpathentry kind="lib" path="lib/liquidlnf.jar"/>
<classpathentry kind="lib" path="lib/GangliaAnalyzer.jar"/>
<classpathentry kind="lib" path="lib/l2fprod-common-tasks.jar"/>
+ <classpathentry kind="lib" path="lib/jsr311-api-1.1.jar"/>
<classpathentry kind="lib" path="lib/trilead-ssh2-build211.jar"/>
+ <classpathentry kind="lib" path="lib/gson-1.3.jar"/>
+ <classpathentry kind="lib" path="lib/jersey-client-1.1.0-ea.jar"/>
+ <classpathentry kind="lib" path="lib/jersey-core-1.1.0-ea.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -1,2 +1,6 @@
.svn
-.DS_Store
+.DS_Store
+bin
+tools
+GRUDU.jar
+build
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
@@ -0,0 +1,95 @@
+package fr.grid5000.api;
+import fr.grid5000.api.errors.HTTPClientError;
+import fr.grid5000.api.errors.HTTPServerError;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.security.GeneralSecurityException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+import com.sun.jersey.core.util.MultivaluedMapImpl;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
+import javax.ws.rs.core.MediaType;
+
+
+public class Connection {
+ private URI uri;
+ private Client client;
+
+ public Connection(String url, Map<String, String> options) throws URISyntaxException, GeneralSecurityException {
+ uri = new URI(url);
+ // http://www.nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/
+ // Create a trust manager that does not validate certificate chains
+ 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) {
+ }
+ }
+ };
+
+ // Install the all-trusting trust manager
+ 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);
+
+ client = Client.create();
+ if (options.containsKey("username") && options.containsKey("password")) {
+ HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(options.get("username"), options.get("password"));
+ client.addFilter(authFilter);
+ }
+
+ }
+
+ /**
+ * GET a resource and return the raw response .
+ * @param path the path to the resource, to be joined to the connection's uri (if no scheme is present) or used as itself.
+ * @param params a hash of query parameters.
+ * @param headers a hash of custom HTTP headers to send with the query.
+ * @return ClientResponse
+ */
+ public ClientResponse get(String path, Map<String, String> params, Map<String, String> headers) throws HTTPClientError, HTTPServerError {
+ URI location = this.uri.resolve(path);
+ WebResource webResource = client.resource(location.toString());
+ MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
+ Iterator<String> iquery = params.keySet().iterator();
+ while(iquery.hasNext()) {
+ String key = iquery.next();
+ queryParams.add(key, params.get(key));
+ }
+ Iterator<String> iheader = headers.keySet().iterator();
+ while(iheader.hasNext()) {
+ String key = iheader.next();
+ webResource.header(key, headers.get(key));
+ }
+ ClientResponse response = webResource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+ return response;
+ }
+
+}
@@ -0,0 +1,5 @@
+package fr.grid5000.api.errors;
+
+public class HTTPClientError extends Exception {
+
+}
@@ -0,0 +1,5 @@
+package fr.grid5000.api.errors;
+
+public class HTTPServerError extends Exception {
+
+}
@@ -0,0 +1,12 @@
+package fr.grid5000.api.objects;
+
+import java.util.Collection;
+import java.util.Map;
+
+public class Cluster {
+ public String uid;
+ public String model;
+ public Collection<Link> links;
+ public Map<String, Node> nodes;
+ Cluster() {}
+}
@@ -0,0 +1,10 @@
+package fr.grid5000.api.objects;
+
+import java.util.Collection;
+
+public class Environment {
+ public String uid;
+ public String type;
+ public Collection<Link> links;
+ Environment() {}
+}
@@ -0,0 +1,12 @@
+package fr.grid5000.api.objects;
+
+import java.util.Collection;
+import java.util.Map;
+
+public class Grid {
+ public String uid;
+ public Collection<Link> links;
+ public Map<String,Site> sites;
+ public Map<String,Environment> environments;
+ Grid() { }
+}
@@ -0,0 +1,8 @@
+package fr.grid5000.api.objects;
+
+public class Link {
+ public String rel;
+ public String href;
+ public String title;
+ Link() {}
+}
@@ -0,0 +1,8 @@
+package fr.grid5000.api.objects;
+
+import java.util.Collection;
+
+public class Node {
+ public String uid;
+ public Collection<Link> links;
+}
@@ -0,0 +1,15 @@
+package fr.grid5000.api.objects;
+
+import java.util.Collection;
+import java.util.Map;
+
+public class Site {
+ public String uid;
+ public String name;
+ public Collection<Link> links;
+ public Map<String, Cluster> clusters;
+ public Map<String, Environment> environments;
+ public Float latitude;
+ public Float longitude;
+ Site() {}
+}

0 comments on commit e718e5b

Please sign in to comment.