5b21bfe092515f97283c01493969dcf18073d963
[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 public class EBIFetchClient {\r
36     Call call;\r
37     String format = "default";\r
38     String style = "raw";\r
39 \r
40     public EBIFetchClient() {\r
41         try {\r
42             call = (Call) new Service().createCall();\r
43             call.setTargetEndpointAddress(new java.net.URL(\r
44                     "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
45         } catch (Exception ex) {\r
46         }\r
47     }\r
48 \r
49     public String[] getSupportedDBs() {\r
50         try {\r
51             call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
52             call.setReturnType(XMLType.SOAP_ARRAY);\r
53 \r
54             return (String[]) call.invoke(new Object[] {  });\r
55         } catch (Exception ex) {\r
56             return null;\r
57         }\r
58     }\r
59 \r
60     public String[] getSupportedFormats() {\r
61         try {\r
62             call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats"));\r
63             call.setReturnType(XMLType.SOAP_ARRAY);\r
64 \r
65             return (String[]) call.invoke(new Object[] {  });\r
66         } catch (Exception ex) {\r
67             return null;\r
68         }\r
69     }\r
70 \r
71     public String[] getSupportedStyles() {\r
72         try {\r
73             call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
74             call.setReturnType(XMLType.SOAP_ARRAY);\r
75 \r
76             return (String[]) call.invoke(new Object[] {  });\r
77         } catch (Exception ex) {\r
78             return null;\r
79         }\r
80     }\r
81 \r
82     public String[] fetchData(String ids, String f, String s) {\r
83         // ids should be of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
84         // max 50 ids can be added at one time\r
85         try {\r
86             call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
87             call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
88             call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
89             call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
90             call.setReturnType(XMLType.SOAP_ARRAY);\r
91 \r
92             if (f != null) {\r
93                 format = f;\r
94             }\r
95 \r
96             if (s != null) {\r
97                 style = s;\r
98             }\r
99 \r
100             return (String[]) call.invoke(new Object[] { ids, format, style });\r
101         } catch (Exception ex) {\r
102             return null;\r
103         }\r
104     }\r
105 \r
106     public String fetchDataFile(String arg, String f, String s) {\r
107         if (f != null) {\r
108             format = f;\r
109         }\r
110 \r
111         if (s != null) {\r
112             style = s;\r
113         }\r
114 \r
115         call.setOperationName(new QName("urn:Dbfetch", "fetchDataFile"));\r
116         call.addParameter("query", XMLType.XSD_STRING, ParameterMode.IN);\r
117         call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
118         call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
119 \r
120         QName qnameAttachment = new QName("urn:Dbfetch", "DataHandler");\r
121         call.registerTypeMapping(javax.activation.DataSource.class,\r
122             qnameAttachment, JAFDataHandlerSerializerFactory.class,\r
123             JAFDataHandlerDeserializerFactory.class);\r
124 \r
125         call.setReturnType(qnameAttachment);\r
126 \r
127         try {\r
128             Object ret = call.invoke(new Object[] { arg, format, style });\r
129 \r
130             if (null == ret) {\r
131                 System.err.println("Received null ");\r
132                 throw new AxisFault("", "Received null", null, null);\r
133             }\r
134 \r
135             if (ret instanceof String) {\r
136                 System.err.println("Received problem response from server: " +\r
137                     ret);\r
138                 throw new AxisFault("", (String) ret, null, null);\r
139             }\r
140 \r
141             if (!(ret instanceof DataHandler)) {\r
142                 //The wrong type of object that what was expected.\r
143                 System.err.println("Received problem response from server:" +\r
144                     ret.getClass().getName());\r
145                 throw new AxisFault("",\r
146                     "Received problem response from server:" +\r
147                     ret.getClass().getName(), null, null);\r
148             }\r
149 \r
150             //Still here, so far so good.\r
151             DataHandler rdh = (DataHandler) ret;\r
152 \r
153             //From here we'll just treat the data resource as file.\r
154             String receivedfileName = rdh.getName(); //Get the filename.\r
155 \r
156             if (receivedfileName == null) {\r
157                 System.err.println("Could not get the file name.");\r
158                 throw new AxisFault("", "Could not get the file name.", null,\r
159                     null);\r
160             }\r
161 \r
162             if (arg.equalsIgnoreCase("medline")) {\r
163                 return receivedfileName;\r
164             } else if (arg.equalsIgnoreCase("interpro")) {\r
165                 return receivedfileName;\r
166             } else {\r
167                 System.err.println(receivedfileName);\r
168             }\r
169         } catch (Exception ex) {\r
170             ex.printStackTrace();\r
171         }\r
172 \r
173         return "ERROR";\r
174     }\r
175 }\r