1 package jalview.fts.service.threedbeacons;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
10 import javax.ws.rs.core.MediaType;
12 import org.json.simple.parser.ParseException;
14 import com.sun.jersey.api.client.Client;
15 import com.sun.jersey.api.client.ClientResponse;
16 import com.sun.jersey.api.client.WebResource;
17 import com.sun.jersey.api.client.config.DefaultClientConfig;
19 import jalview.datamodel.SequenceI;
20 import jalview.fts.api.FTSData;
21 import jalview.fts.api.FTSDataColumnI;
22 import jalview.fts.api.FTSRestClientI;
23 import jalview.fts.core.FTSRestClient;
24 import jalview.fts.core.FTSRestRequest;
25 import jalview.fts.core.FTSRestResponse;
26 import jalview.fts.service.pdb.PDBFTSRestClient;
27 import jalview.util.JSONUtils;
28 import jalview.util.MessageManager;
29 import jalview.util.Platform;
31 public class TDBeaconsFTSRestClient extends FTSRestClient
33 private static final String DEFAULT_THREEDBEACONS_DOMAIN =
34 "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons-hub-api/uniprot/";
36 private static FTSRestClientI instance = null;
38 @SuppressWarnings("unchecked")
40 public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
45 // Removed wantedFields, sortParam & facetPivot from PDBFTSRClient
47 int responseSize = (tdbRestRequest.getResponseSize() == 0)
48 ? getDefaultResponsePageSize()
49 : tdbRestRequest.getResponseSize();
51 String query = tdbRestRequest.getFieldToSearchBy()
52 + tdbRestRequest.getSearchTerm();
54 Class<ClientResponse> clientResponseClass;
58 client = (Client) (Object) new jalview.javascript.web.Client();
59 clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
67 client = Client.create(new DefaultClientConfig());
68 clientResponseClass = ClientResponse.class;
70 WebResource webResource;
71 webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN);
72 URI uri = webResource.getURI();
74 System.out.println(uri);
76 // Execute the REST request
77 ClientResponse clientResponse = webResource
78 .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
80 // Get the JSON string from the response object or directly from the
81 // client (JavaScript)
82 Map<String, Object> jsonObj = null;
83 String responseString = null;
85 System.out.println("query >>>>>>> " + tdbRestRequest.toString());
87 // Check the response status and report exception if one occurs
88 int responseStatus = clientResponse.getStatus();
89 switch (responseStatus)
95 jsonObj = clientResponse.getEntity(Map.class);
99 responseString = clientResponse.getEntity(String.class);
103 throw new Exception(parseJsonExceptionString(responseString));
106 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
108 // Process the response and return the result to the caller.
109 return parseTDBeaconsJsonResponse(responseString, jsonObj, tdbRestRequest);
110 } catch (Exception e)
112 String exceptionMsg = e.getMessage();
113 if (exceptionMsg.contains("SocketException"))
115 // No internet connection
116 throw new Exception(MessageManager.getString(
117 "exception.unable_to_detect_internet_connection"));
119 else if (exceptionMsg.contains("UnknownHostException"))
121 // The server is unreachable
122 throw new Exception(MessageManager.formatMessage(
123 "exception.fts_server_unreachable", "3DB Hub"));
133 public static FTSRestResponse parseTDBeaconsJsonResponse(
134 String tdbJsonResponseString, FTSRestRequest tdbRestRequest)
136 return parseTDBeaconsJsonResponse(tdbJsonResponseString,
137 (Map<String, Object>) null, tdbRestRequest);
140 @SuppressWarnings("unchecked")
141 public static FTSRestResponse parseTDBeaconsJsonResponse(String tdbJsonResponseString,
142 Map<String, Object> jsonObj, FTSRestRequest tdbRestRequest)
144 FTSRestResponse searchResult = new FTSRestResponse();
145 //searchResult.setNumberOfItemsFound(1);
146 List<FTSData> result = null;
152 jsonObj = (Map<String, Object>) JSONUtils.parse(tdbJsonResponseString);
154 Map<String, Object> tdbEntry = (Map<String, Object>) jsonObj.get("uniprot_entry");
155 //String seqLength = tdbEntry.get("sequence_length").toString();
156 int seqLength = Integer.valueOf(tdbEntry.get("sequence_length").toString());
158 // tdbStructures is the list of found structures
159 // Map<String, Object> tdbStructures = (Map<String, Object>) jsonObj.get("structures");
160 Object[] structures = (Object[]) jsonObj.get("structures");
161 String accessName = ((Map<String, Object>) jsonObj.get("uniprot_entry"))
162 .get("ac").toString();
163 // this is to be modified
164 int numFound = structures.length;
168 // result = new ArrayList<>();
169 // List<String> models;
170 // //= ((Map<String, Object>) jsonObj.get("structures"))
171 // //.get("model_identifier").toString();
172 // for (Iterator<String> modelsIter = models.iterator(); modelsIter.hasNext();)
175 // Map<String, Object> doc = (Map<String, Object>) docIter.next();
176 // result.add(getFTSData(doc, tdbRestRequest));
178 searchResult.setNumberOfItemsFound(numFound);
179 // searchResult.setAccessionName(accessName); ?usefulness?
180 // searchResult.setSearchSummary(result);
183 } catch (ParseException e)
190 // private static FTSData getFTSData(Map<String, Object> doc,
191 // FTSRestRequest tdbRestRequest)
193 // String primaryKey = null;
195 // Object[] summaryRowData;
197 // Collection<FTSDataColumnI> displayFields = tdbRestRequest.getWantedFields();
198 // int colCounter = 0;
199 // summaryRowData = new Object[displayFields.size() + 1];
204 private String parseJsonExceptionString(String jsonErrorString)
206 // TODO Auto-generated method stub
211 public String getColumnDataConfigFileName()
213 // TODO Auto-generated method stub
217 public static FTSRestClientI getInstance()
219 if (instance == null)
221 instance = new TDBeaconsFTSRestClient();
226 private Collection<FTSDataColumnI> allDefaultDisplayedStructureDataColumns;
228 public Collection<FTSDataColumnI> getAllDefaultDisplayedStructureDataColumns()
230 if (allDefaultDisplayedStructureDataColumns == null
231 || allDefaultDisplayedStructureDataColumns.isEmpty())
233 allDefaultDisplayedStructureDataColumns = new ArrayList<>();
234 allDefaultDisplayedStructureDataColumns
235 .addAll(super.getAllDefaultDisplayedFTSDataColumns());
237 return allDefaultDisplayedStructureDataColumns;