""");
- if (isHtmlEnabled(type))
+ if (htmlEnabled)
out.write("
\n");
out.write("""
@@ -256,16 +262,26 @@ public void writeTo(BaseResource resource, Class> type, Type genericType, Anno
writeXml(mediaType, basePath, resource, out);
writeJson(mediaType, basePath, resource, out);
- if (isHtmlEnabled(type))
+ if (htmlEnabled)
writeHtml(type, basePath, resource, out);
out.write("");
out.flush();
}
- private String getUrlHeading(BaseResource resource) throws MalformedURLException
+ private String getTitle(UriInfo uri)
+ {
+ if (uri == null || uri.getPath() == null || uriInfo.getPath().isBlank())
+ return "DSF";
+ else if (uriInfo.getPath().endsWith("/"))
+ return "DSF: " + uriInfo.getPath().substring(0, uriInfo.getPath().length() - 1);
+ else
+ return "DSF: " + uriInfo.getPath();
+ }
+
+ private String getUrlHeading(Resource resource) throws MalformedURLException
{
- URI uri = getResourceUrl(resource).map(this::toURI).orElse(uriInfo.getRequestUri());
+ URI uri = getResourceUri(resource);
String[] pathSegments = uri.getPath().split("/");
String u = serverBaseProvider.get();
@@ -294,6 +310,12 @@ else if (uriInfo.getQueryParameters().containsKey("_summary"))
return heading.toString();
}
+
+ private URI getResourceUri(Resource resource) throws MalformedURLException
+ {
+ return getResourceUrlString(resource).map(this::toURI).orElse(uriInfo.getRequestUri());
+ }
+
private URI toURI(String str)
{
try
@@ -306,7 +328,7 @@ private URI toURI(String str)
}
}
- private Optional
getResourceUrl(BaseResource resource) throws MalformedURLException
+ private Optional getResourceUrlString(Resource resource) throws MalformedURLException
{
if (resource instanceof Resource && resource.getIdElement().hasIdPart())
{
@@ -325,7 +347,7 @@ else if (resource instanceof Bundle && !resource.getIdElement().hasIdPart())
return Optional.empty();
}
- private void writeXml(MediaType mediaType, String basePath, BaseResource resource, OutputStreamWriter out)
+ private void writeXml(MediaType mediaType, String basePath, Resource resource, OutputStreamWriter out)
throws IOException
{
IParser parser = getParser(mediaType, fhirContext::newXmlParser);
@@ -389,7 +411,7 @@ private String simplifyXml(String xml)
}
}
- private void writeJson(MediaType mediaType, String basePath, BaseResource resource, OutputStreamWriter out)
+ private void writeJson(MediaType mediaType, String basePath, Resource resource, OutputStreamWriter out)
throws IOException
{
IParser parser = getParser(mediaType, fhirContext::newJsonParser);
@@ -419,23 +441,33 @@ private void writeJson(MediaType mediaType, String basePath, BaseResource resour
}
@SuppressWarnings("unchecked")
- private void writeHtml(Class> resourceType, String basePath, BaseResource resource, OutputStreamWriter out)
+ private void writeHtml(Class> resourceType, String basePath, Resource resource, OutputStreamWriter out)
throws IOException
{
out.write("\n");
- HtmlGenerator generator = (HtmlGenerator) htmlGeneratorsByType.get(resourceType);
- generator.writeHtml(basePath, resource, out);
+ URI resourceUri = getResourceUri(resource);
+
+ HtmlGenerator generator = (HtmlGenerator) htmlGeneratorsByType.get(resourceType).stream()
+ .filter(g -> g.isResourceSupported(basePath, resourceUri, resource)).findFirst().get();
+ generator.writeHtml(basePath, resourceUri, resource, out);
out.write("
\n");
}
- private boolean isHtmlEnabled(Class> resourceType)
+ private boolean isHtmlEnabled(Class> resourceType, String basePath, Resource resource)
+ throws MalformedURLException
{
- return htmlGeneratorsByType.containsKey(resourceType);
+ URI resourceUri = getResourceUri(resource);
+
+ if (htmlGeneratorsByType.containsKey(resourceType))
+ return uriInfo != null && htmlGeneratorsByType.get(resourceType).stream()
+ .anyMatch(g -> g.isResourceSupported(basePath, resourceUri, resource));
+ else
+ return false;
}
- private String adaptFormInputsIfTask(BaseResource resource)
+ private String adaptFormInputsIfTask(Resource resource)
{
if (resource instanceof Task task)
return Task.TaskStatus.DRAFT.equals(task.getStatus()) ? "adaptTaskFormInputs();" : "";
@@ -443,7 +475,7 @@ private String adaptFormInputsIfTask(BaseResource resource)
return "";
}
- private Optional getResourceName(BaseResource resource, String uuid)
+ private Optional getResourceName(Resource resource, String uuid)
{
if (resource instanceof Bundle)
{
diff --git a/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/HtmlGenerator.java b/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/HtmlGenerator.java
new file mode 100644
index 000000000..f27b2ca44
--- /dev/null
+++ b/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/HtmlGenerator.java
@@ -0,0 +1,39 @@
+package dev.dsf.fhir.adapter;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.net.URI;
+
+import org.hl7.fhir.r4.model.Resource;
+
+public interface HtmlGenerator
+{
+ /**
+ * @return the resource type supported by this generator
+ */
+ Class getResourceType();
+
+ /**
+ * @param basePath
+ * the applications base base, e.g. /fhir/
+ * @param resourceUri
+ * not null
+ * @param resource
+ * the resource, not null
+ * @param out
+ * the outputStreamWriter, not null
+ * @throws IOException
+ */
+ void writeHtml(String basePath, URI resourceUri, R resource, OutputStreamWriter out) throws IOException;
+
+ /**
+ * @param basePath
+ * the applications base base, e.g. /fhir/
+ * @param resourceUri
+ * not null
+ * @param resource
+ * not null
+ * @return true
if this HtmlGenerator supports the given resource for the given uri
+ */
+ boolean isResourceSupported(String basePath, URI resourceUri, Resource resource);
+}
diff --git a/dsf-fhir/dsf-fhir-rest-adapter/src/main/java/dev/dsf/fhir/adapter/InputHtmlGenerator.java b/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/InputHtmlGenerator.java
similarity index 100%
rename from dsf-fhir/dsf-fhir-rest-adapter/src/main/java/dev/dsf/fhir/adapter/InputHtmlGenerator.java
rename to dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/InputHtmlGenerator.java
diff --git a/dsf-fhir/dsf-fhir-rest-adapter/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java b/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java
similarity index 50%
rename from dsf-fhir/dsf-fhir-rest-adapter/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java
rename to dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java
index c4de31090..4e819336d 100644
--- a/dsf-fhir/dsf-fhir-rest-adapter/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java
+++ b/dsf-fhir/dsf-fhir-server/src/main/java/dev/dsf/fhir/adapter/QuestionnaireResponseHtmlGenerator.java
@@ -2,11 +2,13 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
+import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hl7.fhir.r4.model.QuestionnaireResponse;
+import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.StringType;
public class QuestionnaireResponseHtmlGenerator extends InputHtmlGenerator
@@ -22,54 +24,57 @@ public Class getResourceType()
}
@Override
- public void writeHtml(String basePath, QuestionnaireResponse questionnaireResponse, OutputStreamWriter out)
- throws IOException
+ public boolean isResourceSupported(String basePath, URI resourceUri, Resource resource)
{
- boolean completed = QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED
+ return resource != null && resource instanceof QuestionnaireResponse;
+ }
+
+ @Override
+ public void writeHtml(String basePath, URI resourceUri, QuestionnaireResponse questionnaireResponse,
+ OutputStreamWriter out) throws IOException
+ {
+ final boolean completed = QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED
.equals(questionnaireResponse.getStatus());
out.write("");
- out.write("