later version of client-side WSDL2Java generated simple Alignment
[jalview.git] / src / ext / jemboss / soap / PrivateRequest.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 *  @author: Copyright (C) Tim Carver
18 *
19 ***************************************************************/
20
21 package ext.jemboss.soap;
22
23 import java.io.*;
24 import java.util.*;
25
26 import ext.jemboss.JembossParams;
27 import javax.swing.JOptionPane;
28 //AXIS
29 import org.apache.axis.client.Call;
30 import org.apache.axis.client.Service;
31 import javax.xml.namespace.QName;
32 import org.apache.axis.encoding.XMLType;
33
34 /**
35 *
36 * Make a axis call to a private server, using the default service
37 *
38 */
39 public class PrivateRequest
40 {
41
42   /** results from calling the web service */
43   private Hashtable proganswer;
44
45   /**
46   *
47   * @param mysettings jemboss properties
48   * @param method     method to call
49   * @param args       arguments
50   * @throws JembossSoapException if authentication fails
51   *
52   */
53    public PrivateRequest(JembossParams mysettings, String method, Vector args)
54                 throws JembossSoapException
55    {
56      this(mysettings, mysettings.getPrivateSoapService(), method, args);
57    }
58
59   /**
60   *
61   * @param mysettings jemboss properties
62   * @param service    service to call
63   * @param method     method to call
64   * @throws JembossSoapException If authentication fails
65   *
66   */
67    public PrivateRequest(JembossParams mysettings, String service, String method)
68                 throws JembossSoapException
69    {
70      this(mysettings, service, method, (Vector) null);
71    }
72
73   /**
74   *
75   * @param mysettings jemboss properties
76   * @param service    service to call
77   * @param method     method to call
78   * @param args       arguments
79   * @throws JembossSoapException if authentication fails
80   */
81    public PrivateRequest(JembossParams mysettings, String service, String method,
82                          Vector args) throws JembossSoapException
83    {
84      try
85      {
86        String  endpoint = mysettings.getPublicSoapURL()+"/"+service;
87        org.apache.axis.client.Service serv =
88                                new org.apache.axis.client.Service();
89        Call    call     = (Call) serv.createCall();
90        QName   qn       = new QName(service, method);
91        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
92        call.setOperationName(new QName(service, method));
93        call.setEncodingStyle(org.apache.axis.Constants.URI_SOAP12_ENC);
94
95        int nargs = 0;
96        Object params[] = null;
97        if(args != null)
98        {
99          if(mysettings.getUseAuth())
100            nargs = args.size()+2;
101          else
102            nargs = args.size()+1;
103
104          params = new Object[nargs];
105          Enumeration e = args.elements();
106          for(int i=0;i<args.size();i++)
107          {
108            Object obj = e.nextElement();
109            params[i] = obj;
110            if(obj.getClass().equals(String.class))
111            {
112              call.addParameter("Args"+i, XMLType.XSD_STRING,
113                              javax.xml.rpc.ParameterMode.IN);
114            }
115            else if(obj.getClass().equals(Hashtable.class))
116            {
117              params[i] = getVector((Hashtable)obj);
118
119              call.addParameter("Args"+i, XMLType.SOAP_VECTOR,
120                              javax.xml.rpc.ParameterMode.IN);
121            }
122            else    // byte[]
123            {
124              call.addParameter("ByteArray", XMLType.XSD_BASE64,
125                                javax.xml.rpc.ParameterMode.IN);
126              params[i] = obj;
127            }
128
129          }
130        }
131
132        if(mysettings.getUseAuth() == true)
133        {
134          if(args == null)
135          {
136            nargs = 2;
137            params = new Object[nargs];
138          }
139          call.addParameter("Door", XMLType.XSD_STRING,
140                            javax.xml.rpc.ParameterMode.IN);
141          params[nargs-2] = mysettings.getServiceUserName();
142
143          call.addParameter("Key", XMLType.XSD_BASE64,
144                            javax.xml.rpc.ParameterMode.IN);
145          params[nargs-1] = mysettings.getServicePasswdByte();
146        }
147        else       //No authorization reqd, so use user name here
148        {          //to create own sand box on server
149          if(nargs == 0)
150          {
151             nargs = 1;
152             params = new Object[nargs];
153          }
154
155          if(args == null)
156            args = new Vector();
157          call.addParameter("Door", XMLType.XSD_STRING,
158                            javax.xml.rpc.ParameterMode.IN);
159          params[nargs-1] = System.getProperty("user.name");
160        }
161
162        call.setReturnType(org.apache.axis.Constants.SOAP_VECTOR);
163        Vector vans = (Vector)call.invoke( params );
164
165        proganswer = new Hashtable();
166        // assumes it's even sized
167        int n = vans.size();
168        for(int j=0;j<n;j+=2)
169        {
170          String s = (String)vans.get(j);
171          if(s.equals("msg"))
172          {
173            String msg = (String)vans.get(j+1);
174            if(msg.startsWith("Failed Authorisation"))
175              throw new JembossSoapException("Authentication Failed");
176            else if(msg.startsWith("Error"))
177              throw new JembossSoapException(msg);
178          }
179          proganswer.put(s,vans.get(j+1));
180        }
181      }
182      catch (Exception e)
183      {
184         System.out.println("Exception in PrivateRequest "+
185                             e.getMessage ()
186                            );
187         e.printStackTrace();
188         throw new JembossSoapException("  Fault Code   = " + e);
189      }
190
191    }
192
193   /**
194   *
195   * Change the contents of a hashtable to a vector
196   * @return     contents of the hash
197   *
198   */
199   private Vector getVector(Hashtable h)
200   {
201     Vector v = new Vector();
202     for(Enumeration e = h.keys() ; e.hasMoreElements() ;)
203     {
204       String s = (String)e.nextElement();
205       v.add(s);
206       v.add(h.get(s));
207     }
208
209     return v;
210   }
211
212
213   /**
214   *
215   * Gets an element out of the embreo result hash
216   * @param val  key to look up
217   * @return     the element, or an empty String if there is no
218   *             element that matches the key
219   *
220   */
221   public Object getVal(String val)
222   {
223     if (proganswer.containsKey(val))
224       return proganswer.get(val);
225     else
226       return "";
227   }
228
229   /**
230   *
231   * Get the results returned by the server call
232   * @return     results
233   *
234   */
235   public Hashtable getHash()
236   {
237     return proganswer;
238   }
239 }
240