Skip to content

Commit

Permalink
4.8.11 digi locker impl v3 (#510)
Browse files Browse the repository at this point in the history
* Adding the digiLocker impl for the new certficate Impl

* application.properties changes
  • Loading branch information
Sahil-tarento authored Mar 18, 2024
1 parent 89de371 commit 54628ba
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 66 deletions.
33 changes: 0 additions & 33 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@
<groupId>com.github.kenglxn.qrgen</groupId>
<artifactId>javase</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svggen</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-dom</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
Expand All @@ -150,29 +140,6 @@
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.guicedee.services/org.apache.fop -->
<dependency>
<groupId>com.guicedee.services</groupId>
<artifactId>org.apache.fop</artifactId>
<version>1.1.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svg-dom</artifactId>
<version>1.14</version> <!-- Or the latest version -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-util -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-util</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.14</version>
</dependency>
<!-- End of PDF and QR Code dependency -->
<dependency>
<groupId>org.sunbird</groupId>
<artifactId>cloud-store-sdk</artifactId>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,12 @@ public void setRedisWheeboxKey(String redisWheeboxKey) {
@Value("${digilocker-api-key}")
private String digiLockerAPIKey;

@Value("${pdf-generator-service-host}")
private String pdfGeneratorServiceBaseUrl;

@Value("${pdf-generator-svg-to-pdf-url}")
private String pdfGeneratorSvgToPdfUrl;

public boolean qListFromCacheEnabled() {
return qListFromCacheEnabled;
}
Expand Down Expand Up @@ -2391,4 +2397,20 @@ public String getDigiLockerAPIKey() {
public void setDigiLockerAPIKey(String digiLockerAPIKey) {
this.digiLockerAPIKey = digiLockerAPIKey;
}

public String getPdfGeneratorServiceBaseUrl() {
return pdfGeneratorServiceBaseUrl;
}

public void setPdfGeneratorServiceBaseUrl(String pdfGeneratorServiceBaseUrl) {
this.pdfGeneratorServiceBaseUrl = pdfGeneratorServiceBaseUrl;
}

public String getPdfGeneratorSvgToPdfUrl() {
return pdfGeneratorSvgToPdfUrl;
}

public void setPdfGeneratorSvgToPdfUrl(String pdfGeneratorSvgToPdfUrl) {
this.pdfGeneratorSvgToPdfUrl = pdfGeneratorSvgToPdfUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.fop.svg.PDFTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.sunbird.cassandra.utils.CassandraOperation;
import org.sunbird.common.model.SBApiResponse;
import org.sunbird.common.service.OutboundRequestHandlerServiceImpl;
Expand All @@ -24,10 +22,7 @@

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -44,6 +39,8 @@ public class DigiLockerIntegrationServiceImpl implements DigiLockerIntegrationSe

@Autowired
CassandraOperation cassandraOperation;
@Autowired
private RestTemplate restTemplate;

@Autowired
OutboundRequestHandlerServiceImpl outboundRequestHandlerService;
Expand Down Expand Up @@ -258,7 +255,7 @@ private String getCertificate(String certificateId) {
if (MapUtils.isNotEmpty(certificateInfo)) {
byte[] out = null;
try {
out = generatePdfFromSvg(decodeUrl((String) certificateInfo.get("printUri")));
out = generatePdfFromSvg((String) certificateInfo.get("printUri"));
if (out != null)
return encodeBytesToBase64(out);
else {
Expand All @@ -276,35 +273,32 @@ private String encodeBytesToBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}

public byte[] generatePdfFromSvg(String svgContent) throws IOException, TranscoderException {
String svgContentDecode = svgContent.replace("data:image/svg+xml,", "");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgContentDecode.getBytes()));
TranscoderOutput output = new TranscoderOutput(outputStream);
PDFTranscoder transcoder = new PDFTranscoder();
transcoder.transcode(input, output);
return outputStream.toByteArray();
} catch (Exception e) {
logger.error("Error while generating pdf from svg: ", e);
}
return null;
}
public byte[] generatePdfFromSvg(String svgContent) throws IOException {
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

Map<String, Object> request = new HashMap<>();
request.put("inputFormat", "svg");
request.put("outputFormat", "pdf");
request.put("printUri", svgContent);

HttpEntity<Object> entity = new HttpEntity<>(request, headers);
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(
serverProperties.getPdfGeneratorServiceBaseUrl() + serverProperties.getPdfGeneratorSvgToPdfUrl(),
HttpMethod.POST,
entity,
byte[].class);

private static String decodeUrl(String encodedUrl) throws UnsupportedEncodingException {
StringBuilder decodedUrl = new StringBuilder();
int index = 0;
while (index < encodedUrl.length()) {
char currentChar = encodedUrl.charAt(index);
if (currentChar == '%') {
String hex = encodedUrl.substring(index + 1, index + 3);
decodedUrl.append((char) Integer.parseInt(hex, 16));
index += 3;
if (responseEntity.getStatusCode() == HttpStatus.OK) {
return responseEntity.getBody();
} else {
decodedUrl.append(currentChar);
index++;
logger.error("Issue while get the data and response is: ", responseEntity);
}
} catch (Exception e) {
logger.error("Issue while get the data from pdf generator repo", e);
}
return decodedUrl.toString();
return null;
}

private boolean addUpdateDigiLockerLookup(Map<String, Object> userDocInfo) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,6 @@ report.property.file.allMdo=ContentCompetencyMapping.csv
operational.reports.passwordlength=15
operational.reports.unzip.password=123456
es.default.result.limit=250

pdf-generator-service-host=http://pdf-generator-service:3000
pdf-generator-svg-to-pdf-url=/public/v8/course/batch/cert/download/mobile

0 comments on commit 54628ba

Please sign in to comment.