Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Navigator - Web ServicesI could use some simple java examples of how to login and call W/S

Solved Go to solution

Navigator - Web ServicesI could use some simple java examples of how to login and call W/S

New Contributor

I am using the java.net and java.URL packages, simple code examples to login and make a service call would be helpful

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Navigator - Web ServicesI could use some simple java examples of how to login and call W/S

New Contributor

Figured it out....

Used the Authenticator class, see below

 

    public static void main(String[] args) {
        try {
            
                Authenticator.setDefault(new CustomAuthenticator());
                 
                URL url = new URL("http://localhost:7187/api/v1/entities?query=type:file&limit=2&offset=10");
                 
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                 
            }
            catch (MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
            }
            catch (IOException e) {
                System.out.println("I/O Error: " + e.getMessage());
            }
             
        }
         
        public static class CustomAuthenticator extends Authenticator {
             
            protected PasswordAuthentication getPasswordAuthentication() {
                 
                String username = "admin";
                String password = "admin";
    
                return new PasswordAuthentication(username, password.toCharArray());
                             
        }
                 
    }

1 REPLY 1

Re: Navigator - Web ServicesI could use some simple java examples of how to login and call W/S

New Contributor

Figured it out....

Used the Authenticator class, see below

 

    public static void main(String[] args) {
        try {
            
                Authenticator.setDefault(new CustomAuthenticator());
                 
                URL url = new URL("http://localhost:7187/api/v1/entities?query=type:file&limit=2&offset=10");
                 
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                 
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                in.close();
                 
            }
            catch (MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
            }
            catch (IOException e) {
                System.out.println("I/O Error: " + e.getMessage());
            }
             
        }
         
        public static class CustomAuthenticator extends Authenticator {
             
            protected PasswordAuthentication getPasswordAuthentication() {
                 
                String username = "admin";
                String password = "admin";
    
                return new PasswordAuthentication(username, password.toCharArray());
                             
        }
                 
    }