OliverH | Posts: 28

Spring Boot app with OneSpan Sign RESTful API

0 votes
Hi, I'm looking to integrate the OneSpan Sign API with our web application using Spring Boot. Is there any sample code available to help me get started? Thanks,

Duo_Liang | Posts: 3776

Reply to: Spring Boot app with OneSpan Sign RESTful API

1 votes
Hi there, Go check this sample project I've built. Simply replace your OneSpan Sign related information in OssService.java class. The workflow was easy that it uses hardcoded dummy package information to create packages, then generates signer sessions and embed it into an iFrame. It develops in REST API so you can easily understand and extend it per your own business logic. Hope this could help! Duo

Duo_Liang | Posts: 3776

Reply to: Spring Boot app with OneSpan Sign RESTful API

1 votes
The project exceeds the size limitation so that I hosted it at this Code Share. Duo

sumantasaha | Posts: 11

Reply to: Spring Boot app with OneSpan Sign RESTful API

0 votes
Hi Thanks for the code share. I was reading through the code. It uses the traditional method of using HttpUrlConnection, OutputStream, etc. However, I was eager to know if the same can be implemented using Spring Rest Template. I have implemented but I was getting 400 bad request. Any help on this would be highly beneficial. Thanks Sumanta

Duo_Liang | Posts: 3776

Reply to: Spring Boot app with OneSpan Sign RESTful API

2 votes

Hey Sumanta, Yes, for sure OneSpan Sign API works with RestTemplate. Like I replied in this thread, could do an extra error handling for your Spring RestTemplate something like below:

		
               try {
			ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, Package.class, params);    
			System.out.println(result);
		} catch (HttpStatusCodeException exception) {
			int statusCode = exception.getStatusCode().value();
			String errorMessage = exception.getResponseBodyAsString();
			System.out.println(statusCode + " : " + errorMessage);
		}

Which would receive the error stream from OneSpan Sign which contains the exact error message. Then we can troubleshoot the cause of the error. Hope this could help! Duo


sumantasaha | Posts: 11

Reply to: Spring Boot app with OneSpan Sign RESTful API

2 votes
Hey Duo, Your reply is appreciated. I have gone through the code as well. I wanted to create a package in Onespan using rest template. The code you shared is a GET API. I needed your help for POST API. Please find the code below
HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
			httpHeaders.add(Constants.HeaderKeys.AUTHORIZATION, Constants.AUTH_TYPE + " "
					+ propertyLoader.getAppProperties().getProperty(Constants.PropertyKeys.ONESPAN_API_KEY));
			httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
			httpHeaders.add("Accept", "application/json; esl-api-version=11.0");
			
			
			MultiValueMap multipartFileMap = new LinkedMultiValueMap>();
			MultiValueMap payloadMap = new LinkedMultiValueMap>();
			MultiValueMap requestDataMap = new LinkedMultiValueMap>();
			
			Iterator attachmentIterator = apttusAttachmentDocList.iterator();
			while (attachmentIterator.hasNext()) {
				AttachedDocumentFileBean attachedDocumentFile = (AttachedDocumentFileBean) attachmentIterator.next();
				InputStream inputStream = apttusAPIBean.getDocumentInfoForDocumentId(attachedDocumentFile.getId());
				logger.info("Input Stream Apttus API :: " + inputStream);
				if (inputStream == null) {
					logger.error("File content not found...");
					throw new ApttusAPIExecutionException("Failed to get the file content");
				}
				//ByteArrayResource byteArrayResource = new ByteArrayResource(IOUtils.toByteArray(inputStream));
				ContentDisposition contentDisposition = ContentDisposition.builder("form-data")
						.name(Constants.FILE_KEY)
						.filename(attachedDocumentFile.getName())
						.build();
				logger.info("Content Disposition :: " + contentDisposition.toString());
				multipartFileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
				multipartFileMap.add(HttpHeaders.CONTENT_TYPE, "application/pdf");
				HttpEntity fileContent = new HttpEntity>(IOUtils.toByteArray(inputStream), multipartFileMap);
				logger.info("Http Entity File Content" + fileContent);
				requestDataMap.add(Constants.FILE_KEY, fileContent);
				
				fileSet.add(attachedDocumentFile.getName());
			}			
			
			ArtifactBean artefact = payloadBean.prepareArtefactWithRoles(packagePayload, fileSet);
			ObjectMapper objectMapper = new ObjectMapper();
			String payload = objectMapper.writeValueAsString(artefact);
			logger.info("Payload Json For Artefact :: " + payload);
			
			ContentDisposition payloadContentDisposition = ContentDisposition.builder("form-data")
					.name(Constants.PAYLOAD_KEY)
					.build();
			payloadMap.add(HttpHeaders.CONTENT_DISPOSITION, payloadContentDisposition.toString());
			payloadMap.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
			HttpEntity payloadContent = new HttpEntity>(payload, payloadMap);
			
			requestDataMap.add(Constants.PAYLOAD_KEY, payloadContent);
			logger.info("Http Entity Payload Content" + payloadContent);
			
			
			HttpEntity> requestEntity = new HttpEntity>(requestDataMap, httpHeaders);
			
			logger.info("Http Entity Request Content" + requestEntity);
			
			String PACKAGE_CREATION_URL= Constants.OneSpanAPIEndPoints.POST_PACKAGE_ESIGNATURE;
			UriComponentsBuilder packageCreateApiUrlBuilder = UriComponentsBuilder.fromHttpUrl(PACKAGE_CREATION_URL);
			PACKAGE_CREATION_URL = packageCreateApiUrlBuilder.build().encode().toUri().toString();
			logger.info("Start to call onespan package creation api...");
			logger.info("Package Creation URL :: " + PACKAGE_CREATION_URL);
			ResponseEntity packageCreateResponse = restTemplate.exchange(PACKAGE_CREATION_URL, 
					HttpMethod.POST, 
					requestEntity,
					OneSpanPackageBean.class);
