Formatting changes
[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 java.io.*;\r
28 \r
29 import javax.activation.DataHandler;\r
30 \r
31 import javax.xml.namespace.QName;\r
32 import javax.xml.rpc.ParameterMode;\r
33 \r
34 \r
35 /**\r
36  * DOCUMENT ME!\r
37  *\r
38  * @author $author$\r
39  * @version $Revision$\r
40  */\r
41 public class EBIFetchClient\r
42 {\r
43     Call call;\r
44     String format = "default";\r
45     String style = "raw";\r
46 \r
47     /**\r
48      * Creates a new EBIFetchClient object.\r
49      */\r
50     public EBIFetchClient()\r
51     {\r
52         try\r
53         {\r
54             call = (Call) new Service().createCall();\r
55             call.setTargetEndpointAddress(new java.net.URL(\r
56                     "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
57         }\r
58         catch (Exception ex)\r
59         {\r
60         }\r
61     }\r
62 \r
63     /**\r
64      * DOCUMENT ME!\r
65      *\r
66      * @return DOCUMENT ME!\r
67      */\r
68     public String[] getSupportedDBs()\r
69     {\r
70         try\r
71         {\r
72             call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
73             call.setReturnType(XMLType.SOAP_ARRAY);\r
74 \r
75             return (String[]) call.invoke(new Object[] {  });\r
76         }\r
77         catch (Exception ex)\r
78         {\r
79             return null;\r
80         }\r
81     }\r
82 \r
83     /**\r
84      * DOCUMENT ME!\r
85      *\r
86      * @return DOCUMENT ME!\r
87      */\r
88     public String[] getSupportedFormats()\r
89     {\r
90         try\r
91         {\r
92             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));\r
93             call.setReturnType(XMLType.SOAP_ARRAY);\r
94 \r
95             return (String[]) call.invoke(new Object[] {  });\r
96         }\r
97         catch (Exception ex)\r
98         {\r
99             return null;\r
100         }\r
101     }\r
102 \r
103     /**\r
104      * DOCUMENT ME!\r
105      *\r
106      * @return DOCUMENT ME!\r
107      */\r
108     public String[] getSupportedStyles()\r
109     {\r
110         try\r
111         {\r
112             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
113             call.setReturnType(XMLType.SOAP_ARRAY);\r
114 \r
115             return (String[]) call.invoke(new Object[] {  });\r
116         }\r
117         catch (Exception ex)\r
118         {\r
119             return null;\r
120         }\r
121     }\r
122 \r
123     /**\r
124      * DOCUMENT ME!\r
125      *\r
126      * @param ids DOCUMENT ME!\r
127      * @param f DOCUMENT ME!\r
128      * @param s DOCUMENT ME!\r
129      *\r
130      * @return DOCUMENT ME!\r
131      */\r
132     public String[] fetchData(String ids, String f, String s)\r
133     {\r
134         // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
135         // max 50 ids can be added at one time\r
136         try\r
137         {\r
138             call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
139             call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
140             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
141             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
142             call.setReturnType(XMLType.SOAP_ARRAY);\r
143 \r
144             if (f != null)\r
145             {\r
146                 format = f;\r
147             }\r
148 \r
149             if (s != null)\r
150             {\r
151                 style = s;\r
152             }\r
153 \r
154             return (String[]) call.invoke(new Object[] { ids, format, style });\r
155         }\r
156         catch (Exception ex)\r
157         {\r
158             return null;\r
159         }\r
160     }\r
161 \r
162     /**\r
163      * DOCUMENT ME!\r
164      *\r
165      * @param arg DOCUMENT ME!\r
166      * @param f DOCUMENT ME!\r
167      * @param s DOCUMENT ME!\r
168      *\r
169      * @return DOCUMENT ME!\r
170      */\r
171     public String fetchDataFile(String arg, String f, String s)\r
172     {\r
173         if (f != null)\r
174         {\r
175             format = f;\r
176         }\r
177 \r
178         if (s != null)\r
179         {\r
180             style = s;\r
181         }\r
182 \r
183         call.setOperationName(new QName("urn:Dbfetch", "fetchDataFile"));\r
184         call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
185         call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
186         call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
187 \r
188         QName qnameAttachment = new QName("urn:Dbfetch", "DataHandler");\r
189         call.registerTypeMapping(javax.activation.DataSource.class,\r
190             qnameAttachment, JAFDataHandlerSerializerFactory.class,\r
191             JAFDataHandlerDeserializerFactory.class);\r
192 \r
193         call.setReturnType(qnameAttachment);\r
194 \r
195         try\r
196         {\r
197             Object ret = call.invoke(new Object[] { arg, format, style });\r
198 \r
199             if (null == ret)\r
200             {\r
201                 System.err.println("Received null ");\r
202                 throw new AxisFault("", "Received null", null, null);\r
203             }\r
204 \r
205             if (ret instanceof String)\r
206             {\r
207                 System.err.println("Received problem response from server: " +\r
208                     ret);\r
209                 throw new AxisFault("", (String) ret, null, null);\r
210             }\r
211 \r
212             if (!(ret instanceof DataHandler))\r
213             {\r
214                 //The wrong type of object that what was expected.\r
215                 System.err.println("Received problem response from server:" +\r
216                     ret.getClass().getName());\r
217                 throw new AxisFault("",\r
218                     "Received problem response from server:" +\r
219                     ret.getClass().getName(), null, null);\r
220             }\r
221 \r
222             //Still here, so far so good.\r
223             DataHandler rdh = (DataHandler) ret;\r
224 \r
225             //From here we'll just treat the data resource as file.\r
226             String receivedfileName = rdh.getName(); //Get the filename.\r
227 \r
228             if (receivedfileName == null)\r
229             {\r
230                 System.err.println("Could not get the file name.");\r
231                 throw new AxisFault("", "Could not get the file name.", null,\r
232                     null);\r
233             }\r
234 \r
235             if (arg.equalsIgnoreCase("medline"))\r
236             {\r
237                 return receivedfileName;\r
238             }\r
239             else if (arg.equalsIgnoreCase("interpro"))\r
240             {\r
241                 return receivedfileName;\r
242             }\r
243             else\r
244             {\r
245                 System.err.println(receivedfileName);\r
246             }\r
247         }\r
248         catch (Exception ex)\r
249         {\r
250             ex.printStackTrace();\r
251         }\r
252 \r
253         return "ERROR";\r
254     }\r
255 }\r