Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 17 upgrade #132

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/code-climate-test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
uses: actions/checkout@v2

# Setup Java Environment
- name: Set up JDK 1.8
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 1.8
java-version: 17

# Cache maven dependencies
- name: Cache Java Dependencies
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# jag-document-utils

[![Maintainability](https://api.codeclimate.com/v1/badges/a0b23562b87853f9824b/maintainability)](https://codeclimate.com/github/bcgov/jag-document-utils/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a0b23562b87853f9824b/test_coverage)](https://codeclimate.com/github/bcgov/jag-document-utils/test_coverage)
[![Lifecycle:Stable](https://img.shields.io/badge/Lifecycle-Stable-97ca00)](https://img.shields.io/badge/Lifecycle-Stable-97ca00) [![Maintainability](https://api.codeclimate.com/v1/badges/a0b23562b87853f9824b/maintainability)](https://codeclimate.com/github/bcgov/jag-document-utils/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a0b23562b87853f9824b/test_coverage)](https://codeclimate.com/github/bcgov/jag-document-utils/test_coverage)

Justice Sector PDF Merge Microservice integration with Adobe Experience Manager

Expand Down
70 changes: 41 additions & 29 deletions src/document-utils-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<version>3.3.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>ca.bc.gov.open</groupId>
<artifactId>document-utils-api</artifactId>
<version>0.1.0</version>
<version>1.0.0</version>
<name>document-utils-api</name>
<description>A RESTful Document Merging Service</description>

<properties>
<java.version>1.8</java.version>
<log4j2.version>2.17.1</log4j2.version>
<springfox.version>2.9.2</springfox.version>
<java.version>17</java.version>
<log4j2.version>2.24.1</log4j2.version>
<springfox.version>3.0.0</springfox.version>
<org.apache.axis.version>1.4</org.apache.axis.version>
<commons-io.version>2.8.0</commons-io.version>
<commons-io.version>2.14.0</commons-io.version>
<commons-discovery.version>0.5</commons-discovery.version>
<org.apache.pdfbox.version>2.0.21</org.apache.pdfbox.version>
<com.adobe.livecycle.version>11.0.0</com.adobe.livecycle.version>
<com.adobe.aem.version>6.5.0</com.adobe.aem.version>
<org.jacoco.version>0.8.6</org.jacoco.version>
<org.jacoco.version>0.8.12</org.jacoco.version>
<net.rakugakibox.spring.boot.version>2.7.1</net.rakugakibox.spring.boot.version>
<io.opentracing.contrib.version>0.3.2</io.opentracing.contrib.version>
<org.keycloak.version>11.0.3</org.keycloak.version>
Expand Down Expand Up @@ -66,17 +66,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand Down Expand Up @@ -105,12 +94,6 @@
<version>${io.opentracing.contrib.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>${org.apache.axis.version}</version>
</dependency>

<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
Expand Down Expand Up @@ -160,16 +143,45 @@
<artifactId>wsdl4j</artifactId>
</dependency>

<!-- keycloak -->
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>${org.keycloak.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.corba</groupId>
<artifactId>glassfish-corba-omgapi</artifactId>
<version>4.2.1</version>
</dependency>

<dependency>
<groupId>dev.akkinoc.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>

<!-- Non updatable but required to run -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>${org.apache.axis.version}</version>
</dependency>

<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.4.0</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package ca.bc.gov.open.jag.documentutils.adobe;

import java.io.IOException;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import ca.bc.gov.open.jag.documentutils.adobe.models.MergeDoc;
import ca.bc.gov.open.jag.documentutils.api.MediaTypes;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeRequest;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeResponse;
import ca.bc.gov.open.jag.documentutils.exception.MergeException;
import ca.bc.gov.open.jag.documentutils.utils.PDFBoxUtilities;
import com.adobe.idp.Document;
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.livecycle.assembler.client.AssemblerOptionSpec;
Expand All @@ -23,13 +16,18 @@
import com.adobe.livecycle.docconverter.client.DocConverterServiceClient;
import com.adobe.livecycle.docconverter.client.PDFAConversionOptionSpec;
import com.adobe.livecycle.docconverter.client.PDFAConversionResult;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import ca.bc.gov.open.jag.documentutils.adobe.models.MergeDoc;
import ca.bc.gov.open.jag.documentutils.api.MediaTypes;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeRequest;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeResponse;
import ca.bc.gov.open.jag.documentutils.exception.MergeException;
import ca.bc.gov.open.jag.documentutils.utils.PDFBoxUtilities;
import java.io.IOException;
import java.util.Base64;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;


@Service
Expand Down Expand Up @@ -152,7 +150,7 @@ private LinkedList<MergeDoc> convertToLinkedList(DocMergeRequest request) {

private MergeDoc buildMergeDoc(ca.bc.gov.open.jag.documentutils.api.models.Document doc, DocMergeRequest request) {

byte[] docBytes = Base64Utils.decode(doc.getData().getBytes());
byte[] docBytes = Base64.getDecoder().decode(doc.getData().getBytes());

if (request.getOptions().isForcePDFAOnLoad() && PDFBoxUtilities.isPDFXfa(docBytes)) {
logger.info("forcePDFA is on and document, order {}, is XFA. Converting to PDF/A...", doc.getIndex());
Expand All @@ -165,7 +163,7 @@ private MergeDoc buildMergeDoc(ca.bc.gov.open.jag.documentutils.api.models.Docum

private String buildOutputDocument(Document document) {
try {
return Base64Utils.encodeToString(IOUtils.toByteArray(document.getInputStream()));
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(document.getInputStream()));
} catch (IOException e) {
logger.error("Error creating pdf a ", e);
throw new MergeException("Error creating a pdf", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import ca.bc.gov.open.jag.documentutils.adobe.AemService;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeRequest;
import ca.bc.gov.open.jag.documentutils.api.models.DocMergeResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
Expand All @@ -14,8 +14,6 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;
import javax.validation.Valid;

/**
*
Expand All @@ -28,7 +26,6 @@
@RestController
@Validated
@RequestMapping("document")
@Api(tags = "Document Api")
@Scope("request")
public class DocumentController {

Expand All @@ -40,7 +37,6 @@ public DocumentController(AemService aemService) {
this.aemService = aemService;
}

@ApiOperation("Merge a collection of PDF Document")
@PostMapping(value = {"/merge" },
consumes = MediaTypes.APPLICATION_JSON,
produces = MediaTypes.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import jakarta.validation.Valid;

import javax.validation.Valid;
import java.util.List;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;

@JsonPropertyOrder({ "index", "data", "title" })
public class Document {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import javax.validation.constraints.NotNull;
import jakarta.validation.constraints.NotNull;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ca.bc.gov.open.jag.documentutils.security;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationToken> {

private final JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter =
new JwtGrantedAuthoritiesConverter();

public static final String KEYCLOAK_PRINCIPLE_ATTRIBUTE = "preferred_username";
public static final String KEYCLOAK_RESOURCE_ATTRIBUTE = "resource_access";
public static final String KEYCLOAK_ROLE_ATTRIBUTE = "roles";

@Value("${jwt.auth.converter.resource-id}")
private String resourceId;

@Override
public AbstractAuthenticationToken convert(@NonNull Jwt jwt) {
Collection<GrantedAuthority> authorities = Stream.concat(
jwtGrantedAuthoritiesConverter.convert(jwt).stream(),
extractResourceRoles(jwt).stream()
).collect(Collectors.toSet());

return new JwtAuthenticationToken(
jwt,
authorities,
getPrincipleClaimName(jwt)
);
}

private String getPrincipleClaimName(Jwt jwt) {
return jwt.getClaim(KEYCLOAK_PRINCIPLE_ATTRIBUTE);
}

private Collection<? extends GrantedAuthority> extractResourceRoles(Jwt jwt) {
Map<String, Object> resourceAccess;
Map<String, Object> resource;
Collection<String> resourceRoles;

Collection<? extends GrantedAuthority> resourceRoles1;
if (jwt.getClaim(KEYCLOAK_RESOURCE_ATTRIBUTE) == null) {
return Set.of();
}
resourceAccess = jwt.getClaim(KEYCLOAK_RESOURCE_ATTRIBUTE);

if (resourceAccess.get(resourceId) == null) {
return Set.of();
}
resource = (Map<String, Object>) resourceAccess.get(resourceId);

resourceRoles = (Collection<String>) resource.get(KEYCLOAK_ROLE_ATTRIBUTE);
return resourceRoles
.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role))
.collect(Collectors.toSet());
}
}
Loading
Loading