update author list in license for (JAL-826)
[jalview.git] / src / jalview / ws / ebi / EBIFetchClient.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
4  * \r
5  * This file is part of Jalview.\r
6  * \r
7  * Jalview is free software: you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License \r
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
10  * \r
11  * Jalview is distributed in the hope that it will be useful, but \r
12  * WITHOUT ANY WARRANTY; without even the implied warranty \r
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
14  * PURPOSE.  See the GNU General Public License for more details.\r
15  * \r
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
17  */\r
18 package jalview.ws.ebi;\r
19 \r
20 import java.io.*;\r
21 import java.util.*;\r
22 import javax.xml.namespace.*;\r
23 import javax.xml.rpc.*;\r
24 \r
25 import org.apache.axis.client.Call;\r
26 import org.apache.axis.client.Service;\r
27 import org.apache.axis.encoding.*;\r
28 \r
29 /**\r
30  * DOCUMENT ME!\r
31  * \r
32  * @author $author$\r
33  * @version $Revision$\r
34  */\r
35 public class EBIFetchClient\r
36 {\r
37   Call call;\r
38 \r
39   String format = "default";\r
40 \r
41   String style = "raw";\r
42 \r
43   /**\r
44    * Creates a new EBIFetchClient object.\r
45    */\r
46   public EBIFetchClient()\r
47   {\r
48     try\r
49     {\r
50       call = (Call) new Service().createCall();\r
51       call.setTargetEndpointAddress(new java.net.URL(\r
52               "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
53     } catch (Exception ex)\r
54     {\r
55     }\r
56   }\r
57 \r
58   /**\r
59    * DOCUMENT ME!\r
60    * \r
61    * @return DOCUMENT ME!\r
62    */\r
63   public String[] getSupportedDBs()\r
64   {\r
65     try\r
66     {\r
67       call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
68       call.setReturnType(XMLType.SOAP_ARRAY);\r
69 \r
70       return (String[]) call.invoke(new Object[]\r
71       {});\r
72     } catch (Exception ex)\r
73     {\r
74       return null;\r
75     }\r
76   }\r
77 \r
78   /**\r
79    * DOCUMENT ME!\r
80    * \r
81    * @return DOCUMENT ME!\r
82    */\r
83   public String[] getSupportedFormats()\r
84   {\r
85     try\r
86     {\r
87       call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));\r
88       call.setReturnType(XMLType.SOAP_ARRAY);\r
89 \r
90       return (String[]) call.invoke(new Object[]\r
91       {});\r
92     } catch (Exception ex)\r
93     {\r
94       return null;\r
95     }\r
96   }\r
97 \r
98   /**\r
99    * DOCUMENT ME!\r
100    * \r
101    * @return DOCUMENT ME!\r
102    */\r
103   public String[] getSupportedStyles()\r
104   {\r
105     try\r
106     {\r
107       call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
108       call.setReturnType(XMLType.SOAP_ARRAY);\r
109 \r
110       return (String[]) call.invoke(new Object[]\r
111       {});\r
112     } catch (Exception ex)\r
113     {\r
114       return null;\r
115     }\r
116   }\r
117 \r
118   public File fetchDataAsFile(String ids, String f, String s)\r
119           throws OutOfMemoryError\r
120   {\r
121     String[] data = fetchData(ids, f, s);\r
122     // TODO: after JV 2.4 - test data==null and pass error(s) back up if\r
123     // possible (OutOfMemoryErrors are usual problem)\r
124     if (data == null)\r
125     {\r
126       return null;\r
127     }\r
128     File outFile = null;\r
129     try\r
130     {\r
131       outFile = File.createTempFile("jalview", ".xml");\r
132       outFile.deleteOnExit();\r
133       PrintWriter out = new PrintWriter(new FileOutputStream(outFile));\r
134       int index = 0;\r
135       while (index < data.length)\r
136       {\r
137         out.println(data[index]);\r
138         index++;\r
139       }\r
140       out.close();\r
141     } catch (Exception ex)\r
142     {\r
143     }\r
144     return outFile;\r
145   }\r
146 \r
147   /**\r
148    * Single DB multiple record retrieval\r
149    * \r
150    * @param ids\r
151    *          db:query1;query2;query3\r
152    * @param f\r
153    *          raw/xml\r
154    * @param s\r
155    *          ?\r
156    * \r
157    * @return Raw string array result of query set\r
158    */\r
159   public String[] fetchData(String ids, String f, String s)\r
160           throws OutOfMemoryError\r
161   {\r
162     // Need to split\r
163     // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
164     StringTokenizer queries = new StringTokenizer(ids, ";");\r
165     String db = null;\r
166     StringBuffer querystring = null;\r
167     while (queries.hasMoreTokens())\r
168     {\r
169       String query = queries.nextToken();\r
170       int p;\r
171       if ((p = query.indexOf(':')) > -1)\r
172       {\r
173         db = query.substring(0, p);\r
174         query = query.substring(p + 1);\r
175       }\r
176       if (querystring == null)\r
177       {\r
178         querystring = new StringBuffer(query);\r
179       }\r
180       else\r
181       {\r
182         querystring.append("," + query);\r
183       }\r
184     }\r
185     if (db == null)\r
186     {\r
187       System.err.println("Invalid Query string : '" + ids\r
188               + "'\nShould be of form 'dbname:q1;q2;q3;q4'");\r
189     }\r
190     return fetchBatch(querystring.toString(), db, f, s);\r
191   }\r
192 \r
193   public String[] fetchBatch(String ids, String db, String f, String s)\r
194           throws OutOfMemoryError\r
195   {\r
196     // max 50 ids can be added at one time\r
197     try\r
198     {\r
199       // call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
200       call.setOperationName(new QName("urn:Dbfetch", "fetchBatch"));\r
201       call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN);\r
202       call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN);\r
203       call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
204       call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
205       call.setReturnType(XMLType.SOAP_ARRAY);\r
206 \r
207       if (f != null)\r
208       {\r
209         format = f;\r
210       }\r
211 \r
212       if (s != null)\r
213       {\r
214         style = s;\r
215       }\r
216 \r
217       try\r
218       {\r
219         return (String[]) call.invoke(new Object[]\r
220         { ids.toLowerCase(), db.toLowerCase(), format, style });\r
221       } catch (OutOfMemoryError er)\r
222       {\r
223 \r
224         System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db\r
225                 + ":\n" + ids);\r
226         throw er;\r
227       }\r
228       // return null;\r
229     } catch (Exception ex)\r
230     {\r
231       if (ex.getMessage().startsWith(\r
232               "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))\r
233       {\r
234         return null;\r
235       }\r
236       System.err.println("Unexpected exception when retrieving from " + db\r
237               + "\nQuery was : '" + ids + "'");\r
238       ex.printStackTrace(System.err);\r
239       return null;\r
240     }\r
241   }\r
242 }\r