import groovy.json.JsonOutput import groovy.json.JsonSlurper import org.apache.http.client.methods.HttpPost import org.apache.http.client.methods.HttpGet import org.apache.http.entity.StringEntity import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClients import org.apache.http.util.EntityUtils import org.apache.http.client.utils.URIBuilder import de.uplanet.scripting.groovy.util.Safely import java.io.File import java.io.FileOutputStream def sbDebug = new StringBuilder() CloseableHttpClient httpclient = HttpClients.createDefault() def conn = g_dbConnections.systemConnection def stmt = null def rs = null def signatures = [] // ============================== //Data for API credential store // ============================== def API_USERNAME = g_credentials.getSecret('apiNameSkribble') def API_KEY = g_credentials.getSecret('apiKeySkribble') // ============================== // Data for API connection from sysDataGroup // ============================== def API_HOST = g_sysDg['GUID'] def strSignaturQuality = g_sysDg['GUID'] //QES, AES, AES_MINIMAL, SES oder DEMO def strLegislation = g_sysDg['GUID'] // ZERTES oder EIDAS def txtMessage = g_sysDg['GUID'] // ============================== //Data sent with the document // ============================== def STRID = g_record["GUID"].value def strTitle = g_record["GUID"].value def strEmail = g_record["GUID"].value def strFirstname = g_record["GUID"].value def strName = g_record["GUID"].value def strMobileNumber = g_record["GUID"].value def fileField = g_record["GUID"] if(strEmail) { signatures << [ "account_email": strEmail, "signer_identity_data": [ "email_address": strEmail, "first_name" : strFirstname, "last_name" : strName, "mobile_number": strMobileNumber ] ] } else { g_log.info("keine E-Mail") } // ============================== //Convert document for signing // ============================== if (!fileField || !fileField.value) { throw new RuntimeException("Keine Datei vorhanden!") } def fileDescriptor = fileField.value[0] def file = fileDescriptor.getFile() def inputStream = file.newInputStream() byte[] pdfBytes = inputStream.bytes inputStream.close() String base64Pdf = Base64.encoder.encodeToString(pdfBytes) //Authentication on Skribble def authUri = new URIBuilder() .setScheme("https") .setHost(API_HOST) .setPath("/v2/access/login") .build() HttpPost authPost = new HttpPost(authUri) authPost.setHeader("Content-Type", "application/json") def authPayload = [ username: API_USERNAME, "api-key": API_KEY ] authPost.setEntity(new StringEntity(JsonOutput.toJson(authPayload), "UTF-8")) def authResponse = httpclient.execute(authPost) def status = authResponse.getStatusLine().getStatusCode() def body = EntityUtils.toString(authResponse.getEntity()) if (status != 200) { throw new RuntimeException("Login fehlgeschlagen: ${status} - ${body}") } def accessToken = body?.trim() if (!accessToken) { throw new RuntimeException("Kein Access Token erhalten") } // ============================== //Create a Signature Package // ============================== def signPayload = [ "title" : strTitle, "message" : txtMessage, "content" : base64Pdf, "signatures" : signatures ] if (strSignaturQuality && strSignaturQuality != "DEMO") { signPayload["quality"] = strSignaturQuality if (strLegislation) { signPayload["legislation"] = strLegislation } } //Send a signature request & save the request ID def signUri = new URIBuilder() .setScheme("https") .setHost(API_HOST) .setPath("/v2/signature-requests") .build() HttpPost signPost = new HttpPost(signUri) signPost.setHeader("Content-Type", "application/json") signPost.setHeader("Authorization", "Bearer ${accessToken}") signPost.setEntity(new StringEntity(JsonOutput.toJson(signPayload), "UTF-8")) def signResponse = httpclient.execute(signPost) def statusCode = signResponse.getStatusLine().getStatusCode() def signBody = EntityUtils.toString(signResponse.getEntity()) if (statusCode != 200 && statusCode != 201) { throw new Exception("Signature Request fehlgeschlagen! Status: ${statusCode}\n${signBody}") } def signJson = new JsonSlurper().parseText(signBody) strRequestID = signJson.id if (!strRequestID) { throw new Exception("Keine Request-ID erhalten!") } g_log.info(strRequestID) g_sharedState.put("requestId", strRequestID) // ============================== // STATUS CHECK // ============================== def statusUri = new URIBuilder() .setScheme("https") .setHost(API_HOST) .setPath("/v2/signature-requests/${strRequestID}") .build() def statusGet = new HttpGet(statusUri) statusGet.setHeader("Authorization", "Bearer ${accessToken}") def statusResponse = httpclient.execute(statusGet) def statusBody = EntityUtils.toString(statusResponse.getEntity()) def json = new JsonSlurper().parseText(statusBody) def strOverallStatus = json.status_overall stmt = g_dbQuery.prepare(conn, """ UPDATE DATAGROUP('GUID') SET STR_DOCSTATUS=? WHERE STRID=? """) stmt.setString(1, strOverallStatus) stmt.setString(2, STRID) stmt.executeUpdate() stmt = Safely.close(stmt)