5
5
import com .fasterxml .jackson .databind .DeserializationFeature ;
6
6
import com .fasterxml .jackson .databind .MapperFeature ;
7
7
import com .fasterxml .jackson .databind .ObjectMapper ;
8
+
9
+ import ca .uhn .fhir .context .FhirContext ;
8
10
import de .gematik .ws .conn .cardservicecommon .v2 .CardTypeType ;
9
11
import de .gematik .ws .conn .connectorcontext .v2 .ContextType ;
10
12
import de .gematik .ws .conn .eventservice .wsdl .v7 .EventServicePortType ;
11
13
import de .gematik .ws .conn .vsds .vsdservice .v5 .FaultMessage ;
12
14
import de .gematik .ws .conn .vsds .vsdservice .v5 .VSDServicePortType ;
13
15
import de .gematik .ws .conn .vsds .vsdservice .v5 .VSDStatusType ;
16
+ import de .gematik .ws .fa .vsdm .vsd .v5 .UCAllgemeineVersicherungsdatenXML ;
17
+ import de .gematik .ws .fa .vsdm .vsd .v5 .UCGeschuetzteVersichertendatenXML ;
18
+ import de .gematik .ws .fa .vsdm .vsd .v5 .UCPersoenlicheVersichertendatenXML ;
14
19
import de .health .service .cetp .IKonnektorClient ;
15
20
import de .health .service .cetp .domain .eventservice .Subscription ;
16
21
import health .ere .ps .config .AppConfig ;
27
32
import jakarta .ws .rs .client .Client ;
28
33
import jakarta .ws .rs .client .Entity ;
29
34
import jakarta .ws .rs .core .Response ;
35
+ import jakarta .xml .bind .DatatypeConverter ;
36
+ import jakarta .xml .bind .JAXBContext ;
37
+ import jakarta .xml .bind .JAXBException ;
30
38
import jakarta .xml .ws .Holder ;
31
39
import org .apache .commons .lang3 .StringUtils ;
32
40
import org .apache .commons .lang3 .tuple .Pair ;
@@ -111,9 +119,32 @@ public class PharmacyService implements AutoCloseable {
111
119
112
120
Client client ;
113
121
114
- private final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
122
+ static final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
115
123
private final ScheduledExecutorService scheduledExecutorService = Executors .newSingleThreadScheduledExecutor ();
116
124
125
+ static DocumentBuilder builder ;
126
+
127
+ static {
128
+ try {
129
+ factory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , true );
130
+ builder = factory .newDocumentBuilder ();
131
+ } catch (ParserConfigurationException e ) {
132
+ log .log (Level .SEVERE , "Could create parser" , e );
133
+ }
134
+ }
135
+
136
+ static final JAXBContext jaxbContext = createJaxbContext ();
137
+
138
+ static JAXBContext createJaxbContext () {
139
+ try {
140
+ return JAXBContext .newInstance (UCPersoenlicheVersichertendatenXML .class ,
141
+ UCAllgemeineVersicherungsdatenXML .class , UCGeschuetzteVersichertendatenXML .class );
142
+ } catch (JAXBException e ) {
143
+ log .log (Level .SEVERE , "Could not init jaxb context" , e );
144
+ return null ;
145
+ }
146
+ }
147
+
117
148
@ PostConstruct
118
149
public void init () {
119
150
client = ERezeptWorkflowService .initClientWithVAU (appConfig );
@@ -141,9 +172,11 @@ public Pair<Bundle, String> getEPrescriptionsForCardHandle(
141
172
runtimeConfig = new RuntimeConfig ();
142
173
}
143
174
runtimeConfig .setSMCBHandle (smcbHandle );
144
- Holder <byte []> pruefungsnachweis = readVSD (correlationId , egkHandle , smcbHandle , runtimeConfig );
175
+ ReadVSDResult readVSD = readVSD (correlationId , egkHandle , smcbHandle , runtimeConfig );
176
+ Holder <byte []> pruefungsnachweis = readVSD .pruefungsnachweis ;
145
177
String pnw = Base64 .getEncoder ().encodeToString (pruefungsnachweis .value );
146
178
try (Response response = client .target (appConfig .getPrescriptionServiceURL ()).path ("/Task" )
179
+ .queryParam ("kvnr" , extractKVNR (readVSD ))
147
180
.queryParam ("pnw" , pnw ).request ()
148
181
.header ("Content-Type" , "application/fhir+xml" )
149
182
.header ("User-Agent" , appConfig .getUserAgent ())
@@ -166,7 +199,35 @@ public Pair<Bundle, String> getEPrescriptionsForCardHandle(
166
199
}
167
200
}
168
201
169
- public Holder <byte []> readVSD (
202
+ static synchronized String extractKVNR (ReadVSDResult readVSDResult ) {
203
+ try {
204
+ Holder <byte []> pnw = readVSDResult .pruefungsnachweis ;
205
+ String decodedXMLFromPNW = new String (new GZIPInputStream (new ByteArrayInputStream (pnw .value )).readAllBytes ());
206
+ Document doc = builder .parse (new ByteArrayInputStream (decodedXMLFromPNW .getBytes ()));
207
+ String e = doc .getElementsByTagName ("E" ).item (0 ).getTextContent ();
208
+ if (e .equals ("3" )) {
209
+ InputStream isPersoenlicheVersichertendaten = new GZIPInputStream (
210
+ new ByteArrayInputStream (readVSDResult .persoenlicheVersichertendaten .value ));
211
+ UCPersoenlicheVersichertendatenXML patient = (UCPersoenlicheVersichertendatenXML ) jaxbContext
212
+ .createUnmarshaller ().unmarshal (isPersoenlicheVersichertendaten );
213
+
214
+ String versichertenID = patient .getVersicherter ().getVersichertenID ();
215
+ log .fine ("VSDM result: " +e +" VersichertenID: " + versichertenID );
216
+ return versichertenID ;
217
+ } else {
218
+ String pn = doc .getElementsByTagName ("PZ" ).item (0 ).getTextContent ();
219
+ String base64PN = new String (DatatypeConverter .parseBase64Binary (pn ));
220
+ String kvnrFromPn = base64PN .substring (0 , 10 );
221
+ return kvnrFromPn ;
222
+ }
223
+ } catch (SAXException | IOException | NullPointerException | JAXBException e ) {
224
+ String msg = "Could not parse PNW message" ;
225
+ log .log (Level .WARNING , msg , e );
226
+ return "" ;
227
+ }
228
+ }
229
+
230
+ public ReadVSDResult readVSD (
170
231
String correlationId ,
171
232
String egkHandle ,
172
233
String smcbHandle ,
@@ -221,7 +282,21 @@ public Holder<byte[]> readVSD(
221
282
readEPrescriptionsMXBean .increaseVSDFailed ();
222
283
throw t ;
223
284
}
224
- return pruefungsnachweis ;
285
+ ReadVSDResult readVSDResult = new ReadVSDResult ();
286
+ readVSDResult .persoenlicheVersichertendaten = persoenlicheVersichertendaten ;
287
+ readVSDResult .allgemeineVersicherungsdaten = allgemeineVersicherungsdaten ;
288
+ readVSDResult .geschuetzteVersichertendaten = geschuetzteVersichertendaten ;
289
+ readVSDResult .vSD_Status = vSD_Status ;
290
+ readVSDResult .pruefungsnachweis = pruefungsnachweis ;
291
+ return readVSDResult ;
292
+ }
293
+
294
+ public class ReadVSDResult {
295
+ Holder <byte []> persoenlicheVersichertendaten ;
296
+ Holder <byte []> allgemeineVersicherungsdaten ;
297
+ Holder <byte []> geschuetzteVersichertendaten ;
298
+ Holder <VSDStatusType > vSD_Status ;
299
+ Holder <byte []> pruefungsnachweis ;
225
300
}
226
301
227
302
private String getEvent (DocumentBuilder builder , byte [] pruefnachweisBytes ) throws IOException , SAXException {
0 commit comments