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