From 3d7387ca0f48a62db152867d03d338a3dbbbe800 Mon Sep 17 00:00:00 2001 From: Dinh Nguyen Date: Tue, 3 Oct 2023 11:40:23 +0700 Subject: [PATCH] Update AdobeSignService.java --- .../connector/service/AdobeSignService.java | 189 ++++++++---------- 1 file changed, 85 insertions(+), 104 deletions(-) diff --git a/adobe-esign-connector/src/com/axonivy/connector/adobe/esign/connector/service/AdobeSignService.java b/adobe-esign-connector/src/com/axonivy/connector/adobe/esign/connector/service/AdobeSignService.java index 0076d60..013aff1 100644 --- a/adobe-esign-connector/src/com/axonivy/connector/adobe/esign/connector/service/AdobeSignService.java +++ b/adobe-esign-connector/src/com/axonivy/connector/adobe/esign/connector/service/AdobeSignService.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.Objects; -import javax.servlet.http.HttpServletRequest; - import com.axonivy.connector.adobe.esign.connector.rest.DownloadResult; import com.axonivy.connector.adobe.esign.connector.rest.UploadWrapper; @@ -38,23 +36,22 @@ import ch.ivyteam.ivy.environment.Ivy; import ch.ivyteam.ivy.process.call.SubProcessCall; import ch.ivyteam.ivy.process.call.SubProcessCallResult; -import ch.ivyteam.ivy.request.IHttpRequest; /** * Service class to call Adobe Sign services and create necessary objects - * + * * @author jpl * */ public class AdobeSignService { private static final AdobeSignService instance = new AdobeSignService(); - + // process paths, signature and parameter names private static final String SERVICE_BASE = "connector/"; private static final String TRANSIENT_DOCUMENTS_SERVICE = SERVICE_BASE + "TransientDocuments"; private static final String AGREEMENTS_SERVICE = SERVICE_BASE + "Agreements"; - + private static final String CREATE_AGREEMENT_SIGNATURE = "createAgreement(AgreementCreationInfo)"; private static final String GET_AGREEMENT_SIGNATURE = "getAgreementById(String)"; private static final String GET_DOCUMENTS_SIGNATURE = "getDocuments(String)"; @@ -62,23 +59,23 @@ public class AdobeSignService { private static final String DOWNLOAD_SIGNATURE = "dowloadDocument(String, String, String, Boolean)"; private static final String SIGNING_URL_SIGNATURE = "getSigningURLs(String,String)"; private static final String FORMFIELD_SIGNATURE = "addFormFields(String, FormFieldPutInfo)"; - + private static final String ID_PARAM = "id"; private static final String ERROR_PARAM = "error"; private static final String UPLOAD_PARAM = "upload"; private static final String AGREEMENT_PARAM = "agreement"; private static final String AGREEMENT_ID_PARAM = "agreementId"; - + private AdobeSignService() {} // locked constructor - + public static AdobeSignService getInstance() { return instance; } - + /** * Convenience method to call upload document and create agreement methods - * + * * @param name * @param workflowId * @param signerEmail @@ -89,10 +86,10 @@ public String uploadDocumentAndCreateSimpleAgreement(String name, String signerE String documentId = uploadDocument(upload); return createAgreement(buildSimpleAgreement(name, documentId, signerEmail)); } - + /** * Convenience method to call upload document and create agreement methods - * + * * @param name * @param workflowId * @param signerEmail @@ -103,13 +100,13 @@ public String uploadDocumentAndCreateSimpleAgreementWithFormFields(String name, String documentId = uploadDocument(upload); String agreementId = createAgreement( buildSimpleAgreementWithFormFields(name, Arrays.asList(documentId), signerEmail, formFieldGenerators)); - + return agreementId; } - + /** * Helper method to create agreement object as required for the REST service - * + * * @param name * @param workflowId * @param documentId @@ -119,10 +116,10 @@ public String uploadDocumentAndCreateSimpleAgreementWithFormFields(String name, public AgreementCreationInfo buildSimpleAgreement(String name, String documentId, String signerEmail) { return buildSimpleAgreementWithFormFields(name, Arrays.asList(documentId), signerEmail, null); } - + /** * Helper method to create agreement object as required for the REST service - * + * * @param name * @param workflowId * @param documentId @@ -132,34 +129,31 @@ public AgreementCreationInfo buildSimpleAgreement(String name, String documentId public AgreementCreationInfo buildSimpleAgreementWithFormFields(String name, List documentIds, String signerEmail, List formFieldGenerators) { AgreementCreationInfo agreement = new AgreementCreationInfo(); - + agreement.setName(name); agreement.setMessage("Please sign this document!"); agreement.setSignatureType(SignatureTypeEnum.ESIGN); agreement.setState(StateEnum.IN_PROCESS); - + // add signers agreement.setParticipantSetsInfo(createParticipantInfoForEmail(Arrays.asList(signerEmail), RoleEnum.SIGNER)); - + // add documentIds - need to be already transferred with the upload document service agreement.setFileInfos(createFileInfosForDocumentIds(documentIds)); - agreement.setEmailOption(createAllDisabledSendOptions()); - - String baseUrl = getRequestBaseUrl(); - String fullUrl = baseUrl + Ivy.var().get("adobe-sign-connector.returnPage"); - agreement.postSignOption(new AgreementsPostSignOption().redirectUrl(fullUrl)); - + agreement.postSignOption( + new AgreementsPostSignOption().redirectUrl(Ivy.var().get("adobe-sign-connector.returnPage"))); + if(Objects.nonNull(formFieldGenerators)) { agreement.setFormFieldGenerators(formFieldGenerators); } - + return agreement; } - + /** * Helper method to create agreement object for two groups of signers as required for the REST service - * + * * @param name * @param workflowId * @param documentId @@ -169,10 +163,10 @@ public AgreementCreationInfo buildSimpleAgreementWithFormFields(String name, Lis public AgreementCreationInfo buildSimpleAgreementFor2SignerGroups(String name, String documentId, List signers1, List signers2) { return buildSimpleAgreementFor2ParticipantGroups(name, documentId, signers1, RoleEnum.SIGNER, signers2, RoleEnum.SIGNER); } - + /** * Helper method to create agreement for two groups of participants object as required for the REST service - * + * * @param name * @param workflowId * @param documentId @@ -182,91 +176,96 @@ public AgreementCreationInfo buildSimpleAgreementFor2SignerGroups(String name, S public AgreementCreationInfo buildSimpleAgreementFor2ParticipantGroups(String name, String documentId, List participants1, RoleEnum participants1Role, List participants2, RoleEnum participants2Role) { AgreementCreationInfo agreement = new AgreementCreationInfo(); - + agreement.setName(name); agreement.setMessage("Please sign this document!"); agreement.setSignatureType(SignatureTypeEnum.ESIGN); agreement.setState(StateEnum.IN_PROCESS); - + // add signers agreement.setParticipantSetsInfo(createParticipantInfoForEmail(participants1, participants1Role)); agreement.getParticipantSetsInfo().add(createParticipants(participants2, participants2Role, 2)); - + // add documentIds - need to be already transferred with the upload document service agreement.setFileInfos(createFileInfosForDocumentIds(Arrays.asList(documentId))); - agreement.setEmailOption(createAllDisabledSendOptions()); - - String baseUrl = getRequestBaseUrl(); - String fullUrl = baseUrl + Ivy.var().get("adobe-sign-connector.returnPage"); - agreement.postSignOption(new AgreementsPostSignOption().redirectUrl(fullUrl)); - + agreement.postSignOption( + new AgreementsPostSignOption().redirectUrl(Ivy.var().get("adobe-sign-connector.returnPage"))); + return agreement; } /** * Wrapper method to call sub process to post a new agreement - * + * * @param agreement * @return * @throws BpmError */ public String createAgreement(AgreementCreationInfo agreement) throws BpmError { - SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE).withStartSignature(CREATE_AGREEMENT_SIGNATURE).withParam(AGREEMENT_PARAM, agreement).call(); - + SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE) + .withStartSignature(CREATE_AGREEMENT_SIGNATURE) + .withParam(AGREEMENT_PARAM, agreement) + .call(); handleError(callResult); AgreementCreationResponse agreementResponse = callResult.get("agreementInfo", AgreementCreationResponse.class); - + return agreementResponse.getId(); } /** * Wrapper method to call sub process to get agreement by its id - * + * * @param agreementId * @return */ public AgreementInfo getAgreement(String agreementId) { - SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE).withStartSignature(GET_AGREEMENT_SIGNATURE).withParam(AGREEMENT_ID_PARAM, agreementId).call(); - + SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE) + .withStartSignature(GET_AGREEMENT_SIGNATURE) + .withParam(AGREEMENT_ID_PARAM, agreementId) + .call(); + handleError(callResult); return callResult.get("agreementInfo", AgreementInfo.class); } - + /** * Wrapper method to call sub process to get list of documents for an agreement - * + * * @param agreementId * @return */ public AgreementDocuments getDocuments(String agreementId) { - SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE).withStartSignature(GET_DOCUMENTS_SIGNATURE).withParam(AGREEMENT_ID_PARAM, agreementId).call(); - + SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE) + .withStartSignature(GET_DOCUMENTS_SIGNATURE) + .withParam(AGREEMENT_ID_PARAM, agreementId) + .call(); handleError(callResult); return callResult.get("documents", AgreementDocuments.class); } - + /** * Wrapper method to call sub process to retrieve the signing URIs of an agreement - * + * * @param agreementId * @param frameParent * @return */ @SuppressWarnings("unchecked") public List getSigningURIs(String agreementId, String frameParent) { - SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE).withStartSignature(SIGNING_URL_SIGNATURE) + SubProcessCallResult callResult = SubProcessCall.withPath(AGREEMENTS_SERVICE) + .withStartSignature(SIGNING_URL_SIGNATURE) .withParam(AGREEMENT_ID_PARAM, agreementId) .withParam("frameParent", frameParent) .call(); - + handleError(callResult); return (List)callResult.get("signingURIs"); } - + /** * Wrapper method to call sub process to upload a new transient document required to create a new agreement - * + * * @param upload * @return * @throws BpmError @@ -274,28 +273,28 @@ public List getSigningURIs(String agreemen public String uploadDocument(String filename, byte[] bytes) throws BpmError { return uploadDocument(new UploadWrapper(filename, bytes)); } - + /** * Wrapper method to call sub process to upload a new transient document required to create a new agreement - * + * * @param upload * @return * @throws BpmError */ public String uploadDocument(UploadWrapper upload) throws BpmError { String documentId; - + SubProcessCallResult callResult = SubProcessCall.withPath(TRANSIENT_DOCUMENTS_SERVICE).withStartSignature(UPLOAD_SIGNATURE).withParam(UPLOAD_PARAM, upload).call(); - + handleError(callResult); documentId = callResult.get(ID_PARAM, String.class); - + return documentId; } - + /** * Wrapper method to call sub process to download a document of an agreement - * + * * @param agreementId * @param documentId * @param filename @@ -309,14 +308,14 @@ public DownloadResult downloadDocument(String agreementId, String documentId, St .withParam("filename", filename) .withParam("asFile", asFile) .call(); - + handleError(callResult); return callResult.get("download", DownloadResult.class); } - + /** * Wrapper method to call sub process to add form fields to an agreement - * + * * @param agreementId * @param formFieldInfo */ @@ -325,28 +324,28 @@ public void addFormFieldsToAgreement(String agreementId, FormFieldPutInfo formFi .withParam("agreementId", agreementId) .withParam("formFieldInfo", formFieldInfo) .call(); - + handleError(callResult); } - + /** * creates participant information for a list of email addresses and role as required for the REST services - * + * * @param signerEmails * @param role - * @param order + * @param order * @return */ public List createParticipantInfoForEmail(List signerEmails, RoleEnum role) { return createParticipantInfoForEmail(signerEmails, role, 1); } - + /** * creates participant information for a list of email addresses and role as required for the REST services - * + * * @param signerEmails * @param role - * @param order + * @param order * @return */ public List createParticipantInfoForEmail(List signerEmails, RoleEnum role, Integer order) { @@ -358,7 +357,7 @@ public List createParticipantInfoForEm /** * creates participants for a list of email addresses - * + * * @param signerEmails * @param role * @param order @@ -368,28 +367,28 @@ public AgreementCreationInfoParticipantSetsInfo createParticipants(List Integer order) { AgreementCreationInfoParticipantSetsInfo participant = new AgreementCreationInfoParticipantSetsInfo(); List memberInfos = new ArrayList<>(); - + for(String email : signerEmails) { AgreementCreationInfoMemberInfos member = new AgreementCreationInfoMemberInfos(); member.setEmail(email); memberInfos.add(member); } - + participant.setMemberInfos(memberInfos); participant.setRole(role); participant.setOrder(order); return participant; } - + /** * creates file info for a list of document ids - * + * * @param documentIds * @return */ public List createFileInfosForDocumentIds(List documentIds) { List fileInfos = new ArrayList<>(); - + for(String id : documentIds) { AgreementsFileInfos fileInfo = new AgreementsFileInfos(); fileInfo.transientDocumentId(id); @@ -397,7 +396,7 @@ public List createFileInfosForDocumentIds(List docu } return fileInfos; } - + public AgreementsFormFieldGenerators createFormFieldWithAnchor(String anchorText, ContentTypeEnum contentType, Double offsetX, Double offsetY) { return new AgreementsFormFieldGenerators().generatorType(GeneratorTypeEnum.ANCHOR_TEXT) .formFieldDescription(new AgreementsFormFieldDescription().contentType(contentType)) @@ -408,7 +407,7 @@ public AgreementsFormFieldGenerators createFormFieldWithAnchor(String anchorText /** * creates options for sending email - * + * * @param completion * @param inflight * @param init @@ -423,38 +422,20 @@ public AgreementsEmailOption createSendOptions(CompletionEmailsEnum completion, emailOptions.setSendOptions(sendOptions ); return emailOptions; } - + /** * create options for sending email with all disabled - * + * * @return */ public AgreementsEmailOption createAllDisabledSendOptions() { return createSendOptions(CompletionEmailsEnum.NONE, InFlightEmailsEnum.NONE, InitEmailsEnum.NONE); } - + private void handleError(SubProcessCallResult callResult) { BpmError error = callResult.get(ERROR_PARAM, BpmError.class); if(Objects.nonNull(error)) { throw error; } } - - /** - * Extracts the base url from actual request. - * Example: http://localhost:8081 - * @return - */ - private String getRequestBaseUrl() { - IHttpRequest req = (IHttpRequest) Ivy.request(); - HttpServletRequest request = req.getHttpServletRequest(); - StringBuilder baseUrlBuilder = new StringBuilder(); - baseUrlBuilder.append(request.getScheme()).append("://").append(request.getServerName()); - int port = request.getServerPort(); - if (port != 80 && port != 443) { - baseUrlBuilder.append(":").append(port); - } - return baseUrlBuilder.toString(); - } - }