-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMetacatGsiClient.java
More file actions
112 lines (100 loc) · 3.7 KB
/
MetacatGsiClient.java
File metadata and controls
112 lines (100 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package edu.ucsb.nceas.metacat.client.gsi;
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
import edu.ucsb.nceas.metacat.client.MetacatClient;
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
import edu.ucsb.nceas.utilities.HttpMessage;
import org.ietf.jgss.GSSCredential;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLStreamHandler;
import java.util.Properties;
/** An extension of the Metacat client that uses Grid Security Infrastructure
* (GSI) enabled HTTPS instead of HTTP to communicate.
*
* <p>Note that not all client deployments will include the JARs necessary to
* run this version of the Metacat client; therefore, we should make sure that
* the superclass (MetacatClient) can run even if this class can't be loaded.
* That is, catch (and log) NoClassDefFoundError, etc. */
public class MetacatGsiClient extends MetacatClient {
/** The current user's GSS credential, as an alternative to
* username/password. Needed for every connection.
* Set via {@link #login(GSSCredential)}. */
private GSSCredential credential;
private void initCredential(GSSCredential credential)
throws MetacatAuthException
{
if (credential == null)
throw new NullPointerException("Credential is null.");
if (this.credential != null)
throw new MetacatAuthException
("Credential already initialized; please create a new "
+ getClass().getName() + " to start a new session.");
this.credential = credential;
}
public String login(GSSCredential credential)
throws MetacatAuthException, MetacatInaccessibleException
{
initCredential(credential);
// code below mostly copied from super.login(username, password)
Properties prop = new Properties();
prop.put("action", "login");
prop.put("qformat", "xml");
String response;
try {
response = sendDataForString(prop, null, null, 0);
} catch (Exception e) {
throw new MetacatInaccessibleException(e);
}
if (response.indexOf("<login>") == -1) {
setSessionId("");
throw new MetacatAuthException(response);
} else {
int start = response.indexOf("<sessionId>") + 11;
int end = response.indexOf("</sessionId>");
if ((start != -1) && (end != -1)) {
setSessionId(response.substring(start,end));
}
}
return response;
}
/** Parse the Metacat URL and, if we are using a GSI credential,
* ensure that the protocol is an SSL-based one (HTTPS or HTTPG). */
private URL parseAndCheckURL() throws MetacatInaccessibleException {
try {
URL url = new URL(getMetacatUrl().trim());
if (credential != null) {
URLStreamHandler gsiHandler;
try {
gsiHandler = (URLStreamHandler) Class
.forName("org.globus.net.protocol.https.Handler")
.newInstance();
} catch (Exception e) {
throw new MetacatInaccessibleException
("Unable to create protocol handler for HTTPS+GSI.", e);
}
// reconstruct with correct handler
url = new URL(url.getProtocol(), url.getHost(), url.getPort(),
url.getFile(), gsiHandler);
}
return url;
}
catch (MalformedURLException e) {
throw new MetacatInaccessibleException
("Unable to parse URL to contact Metacat server: \""
+ getMetacatUrl() + "\".", e);
}
}
/** Create an HttpMessage that can send messages to the server.
* If using a GSI credential, use the credential to set up an SSL
* connection (HTTPS / HTTPG). If using HTTP and username/password,
* just use a regular HTTP conenction. */
protected HttpMessage createHttpMessage()
throws MetacatInaccessibleException, MetacatAuthException, IOException
{
if (credential != null)
return new HttpGsiMessage(credential, parseAndCheckURL());
else
return super.createHttpMessage();
}
}