Merge branch 'Jalview-JS/develop' into develop
[jalview.git] / src / jalview / javascript / web / ClientResponse.java
diff --git a/src/jalview/javascript/web/ClientResponse.java b/src/jalview/javascript/web/ClientResponse.java
new file mode 100644 (file)
index 0000000..23580ed
--- /dev/null
@@ -0,0 +1,92 @@
+package jalview.javascript.web;
+
+import jalview.javascript.json.JSON;
+import jalview.util.Platform;
+
+import java.net.URL;
+
+/**
+ * minimal implementation of com.sun.jersey.api.client.ClientResponse
+ * 
+ * @author hansonr
+ *
+ */
+public class ClientResponse
+{
+
+  private String response;
+
+  private boolean isJSON;
+
+  private Object jsonData;
+
+  int responseCode = -1;
+
+  public ClientResponse(URL url, String[] encoding)
+  {
+    // OK, so it turns out that ajax "json" format - or for that matter, any
+    // format for json is still just text. There is no point in getting this
+    // using special jQuery "json" formats. Duh. BH wasted a whole day try to
+    // "do it right".
+    response = Platform.getFileAsString(url.toString());
+    responseCode = (response == null || response == "" ? 404 : 200);
+    isJSON = encoding[0].equals("application/json");
+    if (isJSON)
+    {
+      try
+      {
+        jsonData = JSON.parse(response);
+      } catch (Exception e)
+      {
+        jsonData = null;
+      }
+      if (jsonData == null)
+      {
+        responseCode = 400;
+      }
+    }
+  }
+
+  public Object getEntity(Class<?> c)
+  {
+
+    if (c == java.util.Map.class)
+    {
+      return jsonData;
+    }
+    return response;
+  }
+
+  // https://www.ebi.ac.uk/pdbe/search/pdb/select?wt=json&fl=pdb_id,title,experimental_method,resolution&rows=500&start=0&q=(text:q93xj9_soltu)+AND+molecule_sequence:%5B%27%27+TO+*%5D+AND+status:REL&sort=overall_quality+desc
+  
+//{
+//"responseHeader":{
+//  "status":0,
+//  "QTime":0,
+//  "params":{
+//    "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
+//    "fl":"pdb_id,title,experimental_method,resolution",
+//    "start":"0",
+//    "sort":"overall_quality desc",
+//    "rows":"500",
+//    "wt":"json"}},
+//"response":{"numFound":1,"start":0,"docs":[
+//    {
+//      "experimental_method":["X-ray diffraction"],
+//      "pdb_id":"4zhp",
+//      "resolution":2.46,
+//      "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S cluster"}]
+//}}
+//
+
+  public int getStatus()
+  {
+    return responseCode;
+  }
+
+  public Object getJSONData()
+  {
+    return jsonData;
+  }
+
+}