b686c95318d1ff6621be9b0c28db8a923f5c6ea9
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.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.fts.service.threedbeacons;
22
23 import java.net.URI;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31
32 import org.json.simple.parser.ParseException;
33
34 import com.sun.jersey.api.client.Client;
35 import com.sun.jersey.api.client.ClientResponse;
36 import com.sun.jersey.api.client.WebResource;
37 import com.sun.jersey.api.client.config.DefaultClientConfig;
38
39 import jalview.datamodel.SequenceI;
40 import jalview.fts.api.FTSData;
41 import jalview.fts.api.FTSDataColumnI;
42 import jalview.fts.api.FTSRestClientI;
43 import jalview.fts.api.StructureFTSRestClientI;
44 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
45 import jalview.fts.core.FTSRestClient;
46 import jalview.fts.core.FTSRestRequest;
47 import jalview.fts.core.FTSRestResponse;
48 import jalview.util.JSONUtils;
49 import jalview.util.MessageManager;
50 import jalview.util.Platform;
51
52 public class TDBeaconsFTSRestClient extends FTSRestClient
53         implements StructureFTSRestClientI
54 {
55   /**
56    * production server URI
57    */
58   private static String TDB_PROD_API = "https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
59
60   /**
61    * dev server URI
62    */
63   private static String TDB_DEV_API = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
64
65   private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API;
66
67   public static FTSRestClientI instance = null;
68
69   protected TDBeaconsFTSRestClient()
70   {
71   }
72
73   @SuppressWarnings("unchecked")
74   @Override
75   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
76           throws Exception
77   {
78     try
79     {
80       String query = tdbRestRequest.getSearchTerm();
81       Client client;
82       Class<ClientResponse> clientResponseClass;
83       if (Platform.isJS())
84       {
85         // JavaScript only
86         client = (Client) (Object) new jalview.javascript.web.Client();
87         clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
88       }
89       else
90       /**
91        * Java only
92        * 
93        * @j2sIgnore
94        */
95       {
96         client = Client.create(new DefaultClientConfig());
97         clientResponseClass = ClientResponse.class;
98       }
99
100       WebResource webResource;
101       webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN + query);
102
103       URI uri = webResource.getURI();
104       System.out.println(uri.toString());
105
106       // Execute the REST request
107       ClientResponse clientResponse;
108       if (isMocked())
109       {
110         clientResponse = null;
111       }
112       else
113       {
114         clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
115                 .get(clientResponseClass);
116       }
117
118       // Get the JSON string from the response object or directly from the
119       // client (JavaScript)
120       Map<String, Object> jsonObj = null;
121       String responseString = null;
122
123       // Check the response status and report exception if one occurs
124       int responseStatus = isMocked()
125               ? (mockQueries.containsKey(query) ? 200 : 404)
126               : clientResponse.getStatus();
127       switch (responseStatus)
128       {
129       // if success
130       case 200:
131         if (Platform.isJS())
132         {
133           jsonObj = clientResponse.getEntity(Map.class);
134         }
135         else
136         {
137           responseString = isMocked() ? mockQueries.get(query)
138                   : clientResponse.getEntity(String.class);
139         }
140         break;
141       case 400:
142         throw new Exception(parseJsonExceptionString(responseString));
143       case 404:
144         return emptyTDBeaconsJsonResponse();
145       default:
146         throw new Exception(
147                 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
148       }
149       // Process the response and return the result to the caller.
150       return parseTDBeaconsJsonResponse(responseString, jsonObj,
151               tdbRestRequest);
152     } catch (Exception e)
153     {
154       String exceptionMsg = e.getMessage();
155       if (exceptionMsg != null)
156       {
157         if (exceptionMsg.contains("SocketException"))
158         {
159           // No internet connection
160           throw new Exception(MessageManager.getString(
161                   "exception.unable_to_detect_internet_connection"));
162         }
163         else if (exceptionMsg.contains("UnknownHostException"))
164         {
165           // The server is unreachable
166           throw new Exception(MessageManager.formatMessage(
167                   "exception.fts_server_unreachable", "3DB Hub"));
168         }
169       }
170       throw e;
171
172     }
173
174   }
175
176   /**
177    * returns response for when the 3D-Beacons service doesn't have a record for
178    * the given query - in 2.11.2 this triggers a failover to the PDBe FTS
179    * 
180    * @return null
181    */
182   private FTSRestResponse emptyTDBeaconsJsonResponse()
183   {
184     return null;
185   }
186
187   public String setSearchTerm(String term)
188   {
189     return term;
190   }
191
192   public static FTSRestResponse parseTDBeaconsJsonResponse(
193           String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
194   {
195     return parseTDBeaconsJsonResponse(tdbJsonResponseString,
196             (Map<String, Object>) null, tdbRestRequest);
197   }
198
199   @SuppressWarnings("unchecked")
200   public static FTSRestResponse parseTDBeaconsJsonResponse(
201           String tdbJsonResponseString, Map<String, Object> jsonObj,
202           FTSRestRequest tdbRestRequest)
203   {
204     FTSRestResponse searchResult = new FTSRestResponse();
205     List<FTSData> result = null;
206
207     try
208     {
209       if (jsonObj == null)
210       {
211         jsonObj = (Map<String, Object>) JSONUtils
212                 .parse(tdbJsonResponseString);
213       }
214
215       Object uniprot_entry = jsonObj.get("uniprot_entry");
216       // TODO: decide if anything from uniprot_entry needs to be reported via
217       // the FTSRestResponse object
218       // Arnaud added seqLength = (Long) ((Map<String, Object>)
219       // jsonObj.get("uniprot_entry")).get("sequence_length");
220
221       List<Object> structures = (List<Object>) jsonObj.get("structures");
222       result = new ArrayList<>();
223
224       int numFound = 0;
225       for (Iterator<Object> strucIter = structures.iterator(); strucIter
226               .hasNext();)
227       {
228         Map<String, Object> structure = (Map<String, Object>) strucIter
229                 .next();
230         result.add(getFTSData(structure, tdbRestRequest));
231         numFound++;
232       }
233
234       searchResult.setNumberOfItemsFound(numFound);
235       searchResult.setSearchSummary(result);
236
237     } catch (ParseException e)
238     {
239       e.printStackTrace();
240     }
241     return searchResult;
242   }
243
244   private static FTSData getFTSData(
245           Map<String, Object> tdbJsonStructureSummary,
246           FTSRestRequest tdbRequest)
247   {
248     String primaryKey = null;
249     Object[] summaryRowData;
250
251     SequenceI associatedSequence;
252
253     Collection<FTSDataColumnI> displayFields = tdbRequest.getWantedFields();
254     SequenceI associatedSeq = tdbRequest.getAssociatedSequence();
255     int colCounter = 0;
256     summaryRowData = new Object[(associatedSeq != null)
257             ? displayFields.size() + 1
258             : displayFields.size()];
259     if (associatedSeq != null)
260     {
261       associatedSequence = associatedSeq;
262       summaryRowData[0] = associatedSequence;
263       colCounter = 1;
264     }
265
266     for (FTSDataColumnI field : displayFields)
267     {
268       String fieldData = (tdbJsonStructure.get(field.getCode()) == null)
269               ? " "
270               : tdbJsonStructure.get(field.getCode()).toString();
271       // System.out.println("Field : " + field + " Data : " + fieldData);
272       if (field.isPrimaryKeyColumn())
273       {
274         primaryKey = fieldData;
275         summaryRowData[colCounter++] = primaryKey;
276       }
277       else if (fieldData == null || fieldData.trim().isEmpty())
278       {
279         summaryRowData[colCounter++] = null;
280       }
281       else
282       {
283         try
284         {
285           summaryRowData[colCounter++] = (field.getDataType()
286                   .getDataTypeClass() == Integer.class)
287                           ? Integer.valueOf(fieldData)
288                           : (field.getDataType()
289                                   .getDataTypeClass() == Double.class)
290                                           ? Double.valueOf(fieldData)
291                                           : fieldData;
292         } catch (Exception e)
293         {
294           // e.printStackTrace();
295           System.out.println("offending value:" + fieldData + fieldData);
296         }
297       }
298     }
299     final String primaryKey1 = primaryKey;
300     final Object[] summaryRowData1 = summaryRowData;
301
302     return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
303   }
304
305   // private static FTSData getFTSData(Map<String, Object> doc,
306   // FTSRestRequest tdbRestRequest)
307   // {
308   // String primaryKey = null;
309   //
310   // Object[] summaryRowData;
311   //
312   // Collection<FTSDataColumnI> displayFields =
313   // tdbRestRequest.getWantedFields();
314   // int colCounter = 0;
315   // summaryRowData = new Object[displayFields.size() + 1];
316   //
317   // return null;
318   // }
319
320   private String parseJsonExceptionString(String jsonErrorString)
321   {
322     // TODO Auto-generated method stub
323     return null;
324   }
325
326   @Override
327   public String getColumnDataConfigFileName()
328   {
329     return "/fts/tdbeacons_data_columns.txt";
330   }
331
332   public static FTSRestClientI getInstance()
333   {
334     if (instance == null)
335     {
336       instance = new TDBeaconsFTSRestClient();
337     }
338     return instance;
339   }
340
341   private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
342
343   @Override
344   public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
345   {
346     if (allDefaultDisplayedStructureDataColumns == null
347             || allDefaultDisplayedStructureDataColumns.isEmpty())
348     {
349       allDefaultDisplayedStructureDataColumns = new ArrayList<>();
350       allDefaultDisplayedStructureDataColumns
351               .addAll(super.getAllDefaultDisplayedFTSDataColumns());
352     }
353     return allDefaultDisplayedStructureDataColumns;
354   }
355
356   @Override
357   public String[] getPreferencesColumnsFor(PreferenceSource source)
358   {
359     String[] columnNames = null;
360     switch (source)
361     {
362     case SEARCH_SUMMARY:
363       columnNames = new String[] { "", "Display", "Group" };
364       break;
365     case STRUCTURE_CHOOSER:
366       columnNames = new String[] { "", "Display", "Group" };
367       break;
368     case PREFERENCES:
369       columnNames = new String[] { "3DB Beacons Field",
370           "Show in search summary", "Show in structure summary" };
371       break;
372     default:
373       break;
374     }
375     return columnNames;
376   }
377 }