a247030e784569bb3cecb44219149f52ac50fb07
[jalview.git] / src / ext / jemboss / soap / PublicRequest.java
1 /****************************************************************
2 *
3 *  This program is free software; you can redistribute it and/or
4 *  modify it under the terms of the GNU General Public License
5 *  as published by the Free Software Foundation; either version 2
6 *  of the License, or (at your option) any later version.
7 *
8 *  This program is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with this program; if not, write to the Free Software
15 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 *
17 *  Based on EmbreoPublicRequest
18 *
19 *  @author: Copyright (C) Tim Carver
20 *
21 ***************************************************************/
22
23 package ext.jemboss.soap;
24
25 import java.io.*;
26 import java.util.*;
27
28 import ext.jemboss.JembossParams;
29
30 //AXIS
31 import org.apache.axis.client.Call;
32 import org.apache.axis.client.Service;
33 import javax.xml.namespace.QName;
34 import org.apache.axis.encoding.XMLType;
35
36 /**
37 *
38 * Make a axis call to a public server, using the default service
39 *
40 */
41 public class PublicRequest
42 {
43
44   private Hashtable proganswer;
45
46   /**
47   *
48   * @param mysettings   jemboss properties
49   * @param method       method to call
50   * @throws JembossSoapException if call to web service fails
51   *
52   */
53    public PublicRequest(JembossParams mysettings, String method)
54                throws JembossSoapException
55    {
56      this(mysettings, mysettings.getPublicSoapService(), method);
57    }
58
59   /**
60   *
61   * @param mysettings   jemboss properties
62   * @param method       method to call
63   * @param args         arguments
64   * @throws JembossSoapException if call to web service fails
65   *
66   */
67    public PublicRequest(JembossParams mysettings, String method, Vector args)
68                throws JembossSoapException
69    {
70      this(mysettings, mysettings.getPublicSoapService(), method, args);
71    }
72
73   /**
74   *
75   * @param mysettings   jemboss properties
76   * @param service      web service to call
77   * @param method       method to call
78   * @throws JembossSoapException if call to web service fails
79   *
80   */
81    public PublicRequest(JembossParams mysettings, String service, String method)
82                throws JembossSoapException
83    {
84      this(mysettings, service, method, (Vector) null);
85    }
86
87   /**
88   *
89   * @param mysettings   jemboss properties
90   * @param service      web service to call
91   * @param method       method to call
92   * @param args         arguments
93   * @throws JembossSoapException if call to web service fails
94   *
95   */
96    public PublicRequest(JembossParams mysettings, String service,
97                         String method, Vector args)
98                throws JembossSoapException
99    {
100
101      try
102      {
103        String  endpoint = mysettings.getPublicSoapURL();
104        org.apache.axis.client.Service serv =
105                         new org.apache.axis.client.Service();
106
107        Call    call     = (Call) serv.createCall();
108        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
109        call.setOperationName(new QName(service, method));
110
111        Object params[] = null;
112        if(args != null)
113        {
114          params = new Object[args.size()];
115          Enumeration e = args.elements();
116          for(int i=0;i<args.size();i++)
117          {
118            Object obj = e.nextElement();
119            if(obj.getClass().equals(String.class))
120            {
121              params[i] = (String)obj;
122              call.addParameter("Args", XMLType.XSD_STRING,
123                              javax.xml.rpc.ParameterMode.IN);
124            }
125            else
126            {
127              params[i] = (byte[])obj;
128              call.addParameter("Args", XMLType.XSD_BYTE,
129                              javax.xml.rpc.ParameterMode.IN);
130            }
131          }
132        }
133        call.setReturnType(org.apache.axis.Constants.SOAP_VECTOR);
134
135        Vector vans;
136        if(args != null)
137          vans = (Vector)call.invoke( params );
138        else
139          vans = (Vector)call.invoke( new Object[] {});
140
141        proganswer = new Hashtable();
142        // assumes it's even sized
143        int n = vans.size();
144        for(int j=0;j<n;j+=2)
145        {
146          String s = (String)vans.get(j);
147          proganswer.put(s,vans.get(j+1));
148        }
149      }
150      catch (Exception e)
151      {
152        throw new JembossSoapException("Connection failed");
153      }
154
155    }
156
157
158   /**
159   *
160   * Gets an element out of the Jemboss result hash
161   * @param val  key to look up
162   * @return     element, or an empty String if there isn't
163   *             an element that matches the key
164   *
165   */
166   public String getVal(String val)
167   {
168     if (proganswer.containsKey(val))
169       return (String)proganswer.get(val);
170     else
171       return "";
172   }
173
174
175   /**
176   *
177   * The hash returned by the Jemboss call.
178   * @param      result
179   *
180   */
181   public Hashtable getHash()
182   {
183     return proganswer;
184   }
185
186 }