diff --git a/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/resources/RedactionResource.java b/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/resources/RedactionResource.java index 6b3d9de2..464b5799 100644 --- a/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/resources/RedactionResource.java +++ b/redaction-service-v1/redaction-service-api-v1/src/main/java/com/iqser/red/service/redaction/v1/resources/RedactionResource.java @@ -1,6 +1,7 @@ package com.iqser.red.service.redaction.v1.resources; import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -23,9 +24,10 @@ public interface RedactionResource { @PostMapping(value = "/debug/htmlTables", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) RedactionResult htmlTables(@RequestBody RedactionRequest redactionRequest); - @PostMapping(value = "/rules", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping(value = "/rules", produces = MediaType.APPLICATION_JSON_VALUE) String getRules(); @PostMapping(value = "/rules/update", consumes = MediaType.APPLICATION_JSON_VALUE) void updateRules(@RequestBody String rules); -} + +} \ No newline at end of file diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/Application.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/Application.java index 78150292..bb091c47 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/Application.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/Application.java @@ -19,7 +19,7 @@ import com.iqser.red.service.redaction.v1.server.settings.RedactionServiceSettin @Import({DefaultWebMvcConfiguration.class}) @EnableConfigurationProperties(RedactionServiceSettings.class) -@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class }) +@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class}) public class Application { public static void main(String[] args) { @@ -30,6 +30,7 @@ public class Application { @Bean public KieContainer kieContainer() { + KieServices kieServices = KieServices.Factory.get(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); @@ -39,6 +40,7 @@ public class Application { KieModule kieModule = kieBuilder.getKieModule(); return kieServices.newKieContainer(kieModule.getReleaseId()); + } } \ No newline at end of file diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/controller/ControllerAdvice.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/controller/ControllerAdvice.java new file mode 100644 index 00000000..14079b31 --- /dev/null +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/controller/ControllerAdvice.java @@ -0,0 +1,41 @@ +package com.iqser.red.service.redaction.v1.server.controller; + +import java.time.OffsetDateTime; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.iqser.gin4.commons.api.errorhandling.ErrorMessage; +import com.iqser.red.service.redaction.v1.server.exception.RulesValidationException; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RestControllerAdvice +public class ControllerAdvice { + + /* error handling */ + + @ResponseBody + @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(value = NullPointerException.class) + public ErrorMessage handleContentNotFoundException(NullPointerException e) { + if (e != null) { + log.error(e.getMessage(), e); + return new ErrorMessage(OffsetDateTime.now(), e.getMessage()); + } + log.error("Nullpointer exception at ", e); + return new ErrorMessage(OffsetDateTime.now(), "Nullpointer exception"); + } + + @ResponseBody + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + @ExceptionHandler(value = RulesValidationException.class) + public ErrorMessage handleRulesValidationException(RulesValidationException e) { + return new ErrorMessage(OffsetDateTime.now(), e.getMessage()); + } + +} \ No newline at end of file diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/exception/RulesValidationException.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/exception/RulesValidationException.java new file mode 100644 index 00000000..8e1d3c29 --- /dev/null +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/exception/RulesValidationException.java @@ -0,0 +1,9 @@ +package com.iqser.red.service.redaction.v1.server.exception; + +public class RulesValidationException extends RuntimeException { + + public RulesValidationException(String message, Throwable t) { + super(message, t); + } + +} \ No newline at end of file diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DroolsExecutionService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DroolsExecutionService.java index 2a3edfb8..3035b992 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DroolsExecutionService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DroolsExecutionService.java @@ -2,9 +2,11 @@ package com.iqser.red.service.redaction.v1.server.redaction.service; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import javax.annotation.PostConstruct; +import org.apache.commons.lang3.StringUtils; import org.kie.api.KieServices; import org.kie.api.builder.KieBuilder; import org.kie.api.builder.KieFileSystem; @@ -14,6 +16,7 @@ import org.kie.api.runtime.KieSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.iqser.red.service.redaction.v1.server.exception.RulesValidationException; import com.iqser.red.service.redaction.v1.server.redaction.model.Section; import com.iqser.red.service.redaction.v1.server.redaction.utils.ResourceLoader; @@ -26,7 +29,7 @@ public class DroolsExecutionService { private String currentDrlRules; @PostConstruct - public void init (){ + public void init() { currentDrlRules = ResourceLoader.loadAsString("drools/rules.drl"); } @@ -39,29 +42,29 @@ public class DroolsExecutionService { return section; } - public void updateRules(String drlAsString) { try { + if (StringUtils.isEmpty(drlAsString)) { + throw new RuntimeException("Rules cannot be empty."); + } KieServices kieServices = KieServices.Factory.get(); - InputStream input = new ByteArrayInputStream(drlAsString.getBytes("UTF-8")); + InputStream input = new ByteArrayInputStream(drlAsString.getBytes(StandardCharsets.UTF_8)); KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); - kieFileSystem.write("src/main/resources/drools/rules.drl", - kieServices.getResources().newInputStreamResource(input)); + kieFileSystem.write("src/main/resources/drools/rules.drl", kieServices.getResources().newInputStreamResource(input)); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); KieModule kieModule = kieBuilder.getKieModule(); kieContainer.updateToVersion(kieModule.getReleaseId()); currentDrlRules = drlAsString; - } catch (Exception e){ - throw new RuntimeException("Could not update rules"); + } catch (Exception e) { + throw new RulesValidationException("Could not update rules", e); } + } - - public String getRules(){ + public String getRules() { return currentDrlRules; } - -} +} \ No newline at end of file