Java Code Examples for org.apache.hadoop.security.authentication.client.AuthenticatedURL

Following code examples demonstrate how to use org.apache.hadoop.security.authentication.client.AuthenticatedURLfrom hadoop. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.hadoop.security.authentication.client.AuthenticatedURLand various code implementation of this class.

	public void DataIngest(Response multipleResponse, int i){
		AuthenticatedURL authenticatedURL = new AuthenticatedURL();
		Token token = new AuthenticatedURL.Token();
		HttpURLConnection conn;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy-hh-mm-ss-a");
		
		try {
			
				String formattedDate = sdf.format(date);
				String AuditLog= "File_Audit_Log";
				String dummy = Integer.toString(i);
				String url = Ingestor.HDFSpath+AuditLog+dummy+formattedDate.toString()+".xml"+"?user.name=hduser&op=CREATE";
				conn = authenticatedURL.openConnection(new URL(url), token);
				conn.setRequestMethod("PUT");
				conn.setDoOutput(true);
				conn.connect();

				OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
				String newLineRemoveResponse = multipleResponse.getResponseBody().replaceAll("\\n", "");
				out.write(newLineRemoveResponse );
				out.close();
				System.out.println(conn.getResponseCode());
				conn.disconnect();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		
	} 


	protected AuthenticatedURL getAuthenticatedURL() {
		if (authenticatedURL == null) {
			authenticatedURL = new AuthenticatedURL(new HDFSKerberosAuthenticator(principal, password));
		}
		return authenticatedURL;
	} 

	public static void main(String []args) throws AuthenticationException{
	AuthenticatedURL authenticatedURL = new AuthenticatedURL();
	Token token = new AuthenticatedURL.Token();
	try {
		HttpURLConnection conn;
		try {
			
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy-hh-mm-ss-a");
			String formattedDate = sdf.format(date);
			String AuditLog= "File_Audit_Log";
			int i=0;
			String dummy = Integer.toString(i);
			
			
			
			
			conn.setRequestMethod("GET");
			conn.setDoOutput(true);
			conn.connect();
			
			
			InputStream is = conn.getInputStream();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));
			String line = reader.readLine();
			while (!line.equals(null) ) {
				
				 System.out.println(line);
				 line = reader.readLine();
				 if(line.equals(null)) break;
			}
			reader.close();
			is.close();
			System.out.println(line);
			 System.out.println(conn.getResponseCode());
			conn.disconnect();
		} catch (NullPointerException  e) {
		}
		
		
		
			
	} catch (IOException e ) {
		e.printStackTrace();
		System.out.println("--111---------------");
	}
	} 

	public void DataIngest(Response multipleResponse, int i){
		AuthenticatedURL authenticatedURL = new AuthenticatedURL();
		Token token = new AuthenticatedURL.Token();
		HttpURLConnection conn;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy-hh-mm-ss-a");
		
		try {
			
				String formattedDate = sdf.format(date);
				String AuditLog= "File_Metadata";
				String dummy = Integer.toString(i);
				String url = Ingestor.HDFSpath1+AuditLog+dummy+formattedDate.toString()+".xml"+"?user.name=hduser&op=CREATE";
				conn = authenticatedURL.openConnection(new URL(url), token);
				conn.setRequestMethod("PUT");
				conn.setDoOutput(true);
				conn.connect();

				OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
				String newLineRemoveResponse = multipleResponse.getResponseBody().replaceAll("\\n", "");
				out.write(newLineRemoveResponse );
				out.close();
				System.out.println(conn.getResponseCode());
				conn.disconnect();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		
	} 

  public static void main(String[] args) {
    try {
      if (args.length != 1) {
        System.err.println("Usage: <URL>");
        System.exit(-1);
      }
      AuthenticatedURL.Token token = new AuthenticatedURL.Token();
      URL url = new URL(args[0]);
      HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);
      System.out.println();
      System.out.println("Token value: " + token);
      System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
      System.out.println();
      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
          System.out.println(line);
          line = reader.readLine();
        }
        reader.close();
      }
      System.out.println();
    }
    catch (Exception ex) {
      System.err.println("ERROR: " + ex.getMessage());
      System.exit(-1);
    }
  } 

		public static void main (String []args){
			
		
			AuthenticatedURL authenticatedURL = new AuthenticatedURL();
			Token token = new AuthenticatedURL.Token();
			try {
				HttpURLConnection conn;
				try {
					conn.setRequestMethod("PUT");
					conn.setDoOutput(true);
					conn.connect();
					
					
					OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
					out.write("abc");
					out.close();
					 System.out.println(conn.getResponseCode());
					conn.disconnect();
				} catch (AuthenticationException e) {
					e.printStackTrace();
				}
				
				
				
					
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("--111---------------");
			}
			
		} 

Advertisement
Javadoc
The AuthenticatedURL class enables the use of the JDK URL class against HTTP endpoints protected wit

h the AuthenticationFilter.

The authentication mechanisms supported by default are Hadoop Simple authentication (also known as pseudo authentication) and Kerberos SPNEGO authentication.

Additional authentication mechanisms can be supported via Authenticator implementations.

The default Authenticator is the KerberosAuthenticator class which supports automatic fallback from Kerberos SPNEGO to Hadoop Simple authentication.

AuthenticatedURL instances are not thread-safe.

The usage pattern of the AuthenticatedURL is: // establishing an initial connection URL url = new URL("http://foo:8080/bar"); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL aUrl = new AuthenticatedURL(); HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token); .... // use the 'conn' instance .... // establishing a follow up connection using a token from the previous connection HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token); .... // use the 'conn' instance ....

Read More
Advertisement