View Javadoc
1   package com.guinetik.examples;
2   
3   import com.guinetik.rr.RocketRest;
4   import com.guinetik.rr.RocketRestOptions;
5   import com.guinetik.rr.RocketRestSecureConfig;
6   import com.guinetik.rr.auth.AuthStrategy;
7   import com.guinetik.rr.auth.AuthStrategyFactory;
8   import com.guinetik.rr.auth.RocketSSL;
9   
10  import javax.net.ssl.HttpsURLConnection;
11  import javax.net.ssl.SSLContext;
12  import java.util.Scanner;
13  
14  /**
15   * Example demonstrating how to call the ADP API using:
16   * 1. OAuth2 Client-Credentials flow (client_id / client_secret)
17   * 2. Mutual TLS with a PKCS#12 certificate handled via {@link RocketSSL}
18   */
19  public class AdpApiExample implements Example {
20  
21      // ====== User-supplied parameters ======
22      private String clientId;
23      private String clientSecret;
24      private String certificatePath;
25      private String certificatePassword;
26  
27      // ====== Constant ADP endpoints ======
28      private String tokenUrl;
29      private String baseUrl;
30      private String endpoint; // resource path
31  
32      static {
33          // Basic logging – keep the output similar to other examples
34          System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG");
35      }
36  
37      public static void main(String[] args) {
38          AdpApiExample example = new AdpApiExample();
39          // Read credentials from environment variables
40          example.clientId = System.getenv("ADP_CLIENT_ID");
41          example.clientSecret = System.getenv("ADP_CLIENT_SECRET");
42          example.certificatePath = System.getenv("ADP_CERTIFICATE_FILE");
43          example.certificatePassword = System.getenv("ADP_CERTIFICATE_PASSWORD");
44          example.tokenUrl = "https://api.adp.com/auth/oauth/v2/token";
45          example.baseUrl = "https://api.adp.com";
46          example.endpoint = "/codelists/hr/v3/position-seeker-management/applicant-onboard-templates/WFN/1";
47          example.run();
48      }
49  
50      @Override
51      public String getName() {
52          return "ADP API (OAuth2 + SSL) Example";
53      }
54  
55      @Override
56      public void run() {
57          System.out.println("\n=== ADP API Integration Demo ===");
58          System.out.println("This example demonstrates connecting to ADP's API using RocketRest with:");
59          System.out.println("1. Custom SSL Certificate (mutual TLS) - Required for most enterprise APIs");
60          System.out.println("2. OAuth2 Client Credentials flow - Secure authentication using client ID/secret");
61          System.out.println();
62          System.out.println("The demo will:");
63          System.out.println("- Load your PKCS12 (.p12) certificate and configure SSL");
64          System.out.println("- Request an OAuth2 access token from ADP");
65          System.out.println("- Make a sample API call to ADP's API");
66          System.out.println("- Display the raw response from the server");
67          System.out.println();
68          try (Scanner scanner = new Scanner(System.in)) {
69              // Prompt only for missing values
70              if (clientId == null || clientId.trim().isEmpty()) {
71                  System.out.print("Client ID: ");
72                  clientId = scanner.nextLine();
73              }
74              if (clientSecret == null || clientSecret.trim().isEmpty()) {
75                  System.out.print("Client Secret: ");
76                  clientSecret = scanner.nextLine();
77              }
78              if (certificatePath == null || certificatePath.trim().isEmpty()) {
79                  System.out.print("Certificate (.p12) Path: ");
80                  certificatePath = scanner.nextLine();
81              }
82              if (certificatePassword == null || certificatePassword.trim().isEmpty()) {
83                  System.out.print("Certificate Password: ");
84                  certificatePassword = scanner.nextLine();
85              }
86              if (endpoint == null || endpoint.trim().isEmpty()) {
87                  System.out.print("API Endpoint (press Enter for default): ");
88                  String input = scanner.nextLine();
89                  if (!input.trim().isEmpty()) {
90                      endpoint = input.trim();
91                  }
92              }
93          }
94  
95          try {
96              // ------------------ SSL setup ------------------
97              RocketSSL.SSLCertificate certInfo = new RocketSSL.SSLCertificate(certificatePath, certificatePassword);
98              RocketSSL sslUtil = new RocketSSL();
99              SSLContext sslContext = sslUtil.getSSLContext(certInfo);
100             if (sslContext == null) {
101                 System.err.println("Failed to create SSL context – aborting example.");
102                 return;
103             }
104             // Make our SSL context the default so every HttpsURLConnection uses it
105             HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
106             // ------------------ Auth strategy ------------------
107             AuthStrategy authStrategy = AuthStrategyFactory.createOAuth2ClientCredentials(
108                     clientId,
109                     clientSecret,
110                     tokenUrl
111             );
112             // Ensure token retrieval uses the same SSL context
113             if (authStrategy instanceof RocketSSL.SSLAware) {
114                 ((RocketSSL.SSLAware) authStrategy).configureSsl(sslContext);
115             }
116             System.out.println("Refreshing ADP access token...");
117             authStrategy.refreshCredentials();
118             // ------------------ RocketRest configuration ------------------
119             RocketRestSecureConfig config = RocketRestSecureConfig.secureBuilder(baseUrl)
120                     .withCustomCertificate(certificatePath, certificatePassword)
121                     .authStrategy(authStrategy)
122                     .tokenUrl(tokenUrl)
123                     .defaultOptions(opts -> {
124                         opts.set(RocketRestOptions.LOG_REQUEST_BODY, true);
125                         opts.set(RocketRestOptions.LOG_RESPONSE_BODY, true);
126                     })
127                     .build();
128             // ------------------ Make the request ------------------
129             RocketRest client = new RocketRest(config);
130             System.out.println("Calling ADP API: " + endpoint);
131             String response = client.get(endpoint, String.class);
132             System.out.println("\nAPI Response:\n" + response);
133             client.shutdown();
134         } catch (Exception e) {
135             System.err.println("Error: " + e.getMessage());
136             e.printStackTrace();
137         }
138     }
139 }