|
@@ -19,49 +19,114 @@
|
|
|
package org.apache.hadoop.hdfs.web;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
|
import org.apache.hadoop.classification.InterfaceStability;
|
|
|
+import org.apache.hadoop.hdfs.web.resources.HttpOpParam;
|
|
|
+import org.apache.hadoop.security.UserGroupInformation;
|
|
|
+import org.apache.hadoop.security.authentication.client.AuthenticatedURL;
|
|
|
+import org.apache.hadoop.security.authentication.client.AuthenticationException;
|
|
|
+import org.apache.hadoop.security.authentication.client.ConnectionConfigurator;
|
|
|
|
|
|
/**
|
|
|
* Utilities for handling URLs
|
|
|
*/
|
|
|
-@InterfaceAudience.LimitedPrivate({"HDFS"})
|
|
|
+@InterfaceAudience.LimitedPrivate({ "HDFS" })
|
|
|
@InterfaceStability.Unstable
|
|
|
public class URLConnectionFactory {
|
|
|
+ private static final Log LOG = LogFactory.getLog(URLConnectionFactory.class);
|
|
|
+
|
|
|
+ /** SPNEGO authenticator */
|
|
|
+ private static final KerberosUgiAuthenticator AUTH = new KerberosUgiAuthenticator();
|
|
|
+
|
|
|
/**
|
|
|
* Timeout for socket connects and reads
|
|
|
*/
|
|
|
- public final static int DEFAULT_SOCKET_TIMEOUT = 1*60*1000; // 1 minute
|
|
|
+ public final static int DEFAULT_SOCKET_TIMEOUT = 1 * 60 * 1000; // 1 minute
|
|
|
+
|
|
|
+ public static final URLConnectionFactory DEFAULT_CONNECTION_FACTORY = new URLConnectionFactory(
|
|
|
+ DEFAULT_SOCKET_TIMEOUT);
|
|
|
|
|
|
- public static final URLConnectionFactory DEFAULT_CONNECTION_FACTORY = new URLConnectionFactory(DEFAULT_SOCKET_TIMEOUT);
|
|
|
-
|
|
|
private int socketTimeout;
|
|
|
|
|
|
+ /** Configure connections for AuthenticatedURL */
|
|
|
+ private ConnectionConfigurator connConfigurator = new ConnectionConfigurator() {
|
|
|
+ @Override
|
|
|
+ public HttpURLConnection configure(HttpURLConnection conn)
|
|
|
+ throws IOException {
|
|
|
+ URLConnectionFactory.setTimeouts(conn, socketTimeout);
|
|
|
+ return conn;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
public URLConnectionFactory(int socketTimeout) {
|
|
|
this.socketTimeout = socketTimeout;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Opens a url with read and connect timeouts
|
|
|
- * @param url to open
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * to open
|
|
|
* @return URLConnection
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public URLConnection openConnection(URL url) throws IOException {
|
|
|
URLConnection connection = url.openConnection();
|
|
|
- setTimeouts(connection);
|
|
|
- return connection;
|
|
|
+ if (connection instanceof HttpURLConnection) {
|
|
|
+ connConfigurator.configure((HttpURLConnection) connection);
|
|
|
+ }
|
|
|
+ return connection;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Opens a url with read and connect timeouts
|
|
|
+ *
|
|
|
+ * @param url URL to open
|
|
|
+ * @return URLConnection
|
|
|
+ * @throws IOException
|
|
|
+ * @throws AuthenticationException
|
|
|
+ */
|
|
|
+ public URLConnection openConnection(HttpOpParam.Op op, URL url)
|
|
|
+ throws IOException, AuthenticationException {
|
|
|
+ if (op.getRequireAuth()) {
|
|
|
+ if (LOG.isDebugEnabled()) {
|
|
|
+ LOG.debug("open AuthenticatedURL connection" + url);
|
|
|
+ }
|
|
|
+ UserGroupInformation.getCurrentUser().checkTGTAndReloginFromKeytab();
|
|
|
+ final AuthenticatedURL.Token authToken = new AuthenticatedURL.Token();
|
|
|
+ return new AuthenticatedURL(AUTH, connConfigurator).openConnection(url,
|
|
|
+ authToken);
|
|
|
+ } else {
|
|
|
+ if (LOG.isDebugEnabled()) {
|
|
|
+ LOG.debug("open URL connection");
|
|
|
+ }
|
|
|
+ return openConnection(url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ConnectionConfigurator getConnConfigurator() {
|
|
|
+ return connConfigurator;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setConnConfigurator(ConnectionConfigurator connConfigurator) {
|
|
|
+ this.connConfigurator = connConfigurator;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Sets timeout parameters on the given URLConnection.
|
|
|
*
|
|
|
- * @param connection URLConnection to set
|
|
|
+ * @param connection
|
|
|
+ * URLConnection to set
|
|
|
+ * @param socketTimeout
|
|
|
+ * the connection and read timeout of the connection.
|
|
|
*/
|
|
|
- public void setTimeouts(URLConnection connection) {
|
|
|
+ static void setTimeouts(URLConnection connection, int socketTimeout) {
|
|
|
connection.setConnectTimeout(socketTimeout);
|
|
|
connection.setReadTimeout(socketTimeout);
|
|
|
}
|