/*
 * Copyright (c) 2017 by OpenText Corporation. All Rights Reserved.
 */
package com.opentext.ia.sdk.sample.authenticate;

import com.opentext.ia.sdk.support.http.Header;
import com.opentext.ia.sdk.support.http.HttpClient;
import com.opentext.ia.sdk.support.http.apache.ApacheHttpClient;
import com.opentext.ia.sdk.support.http.rest.AuthenticationStrategy;
import com.opentext.ia.sdk.support.http.rest.GatewayInfo;
import com.opentext.ia.sdk.support.http.rest.JwtAuthentication;


public final class Authenticate {

  private static final String CLIENT_SECRET = "secret";
  private static final String CLIENT_ID = "infoarchive.cli";
  private static final String PASSWORD = "password";
  private static final String USER_NAME = "sue@iacustomer.com";
  private static final String GATEWAY_URL = "http://localhost:8888";

  private Authenticate() { }

  @SuppressWarnings("PMD.SystemPrintln")
  public static void main(String[] args) {
    HttpClient httpClient = new ApacheHttpClient();
    try {
      // Log in to the gateway with user name & password
      AuthenticationStrategy authentication = new JwtAuthentication(USER_NAME, PASSWORD,
          new GatewayInfo(GATEWAY_URL, CLIENT_ID, CLIENT_SECRET), httpClient);

      // Get an authentication header using a token provided by the gateway
      Header header = authentication.issueAuthHeader();

      // Use this header in calls using the HTTP client
      System.out.println(header);
    } finally {
      httpClient.close();
    }
  }

}

@@ -118,16 +118,65 @@
 icate;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import c
@@ -525,16 +525,41 @@
 tion;%0A%0A%0A
+@SuppressWarnings(%22PMD%22)%0A
 public f
@@ -619,31 +619,36 @@
 ing 
-CLIENT_SECRET
+SETTING_USERNAME
  = %22
+u
 se
-cret
+rname
 %22;%0A 
@@ -680,36 +680,103 @@
 ing 
-CLIENT_ID = %22infoarchive.cli
+SETTING_PASSWORD = %22password%22;%0A  private static final String SETTING_GATEWAY_URL = %22gateway.url
 %22;%0A 
@@ -804,35 +804,45 @@
  String 
-PASSWORD = %22passwor
+SETTING_CLIENT_ID = %22client.i
 d%22;%0A  pr
@@ -871,41 +871,192 @@
 ing 
-U
 SE
-R_NAME = %22sue@iacustomer.com%22;
+TTING_CLIENT_SECRET = %22client.secret%22;%0A  // Make sure you have a running InfoArchive cluster with the following characteristics,%0A  // or set system properties with the correct values
 %0A  p
@@ -1075,23 +1075,192 @@
 c final 
+Map%3C
 String
- 
+, String%3E DEFAULT_SETTINGS = new HashMap%3CString, String%3E() %7B%7B%0A    put(SETTING_USERNAME, %22sue@iacustomer.com%22);%0A    put(SETTING_PASSWORD, %22password%22);%0A    put(SETTING_
 GATEWAY_
@@ -1262,18 +1262,17 @@
 EWAY_URL
- =
+,
  %22http:/
@@ -1287,85 +1287,140 @@
 st:8
-888%22;%0A%0A  private Authenticate() %7B %7D%0A%0A  @SuppressWarnings(%22PMD.SystemPrintln%22)
+080%22);%0A    put(SETTING_CLIENT_ID, %22infoarchive.cli%22);%0A    put(SETTING_CLIENT_SECRET, %22secret%22);%0A  %7D%7D;%0A%0A  private Authenticate() %7B %7D%0A
 %0A  p
@@ -1650,27 +1650,29 @@
 ion(
+get(SETTING_
 USER
-_
 NAME
-, PASSWORD
+)
 ,%0A  
@@ -1678,16 +1678,39 @@
         
+ get(SETTING_PASSWORD),
  new Gat
@@ -1718,16 +1718,28 @@
 wayInfo(
+get(SETTING_
 GATEWAY_
@@ -1745,21 +1745,57 @@
 _URL
+)
 , 
-CLIENT_ID, 
+get(SETTING_CLIENT_ID),%0A          get(SETTING_
 CLIE
@@ -1804,16 +1804,17 @@
 _SECRET)
+)
 , httpCl
@@ -2097,10 +2097,138 @@
  %7D%0A  %7D%0A%0A
+  private static String get(String name) %7B%0A    return System.getProperty(name, DEFAULT_SETTINGS.getOrDefault(name, name));%0A  %7D%0A%0A
 %7D%0A

Allow setting credentials via system properties