Hello! I need to use WebHook in order to receive real time information from a provider (ClassMarker.com) for an "online-course-exam-tracking" purpose. The code that I need to run is shown below (provided by the provided with nice documentation in this link: https://www.classmarker.com/online-testing/api/webhooks/ ), but my only one error is the line #1, that results to relate to a "package" reserved word (see image of the error). I've been reading the forum and found in this post (https://support.wix.com/en/article/corvid-working-with-npm-packages) some information about "nmp packages" that frankly I'm not sure if it's the same thing, but presuming that IT IS the same thing, I tried to search in NPM PACKAGE library to found my provider (ClassMarker.com) with no results; So this is my question and request:
How can I make this work, and if something has to be formally requested for "evaluation" in order to add this package to the approved nmp package list, How should I proceed?
thanks a lot for your help.
package com.classmarker.webhook;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Date;
import java.util.Objects;
import java.util.Enumeration;
import java.util.stream.Collectors;
import java.util.Base64;
@WebServlet("/webhook")
public class WebhookServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
// You are given a uniquе secret code when crеating a Wеbhook.
String classmarkerSecretPhrase = "YOUR_CLASSMARKER_WEBHOOK_SECRET_PHRASE";
String jsonStringPayload = httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
String headerHmacSignature = httpServletRequest.getHeader("x-classmarker-hmac-sha256");
boolean isVerifiedSuccessfully = false;
try {
isVerifiedSuccessfully = verifyWebhook(jsonStringPayload, headerHmacSignature, classmarkerSecretPhrase);
} catch (InvalidKeyException | NoSuchAlgorithmException exception) {
// handle еxception hеre
}
if (isVerifiedSuccessfully) {
saveToFile("json_data_" + new Date() + ".json", jsonStringPayload);
// Notify ClassMarker you have rеceived the Wеbhook.
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
}
else{
httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
private boolean verifyWebhook(String jsonData, String headerHmacSignature, String classmarkerSecretPhrase) throws InvalidKeyException, NoSuchAlgorithmException {
byte[] hashHMacBytes = hashHMac(jsonData, classmarkerSecretPhrase);
String calculatedSignature = Base64.getEncoder().encodeToString(hashHMacBytes);
return Objects.equals(headerHmacSignature, calculatedSignature);
}
private byte[] hashHMac(String sourceString, String key) throws InvalidKeyException, NoSuchAlgorithmException {
String HMAC_SHA256 = "HmacSHA256";
Mac sha512HMAC = Mac.getInstance(HMAC_SHA256);
byte[] keyBytes = key.getBytes(StandardCharsets.US_ASCII);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, HMAC_SHA256);
sha512HMAC.init(secretKeySpec);
return sha512HMAC.doFinal(sourceString.getBytes(StandardCharsets.US_ASCII));
}
private void saveToFile(String fileName, String content) throws IOException {
File file = new File(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
// If filе doesn't еxists, then crеate it.
if (!file.exists()) {
file.createNewFile();
}
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
}
}
$w.onReady(function () {
// TODO: write your page related code here...
});
Good point, I haven't even realized of this! just noticed that all the code had no errors, except for line one. Any suggestions on how to convert Java to JavaScript in order to make it to work?
You can only use JavaScript with Corvid. You can't use Java or other languages.