d2b88de5a1b20427c43bfbf6dd2b04044a552708
[jalview.git] / src / jalview / javascript / web / ClientResponse.java
1 package jalview.javascript.web;
2
3 import jalview.javascript.json.JSON;
4 import jalview.util.Platform;
5
6 import java.net.URL;
7
8 /**
9  * minimal implementation of com.sun.jersey.api.client.ClientResponse
10  * 
11  * @author hansonr
12  *
13  */
14 public class ClientResponse
15 {
16
17   private String response;
18
19   private boolean isJSON;
20
21   private Object jsonData;
22
23   int responseCode = -1;
24
25   public ClientResponse(URL url, String[] encoding)
26   {
27     // OK, so it turns out that ajax "json" format - or for that matter, any
28     // format for json is still just text. There is no point in getting this
29     // using special jQuery "json" formats. Duh. BH wasted a whole day try to
30     // "do it right".
31     response = Platform.getFileAsString(url.toString());
32     responseCode = (response == null || response == "" ? 404 : 200);
33     isJSON = encoding[0].equals("application/json");
34     if (isJSON)
35     {
36       try
37       {
38         jsonData = JSON.parse(response);
39       } catch (Exception e)
40       {
41         jsonData = null;
42       }
43       if (jsonData == null)
44       {
45         responseCode = 400;
46       }
47     }
48   }
49
50   public Object getEntity(Class<?> c)
51   {
52
53     if (c == java.util.Map.class)
54     {
55       return jsonData;
56     }
57     return response;
58   }
59
60   // 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
61
62   // {
63   // "responseHeader":{
64   // "status":0,
65   // "QTime":0,
66   // "params":{
67   // "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
68   // "fl":"pdb_id,title,experimental_method,resolution",
69   // "start":"0",
70   // "sort":"overall_quality desc",
71   // "rows":"500",
72   // "wt":"json"}},
73   // "response":{"numFound":1,"start":0,"docs":[
74   // {
75   // "experimental_method":["X-ray diffraction"],
76   // "pdb_id":"4zhp",
77   // "resolution":2.46,
78   // "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S
79   // cluster"}]
80   // }}
81   //
82
83   public int getStatus()
84   {
85     return responseCode;
86   }
87
88   public Object getJSONData()
89   {
90     return jsonData;
91   }
92
93 }