import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class TestInboundAPI { public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException { System.out.println("Start API Call ..."); System.out.println("--------------------"); // Set the timezone to UTC for 1WorldSync API call TimeZone utc = TimeZone.getTimeZone("UTC"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); dateFormat.setTimeZone(utc); String currentDateTime = dateFormat.format(new Date()); System.out.println("Current DateTime: [" + currentDateTime + "]"); // Formatted Time Stamp LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); String dateStamp = now.format(formatter); String appID = "xxxxxxxx"; // Please update with your APP ID here (8 characters) System.out.println("AppID: [" + appID + "]"); String secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Please update with your Secret Key here (32 characters) String gln = "1234567890123"; // Please update with your 13-digit GLN here (13 digits) System.out.println("GLN: [" + gln + "]"); String user = "bu.1234567890123.xxxxxxxx"; // Please update with your BatchID here System.out.println("User: [" + user + "]"); String docID = "doc-id-must-be-unique-" + currentDateTime; // Please update with the document ID System.out.println("DocID: [" + docID + "]"); // https://product-api.1worldsync.com/V1/feed/catrequest?app_id={{pAppID}}&TIMESTAMP={{pTimeStamp}}&hash_code={{pHashCode}} String uri = "/feed/catrequest?app_id=" + appID + "&TIMESTAMP=" + currentDateTime; String hashInBase64 = generateHash(uri, secretKey); String hashInBase64URIencoded = urlencode(hashInBase64); String timeStampURIencoded = urlencode(currentDateTime); String apiUrl = "https://product-api.1worldsync.com/V1" + "/feed/catrequest?app_id=" + appID + "&TIMESTAMP=" + timeStampURIencoded + "&hash_code=" + hashInBase64URIencoded; System.out.println("URL: [" + apiUrl + "]"); System.out.println("--------------------"); try { URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/xml"); connection.setRequestProperty("userGln", gln); connection.setRequestProperty("userId", user); connection.setDoOutput(true); String xmlContent = "\n" + "\n" + "
\n" + " 0838016009225\n" + " 0838016009225\n" + " 0838016003001\n" + " {{pDocID}}\n" + " {{pStamp}}\n" + "
\n" + " \n" + "
\n" + " 0838016009225\n" + " bu.0838016009225.fv3Mwbk9\n" + "
\n" + " \n" + " {{pDocID}}\n" + " \n" + " APPEND\n" + " 10033200002137\n" + " 0838016009225\n" + " \n" + " US\n" + " {{pDocID}}\n" + " \n" + " \n" + " \n" + "
\n" + "
"; String postData = xmlContent; postData = postData.replace("{{pDocID}}",docID); postData = postData.replace("{{pStamp}}",dateStamp); System.out.println("PostData:" ); System.out.println(postData); System.out.println("--------------------"); try (OutputStream os = connection.getOutputStream()) { byte[] input = postData.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; StringBuilder response = new StringBuilder(); while ((line = br.readLine()) != null) { response.append(line); } System.out.println("API Response: [" + response.toString() + "]"); } } catch (Exception e) { e.printStackTrace(); } System.out.println("--------------------"); System.out.println("End API Call"); } private static String generateHash(String data, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException { Mac sha256Hmac = Mac.getInstance("HmacSHA256"); SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); sha256Hmac.init(secretKeySpec); byte[] hash = sha256Hmac.doFinal(data.getBytes(StandardCharsets.UTF_8)); return java.util.Base64.getEncoder().encodeToString(hash); } private static String urlencode(String value) { try { return java.net.URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); } catch (java.io.UnsupportedEncodingException ex) { throw new RuntimeException(ex.getCause()); } } /* Note: This example does not explain the contents of the documents required to add/modify/publish documents. This information can be found in the schema documentation from the Community Center at https://community.1worldsync.com/ Instead it shows the mechanism required to calculate the correct HMAC hash to complete the authentication handshack using this HMAC hash. Production ---------- receiver : 0838016003001 endpoint : https://product-api.1worldsync.com/V1/feed/catrequest envelope : Pre-Production --------------- receiver : 8380160030003 endpoint : https://product-api.preprod.1worldsync.com/V1/feed/catrequest envelope : Sample Request: POST https://product-api.1worldsync.com/V1/feed/catrequest?app_id=35df93bc&TIMESTAMP=2024-01-02T21%3A45%3A46Z&hash_code=kGZoK9y9FRRYvwJCaAvCaftFdN5Zo%2FDQPyw45H8X6Vg%3D HTTP/1.1 Host: product-api.1worldsync.com Content-Type: application/xml userGln: 0838016009225 userId: bu.0838016009225.fv3Mwbk9 Content-Length: 1255
0838016009225 0838016009225 0838016003001 health-check-0838016009225-2024-01-02T21:45:46Z 2024-01-02T21:45:46
0838016009225 bu.0838016009225.fv3Mwbk9
health-check-0838016009225-2024-01-02T21:45:46Z APPEND 10033200002137 0838016009225 US health-check-0838016009225-2022-03-09T14:44:05.158Z
Sample Response: {"data":{"responseMessage":"Request Accepted for processing. Please check the status of the request using the Response API with trackingId","trackingId":2245841},"serviceError":null,"status":200} */ }