source: axis/FeddCommand.java @ 9b76f1a

axis_examplecompt_changesinfo-ops
Last change on this file since 9b76f1a was 709306c, checked in by Ted Faber <faber@…>, 14 years ago

Commented and working.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1// WSDL generated types and port
2import edu.isi.www.fedd_types.*;
3import edu.isi.www.fedd_wsdl.*;
4
5// The usual suspects
6import java.net.*;
7import java.util.*;
8
9// The Fedid and XTrustManager classes
10import net.deterlab.isi.*;
11
12// SSL manipulations and a couple Exceptions
13import javax.net.ssl.*;
14import javax.xml.rpc.*;
15
16/**
17 * Base class that all the example programs are derived from.  It holds
18 * routines that several classes use, including some key routines to massage
19 * java's security model closer to fedd's.
20 */
21public class FeddCommand {
22
23    // SetUpSecurity is defined below. This is necessary stuff, and putting it
24    // here guarantees that it's always called before main.
25    static { setUpSecurity(); }
26
27    /**
28     * Parse out an experiment name from a return value.  Several fedd
29     * responses identify the experiment that was operated on (or created) by
30     * both a human-readable name and a fedid.  This class scans through the
31     * array of IDTypes returned and keeps the fedid and localname (mnemonic
32     * name) returned.
33     */
34    static class ExperimentLabels {
35        /** The fedid */
36        protected String fedid;
37        /** The localname */
38        protected String localname;
39
40        /**
41         * Construct the class - i.e., parse the array
42         * @param experimentID an array of IDTypes holding the synonymous names
43         */
44        public ExperimentLabels(IDType[] experimentID) {
45            fedid = null;
46            localname = null;
47
48            for (IDType id: experimentID) {
49                if (id.getLocalname() != null) localname = id.getLocalname();
50                if (id.getFedid() != null) {
51                    byte[] rawFedid = id.getFedid();
52
53                    if (rawFedid != null) {
54                        Fedid f = new Fedid(rawFedid);
55
56                        if ( f != null) fedid =  f.toString().substring(6);
57                    }
58                }
59            }
60        }
61        /**
62         * Return the fedid
63         * @return the fedid
64         */
65        String getFedid() { return fedid; }
66        /**
67         * Return the localname
68         * @return the localname
69         */
70        String getLocalname() { return localname; }
71    }
72
73    /**
74     * This magic convinces the SSL routines to accept self-signed certificates
75     * from the server (fedd) and points the SSL routines at the local
76     * keystore.  Other applications may move the keystore assignment, but the
77     * XTrustProvider call is always necessary.
78     */
79    public static void setUpSecurity() {
80        /* This magic turns off certificate chain checking. */
81        XTrustProvider.install();
82
83        /* This tells the SSL system where to find client certificate
84         * information. */
85        String keyStore = "./keystore";
86        String keyStorePassword = "importkey";
87
88        System.setProperty("javax.net.ssl.keyStore", keyStore);
89        System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
90
91    }
92
93    /**
94     * Clear the SSL session cache.  Java aggressively reuses SSL sessions, and
95     * it confuses fedd greatly - connections drop.  This routine invalidates
96     * all the existing sessions.  Necessary when you will make more than one
97     * call.  This is also called by getPort, so getting a new port before each
98     * new call will also atomize the sessions.
99     */
100    static public void clearSSLSessionCache() {
101        try { 
102            SSLContext sctxt = SSLContext.getDefault();
103            SSLSessionContext ssctxt = sctxt.getClientSessionContext();
104            Enumeration<byte[]> e = ssctxt.getIds();
105            while (e.hasMoreElements()) {
106                SSLSession s = ssctxt.getSession(e.nextElement());
107                s.invalidate();
108            }
109        }
110        catch (Exception e) { System.err.println(e); }
111    }
112
113    /**
114     * Get a new FeddPortType on which requests can be made.  The server to
115     * contact is passed as a string.
116     * @param server a String containing the server URL
117     * @return a FeddPortType to talk to.
118     * @throws ServiceException if the services/server can't be found
119     * @throws MalformedURLException if the URL is bad
120     */
121    public static FeddPortType getPort(String server) 
122            throws ServiceException, MalformedURLException {
123        /*
124         * Boilerplate web services access stuff.
125         */
126        clearSSLSessionCache();
127        FeddServiceLocator service = new FeddServiceLocator();
128        FeddPortType port = service.getfeddPort(new URL(server));
129
130        return port;
131    }
132}
Note: See TracBrowser for help on using the repository browser.