Skip to content

Commit

Permalink
Working on adding redaction hub client.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Dec 13, 2024
1 parent 2bedee9 commit 59727ed
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ExplainApiController(FilterService filterService, Gson gson) {
final FilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.TEXT_PLAIN);

return ResponseEntity.status(HttpStatus.OK)
.header("x-document-id", response.documentId())
.header("x-document-id", response.getDocumentId())
.contentType(MediaType.APPLICATION_JSON)
.body(gson.toJson(response));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ai.philterd.philter.api.controllers;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Filter;

import ai.philterd.philter.services.PhilterService;
import ai.philterd.phileas.model.enums.MimeType;
import ai.philterd.phileas.model.responses.BinaryDocumentFilterResponse;
import ai.philterd.phileas.model.responses.FilterResponse;
import ai.philterd.phileas.model.services.FilterService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,10 +17,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ai.philterd.phileas.model.enums.MimeType;
import ai.philterd.phileas.model.responses.BinaryDocumentFilterResponse;
import ai.philterd.phileas.model.responses.FilterResponse;
import ai.philterd.phileas.model.services.FilterService;
import java.util.Collections;
import java.util.List;

@Controller
public class FilterApiController extends AbstractController {
Expand All @@ -45,7 +42,7 @@ public FilterApiController(FilterService filterService) {

LOGGER.info("Received uploaded binary PDF file to be returned as ZIP.");

final List<String> policies = Arrays.asList(policyName);
final List<String> policies = Collections.singletonList(policyName);
final BinaryDocumentFilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.APPLICATION_PDF, MimeType.IMAGE_JPEG);

return ResponseEntity.status(HttpStatus.OK)
Expand All @@ -63,7 +60,7 @@ public FilterApiController(FilterService filterService) {

LOGGER.info("Received uploaded binary PDF file to be returned as PDF.");

final List<String> policies = Arrays.asList(policyName);
final List<String> policies = Collections.singletonList(policyName);
final BinaryDocumentFilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.APPLICATION_PDF, MimeType.APPLICATION_PDF);

return ResponseEntity.status(HttpStatus.OK)
Expand All @@ -79,55 +76,13 @@ public FilterApiController(FilterService filterService) {
@RequestParam(value="p", defaultValue="default") String policyName,
@RequestBody String body) throws Exception {

final List<String> policies = Arrays.asList(policyName);
final List<String> policies = Collections.singletonList(policyName);
final FilterResponse response = filterService.filter(policies, context, documentId, body, MimeType.TEXT_PLAIN);

return ResponseEntity.status(HttpStatus.OK)
.header("x-document-id", response.documentId())
.body(response.filteredText());

}

/*@RequestMapping(value="/api/filter", method=RequestMethod.POST, produces = MediaType.IMAGE_JPEG_VALUE, consumes = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody ResponseEntity<byte[]> filterImageFacialRecognition(
@RequestParam(value="c", defaultValue="none") String context,
@RequestParam(value="d", defaultValue="") String documentId,
@RequestParam(value="p", defaultValue="default") String policyName,
@RequestBody byte[] body) throws Exception {
final BinaryDocumentFilterResponse response = filterService.filter(policyName, context, documentId, body, MimeType.IMAGE_JPEG, MimeType.IMAGE_JPEG);
return ResponseEntity.status(HttpStatus.OK)
.header("x-document-id", response.getDocumentId())
.body(response.getDocument());
}*/

/*@RequestMapping(value="/api/filter", method=RequestMethod.POST, produces = "text/csv", consumes = "text/csv")
public @ResponseBody ResponseEntity<String> filterTextCsvAsTextCsv(
@RequestParam(value="c", defaultValue="none") String context,
@RequestParam(value="d", defaultValue="") String documentId,
@RequestParam(value="p", defaultValue="default") String policyName,
@RequestBody String body) throws Exception {
.body(response.getFilteredText());

if(!statusUp) {
statusUp = getPythonRESTServiceStatus(phileasConfiguration.philterNerEndpoint());
}
if(statusUp) {
final FilterResponse response = filterService.filter(policyName, context, documentId, body, MimeType.CSV);
return ResponseEntity.status(HttpStatus.OK)
.header("x-document-id", response.getDocumentId())
.body(response.getFilteredText());
} else {
throw new ServiceUnavailableException("Philter is still initializing. If message persists refer to Philter's log file.");
}
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ public PhilterConfiguration(Properties properties, String applicationName) throw
this.applicationName = applicationName;
}

// Redaction Hub

public boolean redactionHubEnabled() {
return Boolean.parseBoolean(getProperty("redaction.hub.enabled", "false"));
}

public String redactionHubApiKey() {
return getProperty("redaction.hub.api.key", "");
}

public String redactionHubBaseUrl() {
return getProperty("redaction.hub.base.url", "");
}

public int redactionHubTimeOut() {
return Integer.parseInt(getProperty("redaction.hub.timeout", "3000"));
}

public boolean redactionHubIgnoreSsl() {
return Boolean.parseBoolean(getProperty("redaction.hub.ignore.ssl", "false"));
}

public String redactionHubCertificateName() {
return getProperty("redaction.hub.certificate.name", "");
}

// Metrics

public String metricsPrefix() {
Expand Down
5 changes: 5 additions & 0 deletions philter-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<artifactId>philter-metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ai.philterd</groupId>
<artifactId>redaction-hub-client</artifactId>
<version>${redaction.hub.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,76 @@
import ai.philterd.phileas.model.services.PolicyService;
import ai.philterd.phileas.services.PhileasFilterService;
import ai.philterd.philter.PhilterConfiguration;
import ai.philterd.redactionhub.client.RedactionHubClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class PhilterService implements FilterService {

private final FilterService phileasFilterService;
private final RedactionHubClient redactionHubClient;
private final String certificateName;

@Autowired
public PhilterService(PhileasConfiguration phileasConfiguration) throws IOException {
public PhilterService(PhileasConfiguration phileasConfiguration) throws Exception {

final PhilterConfiguration philterConfiguration = new PhilterConfiguration("philter.properties", "Philter");
final PhilterMetricsService philterMetricsService = new PhilterMetricsService(philterConfiguration);
this.phileasFilterService = new PhileasFilterService(phileasConfiguration, philterMetricsService);

if(philterConfiguration.redactionHubEnabled()) {
this.redactionHubClient = new RedactionHubClient(philterConfiguration.redactionHubApiKey(), philterConfiguration.redactionHubBaseUrl(), philterConfiguration.redactionHubTimeOut(), philterConfiguration.redactionHubIgnoreSsl());
this.certificateName = philterConfiguration.redactionHubCertificateName();
} else {
this.redactionHubClient = null;
this.certificateName = null;
}

}

@Override
public FilterResponse filter(Policy policy, String context, String documentId, String input, MimeType mimeType) throws Exception {
return phileasFilterService.filter(policy, context, documentId, input, mimeType);

final FilterResponse filterResponse = phileasFilterService.filter(policy, context, documentId, input, mimeType);

if(redactionHubClient != null) {

final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName);

return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(),
filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(),
signature);

} else {

return filterResponse;

}

}

@Override
public FilterResponse filter(List<String> policyNames, String context, String documentId, String input, MimeType mimeType) throws Exception {
return phileasFilterService.filter(policyNames, context, documentId, input, mimeType);

final FilterResponse filterResponse = phileasFilterService.filter(policyNames, context, documentId, input, mimeType);

if(redactionHubClient != null) {

final String signature = redactionHubClient.sign(filterResponse.getFilteredText(), certificateName);

return new SignedFilterResponse(filterResponse.getFilteredText(), filterResponse.getContext(),
filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes(),
signature);

} else {

return filterResponse;

}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ai.philterd.philter.services;

import ai.philterd.phileas.model.objects.Explanation;
import ai.philterd.phileas.model.responses.FilterResponse;

import java.util.Map;

public class SignedFilterResponse extends FilterResponse {

private String signature;

public SignedFilterResponse(String filteredText, String context, String documentId, int piece, Explanation explanation, Map<String, String> attributes, String signature) {
super(filteredText, context, documentId, piece, explanation, attributes);
this.signature = signature;
}

public String getSignature() {
return signature;
}

public void setSignature(String signature) {
this.signature = signature;
}

}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@
<junit.version>4.13.2</junit.version>
<micrometer.version>1.13.3</micrometer.version>
<mockito.version>1.10.19</mockito.version>
<phileas.version>2.9.1</phileas.version>
<phileas.version>2.10.0-SNAPSHOT</phileas.version>
<philter.sdk.version>1.4.0</philter.sdk.version>
<redaction.hub.version>1.0.0-SNAPSHOT</redaction.hub.version>
<spring.boot.version>3.3.1</spring.boot.version>
<spring.version>6.1.10</spring.version>
</properties>
Expand Down

0 comments on commit 59727ed

Please sign in to comment.