spotless apply formatting

This commit is contained in:
Justin McKelvy
2023-11-28 07:19:13 -07:00
parent 8c18c7dbc7
commit 0aa001b6f6
47 changed files with 757 additions and 668 deletions

View File

@@ -6,9 +6,9 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
public class CdsHooksConfigCondition implements Condition {
@Override
public boolean matches(ConditionContext theConditionContext, AnnotatedTypeMetadata theAnnotatedTypeMetadata) {
@Override
public boolean matches(ConditionContext theConditionContext, AnnotatedTypeMetadata theAnnotatedTypeMetadata) {
String property = theConditionContext.getEnvironment().getProperty("hapi.fhir.cdshooks.enabled");
return Boolean.parseBoolean(property);
}
}
}

View File

@@ -3,7 +3,5 @@ package ca.uhn.fhir.jpa.starter.cdshooks;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceRequestJson;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties({ "extension" })
public class CdsHooksRequest extends CdsServiceRequestJson {
}
@JsonIgnoreProperties({"extension"})
public class CdsHooksRequest extends CdsServiceRequestJson {}

View File

@@ -1,24 +1,5 @@
package ca.uhn.fhir.jpa.starter.cdshooks;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import ca.uhn.fhir.jpa.starter.AppProperties;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
@@ -26,6 +7,22 @@ import ca.uhn.hapi.fhir.cdshooks.api.ICdsServiceRegistry;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceRequestJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServiceResponseJson;
import ca.uhn.hapi.fhir.cdshooks.api.json.CdsServicesJson;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static ca.uhn.hapi.fhir.cdshooks.config.CdsHooksConfig.CDS_HOOKS_OBJECT_MAPPER_FACTORY;
@@ -36,12 +33,16 @@ public class CdsHooksServlet extends HttpServlet {
@Autowired
private AppProperties appProperties;
@Autowired
private ProviderConfiguration providerConfiguration;
@Autowired
ICdsServiceRegistry cdsServiceRegistry;
@Autowired
RestfulServer restfulServer;
@Autowired
@Qualifier(CDS_HOOKS_OBJECT_MAPPER_FACTORY)
ObjectMapper objectMapper;
@@ -70,8 +71,11 @@ public class CdsHooksServlet extends HttpServlet {
}
ErrorHandling.setAccessControlHeaders(response, appProperties);
response.setHeader("Content-Type", ContentType.APPLICATION_JSON.getMimeType());
response.getWriter().println(new GsonBuilder().setPrettyPrinting().create().toJson(
JsonParser.parseString(objectMapper.writeValueAsString(getServices()))));
response.getWriter()
.println(new GsonBuilder()
.setPrettyPrinting()
.create()
.toJson(JsonParser.parseString(objectMapper.writeValueAsString(getServices()))));
}
@Override
@@ -79,8 +83,8 @@ public class CdsHooksServlet extends HttpServlet {
throws ServletException, IOException {
try {
if (request.getContentType() == null || !request.getContentType().startsWith("application/json")) {
throw new ServletException(String.format("Invalid content type %s. Please use application/json.",
request.getContentType()));
throw new ServletException(String.format(
"Invalid content type %s. Please use application/json.", request.getContentType()));
}
logger.info(request.getRequestURI());
String service = request.getPathInfo().replace("/", "");
@@ -92,7 +96,10 @@ public class CdsHooksServlet extends HttpServlet {
CdsServiceResponseJson serviceResponseJson = cdsServiceRegistry.callService(service, cdsHooksRequest);
// Using GSON pretty print format as Jackson's is ugly
String jsonResponse = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create()
String jsonResponse = new GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
.create()
.toJson(JsonParser.parseString(objectMapper.writeValueAsString(serviceResponseJson)));
logger.info(jsonResponse);
response.setContentType("text/json;charset=UTF-8");
@@ -111,7 +118,9 @@ public class CdsHooksServlet extends HttpServlet {
logger.info("cds-hooks hook instance: {}", request.getHookInstance());
logger.info("cds-hooks local server address: {}", appProperties.getServer_address());
logger.info("cds-hooks fhir server address: {}", request.getFhirServer());
logger.info("cds-hooks cql_logging_enabled: {}", this.getProviderConfiguration().getCqlLoggingEnabled());
logger.info(
"cds-hooks cql_logging_enabled: {}",
this.getProviderConfiguration().getCqlLoggingEnabled());
}
private CdsServicesJson getServices() {

View File

@@ -1,91 +1,105 @@
package ca.uhn.fhir.jpa.starter.cdshooks;
import ca.uhn.fhir.jpa.starter.AppProperties;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import ca.uhn.fhir.jpa.starter.AppProperties;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
public class ErrorHandling {
private ErrorHandling() {}
public static void handleError(HttpServletResponse response, String message,
Exception e, AppProperties myAppProperties)
throws IOException {
setAccessControlHeaders(response, myAppProperties);
response.setStatus(500); // This will be overwritten with the correct status code downstream if needed.
response.getWriter().println(message);
printMessageAndCause(e, response);
if (e instanceof BaseServerResponseException) {
handleServerResponseException((BaseServerResponseException) e, response);
} else if (e.getCause() instanceof BaseServerResponseException) {
handleServerResponseException((BaseServerResponseException) e.getCause(), response);
}
printStackTrack(e, response);
}
public static void handleError(
HttpServletResponse response, String message, Exception e, AppProperties myAppProperties)
throws IOException {
setAccessControlHeaders(response, myAppProperties);
response.setStatus(500); // This will be overwritten with the correct status code downstream if needed.
response.getWriter().println(message);
printMessageAndCause(e, response);
if (e instanceof BaseServerResponseException) {
handleServerResponseException((BaseServerResponseException) e, response);
} else if (e.getCause() instanceof BaseServerResponseException) {
handleServerResponseException((BaseServerResponseException) e.getCause(), response);
}
printStackTrack(e, response);
}
private static void handleServerResponseException(BaseServerResponseException e, HttpServletResponse response)
throws IOException {
switch (e.getStatusCode()) {
case 401:
case 403:
response.getWriter().println("Precondition Failed. Remote FHIR server returned: " + e.getStatusCode());
response.getWriter().println(
"Ensure that the fhirAuthorization token is set or that the remote server allows unauthenticated access.");
response.setStatus(412);
break;
case 404:
response.getWriter().println("Precondition Failed. Remote FHIR server returned: " + e.getStatusCode());
response.getWriter().println("Ensure the resource exists on the remote server.");
response.setStatus(412);
break;
default:
response.getWriter().println("Unhandled Error in Remote FHIR server: " + e.getStatusCode());
}
}
private static void handleServerResponseException(BaseServerResponseException e, HttpServletResponse response)
throws IOException {
switch (e.getStatusCode()) {
case 401:
case 403:
response.getWriter().println("Precondition Failed. Remote FHIR server returned: " + e.getStatusCode());
response.getWriter()
.println(
"Ensure that the fhirAuthorization token is set or that the remote server allows unauthenticated access.");
response.setStatus(412);
break;
case 404:
response.getWriter().println("Precondition Failed. Remote FHIR server returned: " + e.getStatusCode());
response.getWriter().println("Ensure the resource exists on the remote server.");
response.setStatus(412);
break;
default:
response.getWriter().println("Unhandled Error in Remote FHIR server: " + e.getStatusCode());
}
}
private static void printMessageAndCause(Exception e, HttpServletResponse response) throws IOException {
if (e.getMessage() != null) {
response.getWriter().println(e.getMessage());
}
private static void printMessageAndCause(Exception e, HttpServletResponse response) throws IOException {
if (e.getMessage() != null) {
response.getWriter().println(e.getMessage());
}
if (e.getCause() != null && e.getCause().getMessage() != null) {
response.getWriter().println(e.getCause().getMessage());
}
}
if (e.getCause() != null && e.getCause().getMessage() != null) {
response.getWriter().println(e.getCause().getMessage());
}
}
private static void printStackTrack(Exception e, HttpServletResponse response) throws IOException {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
response.getWriter().println(exceptionAsString);
}
private static void printStackTrack(Exception e, HttpServletResponse response) throws IOException {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
response.getWriter().println(exceptionAsString);
}
public static void setAccessControlHeaders(HttpServletResponse resp, AppProperties myAppProperties) {
if (myAppProperties.getCors() != null) {
if (myAppProperties.getCors().getAllow_Credentials()) {
resp.setHeader("Access-Control-Allow-Origin",
myAppProperties.getCors().getAllowed_origin().stream().findFirst().get());
resp.setHeader("Access-Control-Allow-Methods",
String.join(", ", Arrays.asList("GET", "HEAD", "POST", "OPTIONS")));
resp.setHeader("Access-Control-Allow-Headers",
String.join(", ", Arrays.asList("x-fhir-starter", "Origin",
"Accept", "X-Requested-With", "Content-Type", "Authorization", "Cache-Control")));
resp.setHeader("Access-Control-Expose-Headers",
String.join(", ", Arrays.asList("Location", "Content-Location")));
resp.setHeader("Access-Control-Max-Age", "86400");
}
}
}
public static void setAccessControlHeaders(HttpServletResponse resp, AppProperties myAppProperties) {
if (myAppProperties.getCors() != null) {
if (myAppProperties.getCors().getAllow_Credentials()) {
resp.setHeader(
"Access-Control-Allow-Origin",
myAppProperties.getCors().getAllowed_origin().stream()
.findFirst()
.get());
resp.setHeader(
"Access-Control-Allow-Methods",
String.join(", ", Arrays.asList("GET", "HEAD", "POST", "OPTIONS")));
resp.setHeader(
"Access-Control-Allow-Headers",
String.join(
", ",
Arrays.asList(
"x-fhir-starter",
"Origin",
"Accept",
"X-Requested-With",
"Content-Type",
"Authorization",
"Cache-Control")));
resp.setHeader(
"Access-Control-Expose-Headers",
String.join(", ", Arrays.asList("Location", "Content-Location")));
resp.setHeader("Access-Control-Max-Age", "86400");
}
}
}
public static class CdsHooksError extends RuntimeException {
public CdsHooksError(String message) {
super(message);
}
}
public static class CdsHooksError extends RuntimeException {
public CdsHooksError(String message) {
super(message);
}
}
}

View File

@@ -4,7 +4,8 @@ import ca.uhn.fhir.jpa.starter.cr.CrProperties;
public class ProviderConfiguration {
public static final ProviderConfiguration DEFAULT_PROVIDER_CONFIGURATION = new ProviderConfiguration(false, "client_id");
public static final ProviderConfiguration DEFAULT_PROVIDER_CONFIGURATION =
new ProviderConfiguration(false, "client_id");
private final String clientIdHeaderName;
private final boolean cqlLoggingEnabled;

View File

@@ -1,5 +1,11 @@
package ca.uhn.fhir.jpa.starter.cdshooks;
import ca.uhn.fhir.jpa.starter.cr.CrConfigCondition;
import ca.uhn.fhir.jpa.starter.cr.CrProperties;
import ca.uhn.hapi.fhir.cdshooks.api.ICdsHooksDaoAuthorizationSvc;
import ca.uhn.hapi.fhir.cdshooks.config.CdsHooksConfig;
import ca.uhn.hapi.fhir.cdshooks.svc.CdsHooksContextBooter;
import ca.uhn.hapi.fhir.cdshooks.svc.cr.CdsCrSettings;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
@@ -8,15 +14,8 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import ca.uhn.fhir.jpa.starter.cr.CrConfigCondition;
import ca.uhn.fhir.jpa.starter.cr.CrProperties;
import ca.uhn.hapi.fhir.cdshooks.api.ICdsHooksDaoAuthorizationSvc;
import ca.uhn.hapi.fhir.cdshooks.config.CdsHooksConfig;
import ca.uhn.hapi.fhir.cdshooks.svc.CdsHooksContextBooter;
import ca.uhn.hapi.fhir.cdshooks.svc.cr.CdsCrSettings;
@Configuration
@Conditional({ CdsHooksConfigCondition.class, CrConfigCondition.class })
@Conditional({CdsHooksConfigCondition.class, CrConfigCondition.class})
@Import(CdsHooksConfig.class)
public class StarterCdsHooksConfig {
@Bean