eebbfc88cf359bd252b2bcde33197ca697949210
[jalview.git] / src / jalview / io / EBIFetchClient.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.io;\r
20 \r
21 import org.apache.axis.AxisFault;\r
22 import org.apache.axis.client.*;\r
23 import org.apache.axis.encoding.XMLType;\r
24 import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;\r
25 import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;\r
26 \r
27 import javax.activation.DataHandler;\r
28 \r
29 import javax.xml.namespace.QName;\r
30 import javax.xml.rpc.ParameterMode;\r
31 \r
32 \r
33 /**\r
34  * DOCUMENT ME!\r
35  *\r
36  * @author $author$\r
37  * @version $Revision$\r
38  */\r
39 public class EBIFetchClient\r
40 {\r
41     Call call;\r
42     String format = "default";\r
43     String style = "raw";\r
44 \r
45     /**\r
46      * Creates a new EBIFetchClient object.\r
47      */\r
48     public EBIFetchClient()\r
49     {\r
50         try\r
51         {\r
52             call = (Call) new Service().createCall();\r
53             call.setTargetEndpointAddress(new java.net.URL(\r
54                     "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
55         }\r
56         catch (Exception ex)\r
57         {\r
58         }\r
59     }\r
60 \r
61     /**\r
62      * DOCUMENT ME!\r
63      *\r
64      * @return DOCUMENT ME!\r
65      */\r
66     public String[] getSupportedDBs()\r
67     {\r
68         try\r
69         {\r
70             call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
71             call.setReturnType(XMLType.SOAP_ARRAY);\r
72 \r
73             return (String[]) call.invoke(new Object[] {  });\r
74         }\r
75         catch (Exception ex)\r
76         {\r
77             return null;\r
78         }\r
79     }\r
80 \r
81     /**\r
82      * DOCUMENT ME!\r
83      *\r
84      * @return DOCUMENT ME!\r
85      */\r
86     public String[] getSupportedFormats()\r
87     {\r
88         try\r
89         {\r
90             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));\r
91             call.setReturnType(XMLType.SOAP_ARRAY);\r
92 \r
93             return (String[]) call.invoke(new Object[] {  });\r
94         }\r
95         catch (Exception ex)\r
96         {\r
97             return null;\r
98         }\r
99     }\r
100 \r
101     /**\r
102      * DOCUMENT ME!\r
103      *\r
104      * @return DOCUMENT ME!\r
105      */\r
106     public String[] getSupportedStyles()\r
107     {\r
108         try\r
109         {\r
110             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
111             call.setReturnType(XMLType.SOAP_ARRAY);\r
112 \r
113             return (String[]) call.invoke(new Object[] {  });\r
114         }\r
115         catch (Exception ex)\r
116         {\r
117             return null;\r
118         }\r
119     }\r
120 \r
121     /**\r
122      * DOCUMENT ME!\r
123      *\r
124      * @param ids DOCUMENT ME!\r
125      * @param f DOCUMENT ME!\r
126      * @param s DOCUMENT ME!\r
127      *\r
128      * @return DOCUMENT ME!\r
129      */\r
130     public String[] fetchData(String ids, String f, String s)\r
131     {\r
132         // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
133         // max 50 ids can be added at one time\r
134         try\r
135         {\r
136             call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
137             call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
138             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
139             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
140             call.setReturnType(XMLType.SOAP_ARRAY);\r
141 \r
142             if (f != null)\r
143             {\r
144                 format = f;\r
145             }\r
146 \r
147             if (s != null)\r
148             {\r
149                 style = s;\r
150             }\r
151 \r
152             return (String[]) call.invoke(new Object[] { ids, format, style });\r
153         }\r
154         catch (Exception ex)\r
155         {\r
156             return null;\r
157         }\r
158     }\r
159 \r
160     /**\r
161      * DOCUMENT ME!\r
162      *\r
163      * @param arg DOCUMENT ME!\r
164      * @param f DOCUMENT ME!\r
165      * @param s DOCUMENT ME!\r
166      *\r
167      * @return DOCUMENT ME!\r
168      */\r
169     public String fetchDataFile(String arg, String f, String s)\r
170     {\r
171         if (f != null)\r
172         {\r
173             format = f;\r
174         }\r
175 \r
176         if (s != null)\r
177         {\r
178             style = s;\r
179         }\r
180 \r
181         call.setOperationName(new QName("urn:Dbfetch", "fetchDataFile"));\r
182         call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
183         call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
184         call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
185 \r
186         QName qnameAttachment = new QName("urn:Dbfetch", "DataHandler");\r
187         call.registerTypeMapping(javax.activation.DataSource.class,\r
188             qnameAttachment, JAFDataHandlerSerializerFactory.class,\r
189             JAFDataHandlerDeserializerFactory.class);\r
190 \r
191         call.setReturnType(qnameAttachment);\r
192 \r
193         try\r
194         {\r
195             Object ret = call.invoke(new Object[] { arg, format, style });\r
196 \r
197             if (null == ret)\r
198             {\r
199                 System.err.println("Received null ");\r
200                 throw new AxisFault("", "Received null", null, null);\r
201             }\r
202 \r
203             if (ret instanceof String)\r
204             {\r
205                 System.err.println("Received problem response from server: " +\r
206                     ret);\r
207                 throw new AxisFault("", (String) ret, null, null);\r
208             }\r
209 \r
210             if (!(ret instanceof DataHandler))\r
211             {\r
212                 //The wrong type of object that what was expected.\r
213                 System.err.println("Received problem response from server:" +\r
214                     ret.getClass().getName());\r
215                 throw new AxisFault("",\r
216                     "Received problem response from server:" +\r
217                     ret.getClass().getName(), null, null);\r
218             }\r
219 \r
220             //Still here, so far so good.\r
221             DataHandler rdh = (DataHandler) ret;\r
222 \r
223             //From here we'll just treat the data resource as file.\r
224             String receivedfileName = rdh.getName(); //Get the filename.\r
225 \r
226             if (receivedfileName == null)\r
227             {\r
228                 System.err.println("Could not get the file name.");\r
229                 throw new AxisFault("", "Could not get the file name.", null,\r
230                     null);\r
231             }\r
232 \r
233             if (arg.equalsIgnoreCase("medline"))\r
234             {\r
235                 return receivedfileName;\r
236             }\r
237             else if (arg.equalsIgnoreCase("interpro"))\r
238             {\r
239                 return receivedfileName;\r
240             }\r
241             else\r
242             {\r
243                 System.err.println(receivedfileName);\r
244             }\r
245         }\r
246         catch (Exception ex)\r
247         {\r
248             ex.printStackTrace();\r
249         }\r
250 \r
251         return "ERROR";\r
252     }\r
253 }\r