List of usage examples for org.apache.hadoop.security.authentication.client AuthenticatedURL injectToken
public static void injectToken(HttpURLConnection conn, Token token)
From source file:org.apache.oozie.client.AuthOozieClient.java
License:Apache License
/** * Create an authenticated connection to the Oozie server. * <p>/*from www . j a v a 2 s .c o m*/ * It uses Hadoop-auth client authentication which by default supports * Kerberos HTTP SPNEGO, Pseudo/Simple and anonymous. * <p> * if the Java system property {@link #USE_AUTH_TOKEN_CACHE_SYS_PROP} is set to true Hadoop-auth * authentication token will be cached/used in/from the '.oozie-auth-token' file in the user * home directory. * * @param url the URL to open a HTTP connection to. * @param method the HTTP method for the HTTP connection. * @return an authenticated connection to the Oozie server. * @throws IOException if an IO error occurred. * @throws OozieClientException if an oozie client error occurred. */ @Override protected HttpURLConnection createConnection(URL url, String method) throws IOException, OozieClientException { boolean useAuthFile = System.getProperty(USE_AUTH_TOKEN_CACHE_SYS_PROP, "false").equalsIgnoreCase("true"); AuthenticatedURL.Token readToken = new AuthenticatedURL.Token(); AuthenticatedURL.Token currentToken = new AuthenticatedURL.Token(); if (useAuthFile) { readToken = readAuthToken(); if (readToken != null) { currentToken = new AuthenticatedURL.Token(readToken.toString()); } } if (currentToken.isSet()) { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("OPTIONS"); AuthenticatedURL.injectToken(conn, currentToken); if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { AUTH_TOKEN_CACHE_FILE.delete(); currentToken = new AuthenticatedURL.Token(); } } if (!currentToken.isSet()) { Authenticator authenticator = getAuthenticator(); try { new AuthenticatedURL(authenticator).openConnection(url, currentToken); } catch (AuthenticationException ex) { AUTH_TOKEN_CACHE_FILE.delete(); throw new OozieClientException(OozieClientException.AUTHENTICATION, "Could not authenticate, " + ex.getMessage(), ex); } } if (useAuthFile && currentToken.isSet() && !currentToken.equals(readToken)) { writeAuthToken(currentToken); } HttpURLConnection conn = super.createConnection(url, method); AuthenticatedURL.injectToken(conn, currentToken); return conn; }