[709306c] | 1 | // WSDL generated types and port |
---|
[55de6a9] | 2 | import edu.isi.www.fedd_types.*; |
---|
| 3 | import edu.isi.www.fedd_wsdl.*; |
---|
[709306c] | 4 | |
---|
| 5 | // The usual suspects |
---|
[10f5e84] | 6 | import java.net.*; |
---|
| 7 | import java.util.*; |
---|
| 8 | |
---|
[709306c] | 9 | // The Fedid and XTrustManager classes |
---|
| 10 | import net.deterlab.isi.*; |
---|
[55de6a9] | 11 | |
---|
[709306c] | 12 | // SSL manipulations and a couple Exceptions |
---|
[10f5e84] | 13 | import javax.net.ssl.*; |
---|
[709306c] | 14 | import javax.xml.rpc.*; |
---|
[10f5e84] | 15 | |
---|
[709306c] | 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 | */ |
---|
[10f5e84] | 21 | public class FeddCommand { |
---|
| 22 | |
---|
[709306c] | 23 | // SetUpSecurity is defined below. This is necessary stuff, and putting it |
---|
| 24 | // here guarantees that it's always called before main. |
---|
[10f5e84] | 25 | static { setUpSecurity(); } |
---|
[55de6a9] | 26 | |
---|
[709306c] | 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 | */ |
---|
[55de6a9] | 34 | static class ExperimentLabels { |
---|
[709306c] | 35 | /** The fedid */ |
---|
[55de6a9] | 36 | protected String fedid; |
---|
[709306c] | 37 | /** The localname */ |
---|
[55de6a9] | 38 | protected String localname; |
---|
| 39 | |
---|
[709306c] | 40 | /** |
---|
| 41 | * Construct the class - i.e., parse the array |
---|
| 42 | * @param experimentID an array of IDTypes holding the synonymous names |
---|
| 43 | */ |
---|
[55de6a9] | 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 | } |
---|
[709306c] | 61 | /** |
---|
| 62 | * Return the fedid |
---|
| 63 | * @return the fedid |
---|
| 64 | */ |
---|
[55de6a9] | 65 | String getFedid() { return fedid; } |
---|
[709306c] | 66 | /** |
---|
| 67 | * Return the localname |
---|
| 68 | * @return the localname |
---|
| 69 | */ |
---|
[55de6a9] | 70 | String getLocalname() { return localname; } |
---|
| 71 | } |
---|
| 72 | |
---|
[709306c] | 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 | */ |
---|
[55de6a9] | 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); |
---|
[10f5e84] | 90 | |
---|
| 91 | } |
---|
| 92 | |
---|
[709306c] | 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 | */ |
---|
[10f5e84] | 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); } |
---|
[55de6a9] | 111 | } |
---|
| 112 | |
---|
[709306c] | 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 { |
---|
[55de6a9] | 123 | /* |
---|
| 124 | * Boilerplate web services access stuff. |
---|
| 125 | */ |
---|
[10f5e84] | 126 | clearSSLSessionCache(); |
---|
[55de6a9] | 127 | FeddServiceLocator service = new FeddServiceLocator(); |
---|
| 128 | FeddPortType port = service.getfeddPort(new URL(server)); |
---|
| 129 | |
---|
| 130 | return port; |
---|
| 131 | } |
---|
| 132 | } |
---|