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. * *
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("