Webhook Controller
Create a Controller Class
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
public class WebhookController {
@Autowired
private EformSignService eformSignService;
public static JsonNode token;
private static Map<String, Object> map = new HashMap<>();
private static JsonNode res;
private static String downPath = "C:\\Users\\Public\\Downloads\\";
private static String member_id = "[email protected]";
private static String fileType = "document,audit_trail";
private static String apiUrl;
private static String accessToken;
private static String refreshToken;
private static String expiredAt;
@PostMapping(value = "/webhook")
public void postBody(
@RequestHeader("eformsign_signature") String signature,
@RequestBody String body) throws Exception {
System.out.println(" Webhook Header: " + signature);
System.out.println(" Body: " + body);
boolean valid = EformSignService.verifySignature(signature, body);
System.out.println(" verifySignature: " + valid);
if (valid) {
JSONParser jsonParse = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParse.parse(body);
String event_type = (String) jsonObj.get("event_type");
JSONObject doc = (JSONObject) jsonObj.get(event_type);
String document_status = (String) doc.get("document_status");
if(token == null) {
res = eformSignService.getAccessToken(member_id).block();
saveToken();
System.out.println(" getAccessToken: " + token);
} else if ( System.currentTimeMillis() > token.get("expiredAt").asLong()) {
System.out.println(" Token expired." );
res = eformSignService.getRefreshToken(token).block();
saveToken();
System.out.println(" getRefreshToken: " + res);
}
if(event_type.equals("ready_document_pdf") && document_status.equals("doc_complete")) {
String documentId = (String) doc.get("document_id");
byte[] bytes = eformSignService.downloadDocument(token, documentId, fileType).block();
Path path = Paths.get( downPath + documentId + ".zip");
Files.write(path, bytes);
System.out.println(" downloadDocument: " + path.getFileName() + " (" + Files.size(path) + " bytes)");
res = eformSignService.deleteDocument(token, documentId).block();
System.out.println(" deleteDocument: " + downPath+documentId+".zip" + res);
}
}
}
private static void saveToken() {
try {
apiUrl = res.get("api_key").get("company").get("api_url").asText();
} catch (NullPointerException e) {}
accessToken = res.get("oauth_token").get("access_token").asText();
refreshToken = res.get("oauth_token").get("refresh_token").asText();
int expiresIn = res.get("oauth_token").get("expires_in").asInt();
expiredAt = (((new Date().getTime() + (expiresIn - 3580) * 1000L)) + "");
if (apiUrl != null) map.put("apiUrl", apiUrl);
map.put("accessToken", accessToken);
map.put("refreshToken", refreshToken);
map.put("expiredAt", expiredAt);
ObjectMapper objectMapper = new ObjectMapper();
token = objectMapper.valueToTree(map);
}
Last updated