1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.commons.ihe.fhir.support;
18
19 import ca.uhn.fhir.rest.server.exceptions.*;
20 import org.hl7.fhir.dstu3.model.*;
21 import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
22
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.function.Function;
27 import java.util.stream.Collectors;
28
29
30
31
32
33
34
35 public final class FhirUtils {
36
37
38
39
40
41
42
43
44 public static Map<ResourceType, List<Bundle.BundleEntryComponent>> getBundleEntries(Bundle bundle) {
45 return bundle.getEntry().stream()
46 .collect(Collectors.groupingBy(entry -> {
47 Bundle.BundleEntryRequestComponent request = entry.getRequest();
48 if (request == null || request.getUrl() == null) {
49 throw unprocessableEntity(
50 OperationOutcome.IssueSeverity.ERROR,
51 OperationOutcome.IssueType.INVALID,
52 null, null,
53 "Invalid bundle entry request element %s",
54 entry);
55 }
56 return entry.getResource().getResourceType();
57 }
58 , LinkedHashMap::new, Collectors.toList()));
59 }
60
61
62
63
64 public static InternalErrorException internalError(
65 OperationOutcome.IssueSeverity severity,
66 OperationOutcome.IssueType type,
67 String code,
68 String diagnostics,
69 String msg,
70 Object... args) {
71 return exception(InternalErrorException::new, severity, type, code, diagnostics, msg, args);
72 }
73
74 public static UnprocessableEntityException unprocessableEntity(
75 OperationOutcome.IssueSeverity severity,
76 OperationOutcome.IssueType type,
77 String code,
78 String diagnostics,
79 String msg,
80 Object... args) {
81 return exception(UnprocessableEntityException::new, severity, type, code, diagnostics, msg, args);
82 }
83
84 public static InvalidRequestException invalidRequest(
85 OperationOutcome.IssueSeverity severity,
86 OperationOutcome.IssueType type,
87 String code,
88 String diagnostics,
89 String msg,
90 Object... args) {
91 return exception(InvalidRequestException::new, severity, type, code, diagnostics, msg, args);
92 }
93
94 public static ResourceNotFoundException resourceNotFound(
95 OperationOutcome.IssueSeverity severity,
96 OperationOutcome.IssueType type,
97 String code,
98 String diagnostics,
99 String msg,
100 Object... args) {
101 return exception(ResourceNotFoundException::new, severity, type, code, diagnostics, msg, args);
102 }
103
104 public static ForbiddenOperationException forbiddenOperation(
105 OperationOutcome.IssueSeverity severity,
106 OperationOutcome.IssueType type,
107 String code,
108 String diagnostics,
109 String msg,
110 Object... args) {
111 return exception(ForbiddenOperationException::new, severity, type, code, diagnostics, msg, args);
112 }
113
114 public static <T extends BaseServerResponseException> T exception(Function<String, T> func,
115 OperationOutcome.IssueSeverity severity,
116 OperationOutcome.IssueType type,
117 String code,
118 String diagnostics,
119 String msg,
120 Object... args) {
121 OperationOutcome operationOutcome = new OperationOutcome();
122 CodeableConcept errorCode = null;
123 if (code != null) {
124 errorCode = new CodeableConcept();
125 errorCode.addCoding().setCode(code);
126 }
127 operationOutcome.addIssue()
128 .setSeverity(severity)
129 .setCode(type)
130 .setDetails(errorCode)
131 .setDiagnostics(diagnostics);
132 return exception(func, operationOutcome, msg, args);
133 }
134
135 public static <T extends BaseServerResponseException> T exception(Function<String, T> func,
136 IBaseOperationOutcome operationOutcome,
137 String msg,
138 Object... args) {
139 T exception = func.apply(String.format(msg, args));
140 exception.setOperationOutcome(operationOutcome);
141 return exception;
142 }
143 }