From cc83eabc89a054e28dd7af36b6c48aa943eade4b Mon Sep 17 00:00:00 2001 From: Ananya Banerjee Date: Wed, 8 Sep 2021 11:53:48 +0530 Subject: [PATCH] Adding helper classes to consume Twistage import API and ingesting signature --- .../webhook/controller/WebhookController.java | 7 ++- .../webhook/helper/AuthenticationHelper.java | 61 +++++++++++++++++++ .../hyland/webhook/helper/ImportHelper.java | 46 ++++++++++++++ src/main/resources/application.properties | 3 +- 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/hyland/webhook/helper/AuthenticationHelper.java create mode 100644 src/main/java/com/hyland/webhook/helper/ImportHelper.java diff --git a/src/main/java/com/hyland/webhook/controller/WebhookController.java b/src/main/java/com/hyland/webhook/controller/WebhookController.java index 0607037..6a65d57 100644 --- a/src/main/java/com/hyland/webhook/controller/WebhookController.java +++ b/src/main/java/com/hyland/webhook/controller/WebhookController.java @@ -27,8 +27,7 @@ public String getNotification() { @PostMapping public ResponseEntity onRecordingCompleted(@RequestBody String requestBody, @RequestHeader(value = WebHookConstants.HOST) String host, @RequestHeader(value = WebHookConstants.USER_AGENT) String userAgent, @RequestHeader(value = WebHookConstants.AUTHORIZATION) String authToken) { - log.info("#### Incoming Webhook Notification from Zoom API ##### {}", requestBody); - ObjectMapper mapper = new ObjectMapper(); + log.debug("#### Incoming Webhook Notification from Zoom API ##### {}", requestBody); log.debug("#### Request Header Information ##### Host :: User Agent :: {} {} ", host, userAgent); try { @@ -38,12 +37,16 @@ public ResponseEntity onRecordingCompleted(@RequestBody String requestBo return new ResponseEntity<>(requestBody, HttpStatus.FORBIDDEN); } //Convert JSON object to Java POJO + ObjectMapper mapper = new ObjectMapper(); RecordingCompletedSchema recordingObject = mapper.readValue(requestBody, RecordingCompletedSchema.class); log.debug("Converted Json Payload to Object"); // Validate if the event notification is for Recording Completed event subscribed if (recordingObject.getEvent().equals(WebHookConstants.RECORDING_COMPLETED)) { log.debug("Recording Completed Event Payload {} ::", recordingObject); + String contributor = "admin"; + String library = "XXXX"; + String licenseKey = "XXXX"; } diff --git a/src/main/java/com/hyland/webhook/helper/AuthenticationHelper.java b/src/main/java/com/hyland/webhook/helper/AuthenticationHelper.java new file mode 100644 index 0000000..02837b4 --- /dev/null +++ b/src/main/java/com/hyland/webhook/helper/AuthenticationHelper.java @@ -0,0 +1,61 @@ +package com.hyland.webhook.helper; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; + +public class AuthenticationHelper { + + private AuthenticationHelper(){ + } + + protected static String getViewSignature(String licenseKey) { + return fetch ("https://indiaservice.twistage.com/api/view_key?licenseKey=" + + licenseKey).trim(); + } + + protected static String getIngestSignature(String licenseKey, String contributor, String library) { + return fetch ("https://indiaservice.twistage.com/api/ingest_key?licenseKey=" + + licenseKey + "&contributor=" + contributor + "&library_id=" + library).trim(); + } + + protected static String fetch(String url) { + try { + Object content = createUrl(url).getContent(); + if (content instanceof String) { + return (String) content; + } else if (content instanceof InputStream) { + return getInputStreamContents((InputStream) content); + } else { + throw new RuntimeException ("Unexpected content returned from URL: " + + content.toString()); + } + } catch (IOException e) { + throw new RuntimeException (e); + } + } + + protected static URL createUrl(String url) { + try { + return new URL(url); + } catch (MalformedURLException e) { + throw new RuntimeException (e); + } + } + + protected static String getInputStreamContents(InputStream inputStream) throws IOException { + StringBuilder contents = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { + String line = null; + while ((line = bufferedReader.readLine()) != null) { + contents.append(line); + contents.append(System.getProperty("line.separator")); + } + } + return contents.toString(); + } + +} diff --git a/src/main/java/com/hyland/webhook/helper/ImportHelper.java b/src/main/java/com/hyland/webhook/helper/ImportHelper.java new file mode 100644 index 0000000..3c1a441 --- /dev/null +++ b/src/main/java/com/hyland/webhook/helper/ImportHelper.java @@ -0,0 +1,46 @@ +package com.hyland.webhook.helper; + +import java.io.*; +import java.net.HttpURLConnection; + +import static com.hyland.webhook.helper.AuthenticationHelper.*; + +public class ImportHelper { + + protected String upload (String licenseKey, String contributor, String library, + String pathOfXmlFile) + throws IOException { + + String xml = getInputStreamContents (new FileInputStream(new File (pathOfXmlFile))); + + String auth = getIngestSignature(licenseKey, contributor, library); + + String url = "https://indiaservice.twistage.com/videos/create_many?signature=" + auth; + + InputStream inputStream = new ByteArrayInputStream(xml.getBytes()); + + HttpURLConnection conn = (HttpURLConnection) createUrl(url).openConnection(); + + conn.setDoInput (true); + conn.setDoOutput (true); + conn.setUseCaches (false); + + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "text/xml"); + + BufferedInputStream bufferedInput = new BufferedInputStream(inputStream); + BufferedOutputStream bufferedOutput = new BufferedOutputStream(conn.getOutputStream()); + + int nextByte; + while ((nextByte = bufferedInput.read ()) != -1) { + bufferedOutput.write (nextByte); + } + + bufferedOutput.flush (); + bufferedOutput.close (); + + // read the server's response + return getInputStreamContents (conn.getInputStream()); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 389b2d9..694eb88 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,4 @@ zoom.verification.token=pZwChq6bS5KstopcfycnDA -logging.level.org.springframework.security: DEBUG +logging.level.org.springframework.security= DEBUG +twistage.api.licenseKey=2f5752b1a0d1a