Thanks Sumanta

Duo_Liang | Posts: 3776

Reply to: Spring Boot app with OneSpan Sign RESTful API

0 votes
Hey Sumanta, I was suggesting to use code like below to cope with error handling:
		
try {
			ResponseEntity response = restTemplate.exchange(PACKAGE_CREATION_URL,
					HttpMethod.POST, requestEntity, OneSpanPackageBean.class);
			return response.getBody();
		} catch (HttpStatusCodeException exception) {
			return exception.getResponseBodyAsString();
		}
And for a working full code, here's a function used to create a transaction(with hardcoded metadata):
	
@PostMapping("/upload")
	public String uploadFile(){
		MultiValueMap parts = new LinkedMultiValueMap();
		parts.add("file", "123");
		parts.add("payload",
				"{\"roles\":[{\"id\":\"Role1\",\"signers\":[{\"email\":\"[email protected]\",\"firstName\":\"1.firstname\",\"lastName\":\"1.lastname\",\"company\":\"OneSpan Sign\"}]},{\"id\":\"Role2\",\"signers\":[{\"email\":\"[email protected]\",\"firstName\":\"2.firstname\",\"lastName\":\"2.lastname\",\"company\":\"OneSpan Sign\"}]}],\"documents\":[{\"approvals\":[{\"role\":\"Role1\",\"fields\":[{\"page\":0,\"top\":100,\"subtype\":\"FULLNAME\",\"height\":50,\"left\":100,\"width\":200,\"type\":\"SIGNATURE\"}]},{\"role\":\"Role2\",\"fields\":[{\"page\":0,\"top\":300,\"subtype\":\"FULLNAME\",\"height\":50,\"left\":100,\"width\":200,\"type\":\"SIGNATURE\"}]}],\"name\":\"Test Document\"}],\"name\":\"Example Package\",\"type\":\"PACKAGE\",\"language\":\"en\",\"emailMessage\":\"\",\"description\":\"New Package\",\"autocomplete\":true,\"status\":\"SENT\"}");

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		headers.add("Accept", "application/json; esl-api-version=11.0");
		headers.add("Authorization", "Basic your_api_key");

		HttpEntity> requestEntity = new HttpEntity>(parts,
				headers);

		try {
			ResponseEntity response = restTemplate.exchange("https://sandbox.esignlive.com/api/packages",
					HttpMethod.POST, requestEntity, String.class);
			return response.getBody();
		} catch (HttpStatusCodeException exception) {
			return exception.getResponseBodyAsString();
		}

	}
Simply change the API_KEY and PACKAGE_CREATION_URL to yours and you'll see the output. Hope this could help! Duo

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off