JAL-3746 apply copyright to source
[jalview.git] / src / jalview / javascript / web / ClientResponse.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.javascript.web;
22
23 import jalview.javascript.json.JSON;
24 import jalview.util.Platform;
25
26 import java.net.URL;
27
28 /**
29  * minimal implementation of com.sun.jersey.api.client.ClientResponse
30  * 
31  * @author hansonr
32  *
33  */
34 public class ClientResponse
35 {
36
37   private String response;
38
39   private boolean isJSON;
40
41   private Object jsonData;
42
43   int responseCode = -1;
44
45   public ClientResponse(URL url, String[] encoding)
46   {
47     // OK, so it turns out that ajax "json" format - or for that matter, any
48     // format for json is still just text. There is no point in getting this
49     // using special jQuery "json" formats. Duh. BH wasted a whole day try to
50     // "do it right".
51     response = Platform.getFileAsString(url.toString());
52     responseCode = (response == null || response == "" ? 404 : 200);
53     isJSON = encoding[0].equals("application/json");
54     if (isJSON)
55     {
56       try
57       {
58         jsonData = JSON.parse(response);
59       } catch (Exception e)
60       {
61         jsonData = null;
62       }
63       if (jsonData == null)
64       {
65         responseCode = 400;
66       }
67     }
68   }
69
70   public Object getEntity(Class<?> c)
71   {
72
73     if (c == java.util.Map.class)
74     {
75       return jsonData;
76     }
77     return response;
78   }
79
80   // 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
81
82   // {
83   // "responseHeader":{
84   // "status":0,
85   // "QTime":0,
86   // "params":{
87   // "q":"(text:q93xj9_soltu) AND molecule_sequence:['' TO *] AND status:REL",
88   // "fl":"pdb_id,title,experimental_method,resolution",
89   // "start":"0",
90   // "sort":"overall_quality desc",
91   // "rows":"500",
92   // "wt":"json"}},
93   // "response":{"numFound":1,"start":0,"docs":[
94   // {
95   // "experimental_method":["X-ray diffraction"],
96   // "pdb_id":"4zhp",
97   // "resolution":2.46,
98   // "title":"The crystal structure of Potato ferredoxin I with 2Fe-2S
99   // cluster"}]
100   // }}
101   //
102
103   public int getStatus()
104   {
105     return responseCode;
106   }
107
108   public Object getJSONData()
109   {
110     return jsonData;
111   }
112
113 }