-
Notifications
You must be signed in to change notification settings - Fork 11
/
MessageStorage.java
1891 lines (1644 loc) · 85.3 KB
/
MessageStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This Source Code Form is subject to the terms of the MIT License.
* Copyright (c) 2022 Draegerwerk AG & Co. KGaA.
*
* SPDX-License-Identifier: MIT
*/
package com.draeger.medical.sdccc.messages;
import com.draeger.medical.sdccc.configuration.TestSuiteConfig;
import com.draeger.medical.sdccc.messages.guice.MessageFactory;
import com.draeger.medical.sdccc.messages.mapping.HTTPHeaderEntity;
import com.draeger.medical.sdccc.messages.mapping.HTTPHeaderEntity_;
import com.draeger.medical.sdccc.messages.mapping.ManipulationData;
import com.draeger.medical.sdccc.messages.mapping.ManipulationData_;
import com.draeger.medical.sdccc.messages.mapping.ManipulationParameter;
import com.draeger.medical.sdccc.messages.mapping.ManipulationParameter_;
import com.draeger.medical.sdccc.messages.mapping.MdibVersionGroupEntity;
import com.draeger.medical.sdccc.messages.mapping.MdibVersionGroupEntity_;
import com.draeger.medical.sdccc.messages.mapping.MessageContent;
import com.draeger.medical.sdccc.messages.mapping.MessageContent_;
import com.draeger.medical.sdccc.util.Constants;
import com.draeger.medical.sdccc.util.TestRunObserver;
import com.draeger.medical.sdccc.util.XPathExtractor;
import com.draeger.medical.t2iapi.ResponseTypes;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.somda.sdc.dpws.CommunicationLog;
import org.somda.sdc.dpws.soap.ApplicationInfo;
import org.somda.sdc.dpws.soap.CommunicationContext;
import org.somda.sdc.dpws.soap.HttpApplicationInfo;
import org.somda.sdc.dpws.soap.SoapConstants;
import org.somda.sdc.dpws.soap.wsaddressing.WsAddressingConstants;
/**
* Storage for incoming and outgoing messages.
*/
@Singleton
public class MessageStorage implements AutoCloseable {
private static final Logger LOG = LogManager.getLogger(MessageStorage.class);
private static final int FETCH_SIZE = 10;
// Prefixes of XML declarations according to Appendix F.1 of the XML Standard
private static final int XML_DECLARATION_PREFIX_LENGTH = 4;
private static final Map<byte[], Charset> XML_DECLARATION_PREFIXES = Map.of(
new byte[] {0x00, 0x00, 0x00, 0x3C},
Charset.forName("UTF-32BE"),
new byte[] {0x3C, 0x00, 0x00, 0x00},
Charset.forName("UTF-32LE"),
new byte[] {0x00, 0x3C, 0x00, 0x3F},
StandardCharsets.UTF_16BE,
new byte[] {0x3C, 0x00, 0x3F, 0x00},
StandardCharsets.UTF_16LE,
new byte[] {0x3C, 0x3F, 0x78, 0x6D},
StandardCharsets.US_ASCII, // any ASCII-compatible charset
// AFAIK, all charsets of the EBCDIC family have common bit-patterns for the characters used
// in the XML declaration. Hence, any of them could be used to decode the declaration. However,
// only 'ebcdic-international-500+euro' was tested in this respect and hence it is used as a
// placeholder for 'any EBCDIC charset'.
new byte[] {0x4C, 0x6F, (byte) 0xA7, (byte) 0x94},
Charset.forName("ebcdic-international-500+euro"));
private static final List<String> SDC_MIME_TYPES = List.of("application/soap+xml", "application/xml");
private static final String HTTP_HEADER_NAME_CONTENT_TYPE = "content-type";
private static final String CREATE_MESSAGE_STREAM_CALLED_ON_CLOSED_STORAGE =
"createMessageStream called on closed storage";
private static final String GET_UNIQUE_SEQUENCE_IDS_CALLED_ON_CLOSED_STORAGE =
"getUniqueSequenceIds called on closed storage";
private static final String GET_INBOUND_MESSAGES_CALLED_ON_CLOSED_STORAGE =
"getInboundMessages called on closed storage";
private static final String GET_OUTBOUND_MESSAGES_CALLED_ON_CLOSED_STORAGE =
"getOutboundMessages called on closed storage";
private static final String GET_INBOUND_SOAP_MESSAGES_CALLED_ON_CLOSED_STORAGE =
"getInboundSoapMessages called on closed storage";
private static final String GET_INBOUND_MESSAGE_BY_BODY_TYPE_CALLED_ON_CLOSED_STORAGE =
"getInboundMessagesByBodyType called on closed storage";
private static final String GET_INBOUND_MESSAGE_BY_TIME_INTERVAL_CALLED_ON_CLOSED_STORAGE =
"getInboundMessagesByTimeInterval called on closed storage";
private static final String GET_MANIPULATION_DATA_BY_MANIPULATION =
"getManipulationDataByManipulation called on closed storage";
private static final String FILTERING_FOR_GIVEN_ELEMENT_NAME_NOT_IMPLEMENTED =
"Filtering for the given element name is not supported due to MdibVersionGroups "
+ "not being parsed for elements with the name %s .";
private static final String INCONSISTENT_CHARSET_DECLARATION_WITH_ORIGINS =
"MessageID=%s: Inconsistent charset" + " declaration: %s, but %s";
private static final String HTTP_HEADER_ORIGIN = "HTTP Header states '%s'";
private static final String BOM_ORIGIN = "Unicode Byte Order Mark for %s found";
private static final String XML_DECLARATION_ORIGIN = "XML Declaration states '%s'";
private static final String XML_DECLARATION_PREFIX_ORIGIN = "XML Declaration is encoded in %s";
private final Pattern charsetPattern = Pattern.compile(".*;\\s*charset\\s*=\\s*([^;]*).*");
private final Pattern encodingFromXmlDeclarationPatternDoubleQuotes =
Pattern.compile(".*<\\?.*encoding\\s*=\\s*\"([^\"]*)\".*\\?>.*", Pattern.DOTALL);
private final Pattern encodingFromXmlDeclarationPatternSingleQuotes =
Pattern.compile(".*<\\?.*encoding\\s*=\\s*'([^']*)'.*\\?>.*", Pattern.DOTALL);
private final MessageFactory messageFactory;
private final AtomicBoolean closed;
private final XPathExtractor actionExtractor;
private final XMLInputFactory xmlInputFactory;
// the containing class has to be singleton, because we want only one SessionFactory
private final SessionFactory sessionFactory;
private final HibernateConfig configuration;
private final ArrayBlockingQueue<DatabaseEntry> messageQueue;
private final List<DatabaseInteractionThread> databaseInteractionThreads;
private final ReentrantLock queueExitLock;
private final ReentrantLock closeLock;
private final int blockingQueueSize;
private final CyclicBarrier flushBarrier;
private final TestRunObserver testRunObserver;
@Inject
MessageStorage(
@Named(TestSuiteConfig.COMMLOG_MESSAGE_BUFFER_SIZE) final int blockingQueueSize,
final MessageFactory messageFactory,
final HibernateConfig configuration,
final TestRunObserver testRunObserver) {
this.messageFactory = messageFactory;
this.testRunObserver = testRunObserver;
this.closed = new AtomicBoolean();
this.blockingQueueSize = blockingQueueSize;
this.actionExtractor = new XPathExtractor(String.format("//%s:Action", WsAddressingConstants.NAMESPACE_PREFIX));
this.configuration = configuration;
this.sessionFactory = this.configuration.getConfiguration().buildSessionFactory();
this.messageQueue = new ArrayBlockingQueue<>(this.blockingQueueSize);
this.queueExitLock = new ReentrantLock();
this.closeLock = new ReentrantLock();
final int logicalProcessorCount = Runtime.getRuntime().availableProcessors();
final int logicalProcessorsToUse;
if (logicalProcessorCount > 2) {
logicalProcessorsToUse = logicalProcessorCount / 2;
} else {
logicalProcessorsToUse = 1;
}
LOG.info(
"Logical processor count is {}. Will use {} database interaction threads.",
logicalProcessorCount,
logicalProcessorsToUse);
// database interaction threads plus the main flush functions caller thread
this.flushBarrier = new CyclicBarrier(logicalProcessorsToUse + 1);
this.databaseInteractionThreads = new ArrayList<>(logicalProcessorsToUse);
for (int i = 0; i < logicalProcessorsToUse; i++) {
final DatabaseInteractionThread databaseInteractionThread = new DatabaseInteractionThread();
databaseInteractionThread.setDaemon(true);
databaseInteractionThread.start();
this.databaseInteractionThreads.add(databaseInteractionThread);
}
this.xmlInputFactory = XMLInputFactory.newInstance();
}
public XPathExtractor getActionExtractor() {
return actionExtractor;
}
/**
* Adds message to the message database.
*
* @param message to add to the database
*/
public void addMessage(final DatabaseEntry message) {
this.closeLock.lock();
try {
if (this.closed.get()) {
final String errorString = "addMessageContent called on closed storage";
LOG.error(errorString);
testRunObserver.invalidateTestRun(errorString);
return;
}
try {
this.messageQueue.put(message);
} catch (final InterruptedException e) {
LOG.error("unable to put message content into queue", e);
testRunObserver.invalidateTestRun(e);
}
} finally {
this.closeLock.unlock();
}
}
public XMLInputFactory getXmlInputFactory() {
return xmlInputFactory;
}
private MessageContent convertMessageToMessageContent(final Message message) {
boolean isSOAP = false;
String body = "";
final Set<String> actions = new HashSet<>();
final List<MdibVersionGroupEntity.MdibVersionGroup> mdibVersionGroups = new LinkedList<>();
final byte[] bodyBytes = message.getFinalMemory();
if (bodyBytes.length > 0) {
// TODO: removed charset determination temporarily (https://github.com/Draegerwerk/SDCcc/issues/48)
// final Charset charset = determineCharsetFromMessage(message);
// TODO: would it not be better to use CharsetDecoder here? (https://github.com/Draegerwerk/SDCcc/issues/2)
body = new String(message.getFinalMemory(), StandardCharsets.UTF_8);
isSOAP = processMessageBody(body, actions, mdibVersionGroups);
}
return new MessageContent(
body,
message.getCommunicationContext(),
message.getDirection(),
message.getMessageType(),
message.getTimestamp(),
message.getNanoTimestamp(),
mdibVersionGroups,
actions,
message.getID(),
isSOAP);
}
private boolean processMessageBody(
final String body,
final Set<String> actions,
final List<MdibVersionGroupEntity.MdibVersionGroup> mdibVersionGroups) {
var isSOAP = false;
try {
final XMLEventReader reader = this.getXmlInputFactory().createXMLEventReader(new StringReader(body));
while (reader.hasNext()) {
final XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
final StartElement startElement = nextEvent.asStartElement();
if (startElement.getName().getLocalPart().equals("Action")
&& startElement.getName().getNamespaceURI().equals(WsAddressingConstants.NAMESPACE)) {
handleActionEvent(actions, reader);
} else if (startElement.getName().getLocalPart().equals("Body")
&& startElement.getName().getNamespaceURI().equals(SoapConstants.NAMESPACE)) {
handleSoapBodyEvent(mdibVersionGroups, reader);
} else if (startElement.getName().getLocalPart().equals("Envelope")
&& startElement.getName().getNamespaceURI().equals(SoapConstants.NAMESPACE)) {
isSOAP = true;
}
}
}
} catch (final XMLStreamException e) {
LOG.trace(
"unable to extract action or body from message content, " + "this is expected for invalid messages",
e);
}
return isSOAP;
}
private ManipulationData convertManipulationInfoToManipulationData(final ManipulationInfo manipulationInfo) {
return new ManipulationData(
manipulationInfo.getStartTimestamp(),
manipulationInfo.getFinishTimestamp(),
manipulationInfo.getResult(),
manipulationInfo.getMethodName(),
manipulationInfo.getParameter(),
manipulationInfo.getID());
}
protected Charset determineCharsetFromMessage(final Message message) {
// Note: charset can be determined from (in the order of precedence)
// 1. HTTP Header
// 2. Unicode Byte Order Mark
// 3. XML Declaration
try {
// 1. check HTTP Header
final Charset charsetFromHttpHeader = determineCharsetFromHttpHeader(message);
// 2. check Unicode Byte Order Mark
final Charset charsetFromUnicodeByteOrderMark = determineCharsetFromUnicodeByteOrderMark(message);
// 3. check XML Declaration
final Charset charsetFromXmlDeclaration = determineCharsetFromXmlDeclaration(message);
checkFullCharsetConsistency(
charsetFromHttpHeader,
charsetFromUnicodeByteOrderMark,
charsetFromXmlDeclaration,
(String originA, String valueA, String originB, String valueB) ->
this.testRunObserver.invalidateTestRun(String.format(
INCONSISTENT_CHARSET_DECLARATION_WITH_ORIGINS,
message.getID(),
String.format(originA, valueA),
String.format(originB, valueB))));
final Charset charset = chooseMessageCharset(
charsetFromHttpHeader, charsetFromUnicodeByteOrderMark, charsetFromXmlDeclaration);
checkMimeType(message);
if (!charset.equals(StandardCharsets.UTF_8)) {
String charsetWithOrigin = String.format("'%s' in Unicode Byte Order Mark", charset);
if (charset.equals(charsetFromHttpHeader)) {
charsetWithOrigin = String.format("'%s' in HTTP Header", charset);
} else if (charset.equals(charsetFromXmlDeclaration)) {
charsetWithOrigin = String.format("'%s' in XML Declaration", charset);
}
this.testRunObserver.invalidateTestRun(String.format(
"Encountered a message whose encoding is declared to be %s. This violates"
+ " MDPWS:R0007_0 - SOAP ENVELOPEs SHALL be encoded by using UTF-8.",
charsetWithOrigin));
}
return charset;
} catch (Exception e) {
return StandardCharsets.UTF_8;
}
}
private Charset chooseMessageCharset(
@Nullable final Charset charsetFromHttpHeader,
@Nullable final Charset charsetFromUnicodeByteOrderMark,
@Nullable final Charset charsetFromXmlDeclaration) {
final Charset charset;
if (charsetFromHttpHeader != null) {
charset = charsetFromHttpHeader;
} else {
if (charsetFromUnicodeByteOrderMark != null) {
// consistency between BOM and XMLDeclaration has already been checked in
// determineCharsetFromXMLDeclaration(). No need to do it again.
charset = charsetFromUnicodeByteOrderMark;
} else {
if (charsetFromXmlDeclaration != null) {
charset = charsetFromXmlDeclaration;
} else {
this.testRunObserver.invalidateTestRun("Message encoding could not be determined."
+ " Please ensure that all Messages send by the Device under Test declare their encoding"
+ " either in the HTTP Header, in the Unicode Byte Order Mark, or in the XML Declaration"
+ " as mandated by the XML Standard.");
charset = StandardCharsets.UTF_8;
}
}
}
return charset;
}
private void checkFullCharsetConsistency(
@Nullable final Charset charsetFromHttpHeader,
@Nullable final Charset charsetFromUnicodeByteOrderMark,
@Nullable final Charset charsetFromXmlDeclaration,
final InconsistencyReport report) {
if (charsetFromHttpHeader != null) {
if (charsetFromUnicodeByteOrderMark != null
&& !charsetFromUnicodeByteOrderMark.equals(charsetFromHttpHeader)) {
report.report(
HTTP_HEADER_ORIGIN,
charsetFromHttpHeader.toString(),
BOM_ORIGIN,
charsetFromUnicodeByteOrderMark.toString());
} else if (charsetFromXmlDeclaration != null && !charsetFromXmlDeclaration.equals(charsetFromHttpHeader)) {
report.report(
HTTP_HEADER_ORIGIN,
charsetFromHttpHeader.toString(),
XML_DECLARATION_ORIGIN,
charsetFromXmlDeclaration.toString());
}
} else {
if (charsetFromXmlDeclaration != null
&& charsetFromUnicodeByteOrderMark != null
&& !charsetFromUnicodeByteOrderMark.equals(charsetFromXmlDeclaration)) {
report.report(
BOM_ORIGIN,
charsetFromUnicodeByteOrderMark.toString(),
XML_DECLARATION_ORIGIN,
charsetFromXmlDeclaration.toString());
}
}
}
private void checkMimeType(final Message message) {
final ApplicationInfo applicationInfo =
message.getCommunicationContext().getApplicationInfo();
if (applicationInfo instanceof HttpApplicationInfo) {
final List<String> contentTypeHeaderValues =
((HttpApplicationInfo) applicationInfo).getHeaders().get(HTTP_HEADER_NAME_CONTENT_TYPE);
for (final String value : contentTypeHeaderValues) {
final int index = value.indexOf(";");
final String mimeType;
if (index >= 0) {
mimeType = value.substring(0, index);
} else {
mimeType = value;
}
if (!SDC_MIME_TYPES.contains(mimeType)) {
this.testRunObserver.invalidateTestRun(String.format(
"encountered a SOAP Envelope whose mimeType '%s' (declared in its "
+ "HTTP Header) indicates that it was not serialized as 'application/soap+xml' and "
+ "that hence violates the definition of a SOAP TEXT ENVELOPE in MDPWS Section 3.1.",
mimeType));
}
}
}
}
private Charset determineCharsetFromUnicodeByteOrderMark(final Message message) {
Charset charsetFromUnicodeByteOrderMark = null;
try {
final ByteArrayInputStream in = new ByteArrayInputStream(message.getFinalMemory());
final BOMInputStream bomIn = new BOMInputStream(
in,
ByteOrderMark.UTF_8,
ByteOrderMark.UTF_16LE,
ByteOrderMark.UTF_16BE,
ByteOrderMark.UTF_32LE,
ByteOrderMark.UTF_32BE);
if (!bomIn.hasBOM()) {
LOG.trace("Unable to determine charset from byte order mark for message with ID '" + message.getID()
+ "'. Will use another option.");
} else if (bomIn.hasBOM(ByteOrderMark.UTF_8)) {
charsetFromUnicodeByteOrderMark = StandardCharsets.UTF_8;
} else if (bomIn.hasBOM(ByteOrderMark.UTF_16LE)) {
charsetFromUnicodeByteOrderMark = StandardCharsets.UTF_16LE;
} else if (bomIn.hasBOM(ByteOrderMark.UTF_16BE)) {
charsetFromUnicodeByteOrderMark = StandardCharsets.UTF_16BE;
} else if (bomIn.hasBOM(ByteOrderMark.UTF_32LE)) {
charsetFromUnicodeByteOrderMark = Charset.forName("UTF_32LE");
} else if (bomIn.hasBOM(ByteOrderMark.UTF_32BE)) {
charsetFromUnicodeByteOrderMark = Charset.forName("UTF_32BE");
}
} catch (IOException e) {
this.testRunObserver.invalidateTestRun("Unable to read message contents", e);
}
return charsetFromUnicodeByteOrderMark;
}
private Charset determineCharsetFromXmlDeclaration(final Message message) {
final Charset charsetFromBOM = determineCharsetFromUnicodeByteOrderMark(message);
final Charset charsetFromPrefix = determineCharsetFromXmlDeclarationPrefix(message);
Charset result = null;
if (charsetFromBOM != null) {
result = determineCharsetFromXmlDeclarationInternal(message, charsetFromBOM);
} else {
if (charsetFromPrefix != null) {
result = determineCharsetFromXmlDeclarationInternal(message, charsetFromPrefix);
}
}
checkCharsetConsistency(
charsetFromBOM,
charsetFromPrefix,
result,
(String originA, String valueA, String originB, String valueB) ->
this.testRunObserver.invalidateTestRun(String.format(
INCONSISTENT_CHARSET_DECLARATION_WITH_ORIGINS,
message.getID(),
String.format(originA, valueA),
String.format(originB, valueB))));
return result;
}
private void checkCharsetConsistency(
final @javax.annotation.Nullable Charset charsetFromBOM,
final @javax.annotation.Nullable Charset charsetFromPrefix,
final @javax.annotation.Nullable Charset charsetFromDeclaration,
final InconsistencyReport report) {
if (charsetFromDeclaration == null) {
if (charsetFromBOM != null
&& charsetFromPrefix != null
&& !bomAndPrefixAreCompatible(charsetFromBOM, charsetFromPrefix)) {
report.report(
XML_DECLARATION_PREFIX_ORIGIN,
charsetFromPrefix.toString(),
BOM_ORIGIN,
charsetFromBOM.toString());
}
} else {
if (charsetFromBOM != null && !charsetFromBOM.equals(charsetFromDeclaration)) {
report.report(
XML_DECLARATION_ORIGIN,
charsetFromDeclaration.toString(),
BOM_ORIGIN,
charsetFromBOM.toString());
}
final boolean charset_declaration_inconsistent =
charsetFromPrefix != null && !charsetFromPrefix.equals(charsetFromDeclaration);
// EBCDIC Prefix is consistent with any charset from the EBCDIC family.
final boolean special_case_EBCDIC_family = charsetFromPrefix != null
&& charsetFromPrefix.equals(Charset.forName("ebcdic-international-500+euro"))
&& isEBCDIC(charsetFromDeclaration);
// UTF-8 Prefix is consistent with any charset using the same bit pattern for the ASCII characters
// as UTF-8. Since we were able to read the XML Declaration successfully we can assume this is the
// case.
final boolean special_case_ASCII_compatible =
charsetFromPrefix != null && charsetFromPrefix.equals(StandardCharsets.US_ASCII);
if (charset_declaration_inconsistent && !special_case_EBCDIC_family && !special_case_ASCII_compatible) {
report.report(
XML_DECLARATION_ORIGIN,
charsetFromDeclaration.toString(),
XML_DECLARATION_PREFIX_ORIGIN,
charsetFromPrefix.toString());
}
}
}
private boolean bomAndPrefixAreCompatible(final Charset charsetFromBOM, final Charset charsetFromPrefix) {
if (StandardCharsets.US_ASCII.equals(charsetFromPrefix) && StandardCharsets.UTF_8.equals(charsetFromBOM)) {
return true;
}
return charsetFromPrefix.equals(charsetFromBOM);
}
private boolean isEBCDIC(final Charset charset) {
boolean result = false;
for (String alias : charset.aliases()) {
if (alias.toLowerCase().startsWith("ebcdic")) {
result = true;
break;
}
}
return result;
}
private Charset determineCharsetFromXmlDeclarationPrefix(final Message message) {
final ByteArrayInputStream in = new ByteArrayInputStream(message.getFinalMemory());
final BOMInputStream bomIn = new BOMInputStream(in, ByteOrderMark.UTF_8);
try {
Charset result = null;
final byte[] msg = bomIn.readNBytes(XML_DECLARATION_PREFIX_LENGTH);
for (Map.Entry<byte[], Charset> e : XML_DECLARATION_PREFIXES.entrySet()) {
if (startsWith(msg, e.getKey())) {
result = e.getValue();
break;
}
}
return result;
} catch (IOException e) {
return null;
}
}
/**
* Utility method to check if one byte array starts with a specified sequence of bytes.
*
* @param array The array to check
* @param prefix The prefix bytes to test for
* @return true if the array starts with the bytes from the prefix
*/
private static boolean startsWith(final byte[] array, final byte[] prefix) {
if (array == prefix) {
return true;
}
return startsWithInternal1(array, prefix);
}
private static boolean startsWithInternal1(final byte[] array, final byte[] prefix) {
if (prefix.length > array.length) {
return false;
}
return startsWithInternal2(array, prefix);
}
private static boolean startsWithInternal2(final byte[] array, final byte[] prefix) {
final int prefixLength = prefix.length;
for (int i = 0; i < prefixLength; i++) {
if (array[i] != prefix[i]) {
return false;
}
}
return true;
}
private Charset determineCharsetFromXmlDeclarationInternal(final Message message, final Charset encoding) {
Charset charsetFromXmlDeclaration = null;
final String content = new String(message.getFinalMemory(), encoding);
final Matcher matcher = encodingFromXmlDeclarationPatternDoubleQuotes.matcher(content);
if (matcher.matches()) {
charsetFromXmlDeclaration = Charset.forName(matcher.group(1));
} else {
final Matcher matcher2 = encodingFromXmlDeclarationPatternSingleQuotes.matcher(content);
if (matcher2.matches()) {
charsetFromXmlDeclaration = Charset.forName(matcher2.group(1));
}
}
return charsetFromXmlDeclaration;
}
private Charset determineCharsetFromHttpHeader(final Message message) {
Charset charsetFromHttpHeader = null;
final ApplicationInfo applicationInfo =
message.getCommunicationContext().getApplicationInfo();
if (applicationInfo instanceof HttpApplicationInfo) {
final List<String> contentTypeHeaderValues =
((HttpApplicationInfo) applicationInfo).getHeaders().get(HTTP_HEADER_NAME_CONTENT_TYPE);
for (final String value : contentTypeHeaderValues) {
final Matcher matcher = charsetPattern.matcher(value);
if (matcher.matches()) {
final String charsetName =
stripQuotes(matcher.group(1).trim()).trim();
try {
charsetFromHttpHeader = Charset.forName(charsetName);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
// in this case the message contained a Content-Type Header with a charset directive,
// but the encoding given in that directive was invalid - which is a finding the user should
// be notified about.
// NOTE: returning null in this case is ok as determineCharsetFromMessage() will then try to
// use other clues to determine the charset and hopefully succeed in correctly decoding
// the Message before storing it in the DB.
this.testRunObserver.invalidateTestRun(
String.format("Encountered invalid/unknown charset '%s' in HTTP Header", charsetName),
e);
}
}
}
}
return charsetFromHttpHeader;
}
private String stripQuotes(final String str) {
if ((str.startsWith("\"") && str.endsWith("\"")) || (str.startsWith("'") && str.endsWith("'"))) {
return str.substring(1, str.length() - 1);
} else {
return str;
}
}
private void handleActionEvent(final Set<String> actions, final XMLEventReader reader) throws XMLStreamException {
final XMLEvent nextEvent;
nextEvent = reader.nextEvent();
if (nextEvent.isCharacters()) {
actions.add(nextEvent.asCharacters().getData());
} else {
LOG.warn("empty action element encountered");
actions.add("");
}
}
private boolean checkElementSupportsMdibVersionSorting(final QName name) {
return Constants.RELEVANT_REPORT_BODIES.contains(name) || Constants.MSG_GET_MDIB_RESPONSE.equals(name);
}
private void handleSoapBodyEvent(
final List<MdibVersionGroupEntity.MdibVersionGroup> mdibVersionGroups, final XMLEventReader reader)
throws XMLStreamException {
long childCounter = 0;
XMLEvent nextEvent;
var level = 0;
while (level >= 0) {
nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
level++;
// only add elements on level 1, i.e. direct children of the SOAP body
if (level == 1) {
childCounter++;
final StartElement startElement = nextEvent.asStartElement();
final String bodyElementNameString = startElement.getName().toString();
long mdibVersion = -3L;
String sequenceId = null;
if (this.checkElementSupportsMdibVersionSorting(startElement.getName())) {
final Attribute mdibVersionAttribute = startElement.getAttributeByName(Constants.MDIB_VERSION);
if (mdibVersionAttribute == null) {
mdibVersion = 0L;
} else if (mdibVersionAttribute.getValue() == null) {
mdibVersion = -2L;
this.testRunObserver.invalidateTestRun(
"Calling getValue on mdibVersionAttribute resulted in null. "
+ "Saved -2L as a replacement value.");
} else if (mdibVersionAttribute.getValue().equals("")) {
mdibVersion = -1L;
this.testRunObserver.invalidateTestRun(
"Encountered MdibVersion attribute that has an empty string as its value"
+ "and saved -1L as a replacement value.");
} else {
try {
mdibVersion = Long.parseLong(mdibVersionAttribute.getValue());
} catch (NumberFormatException e) {
this.testRunObserver.invalidateTestRun(e);
}
}
final Attribute sequenceIdAttribute = startElement.getAttributeByName(Constants.SEQUENCE_ID);
if (sequenceIdAttribute != null) {
sequenceId = sequenceIdAttribute.getValue();
} else {
this.testRunObserver.invalidateTestRun(String.format(
"Encountered body with the QName %s " + "without a SequenceId attribute.",
startElement.getName()));
}
}
mdibVersionGroups.add(new MdibVersionGroupEntity.MdibVersionGroup(
mdibVersion, sequenceId, bodyElementNameString));
}
} else if (nextEvent.isEndElement()) {
level--;
}
}
if (childCounter > 1) {
this.testRunObserver.invalidateTestRun(
"Encountered multiple elements in soap body, but more than one are not allowed.");
}
}
/**
* Creates a {@linkplain Message} object in which the message information can be written.
*
* @param path protocol type used, i.e. UDP or TCP
* @param direction the direction of the message, i.e. inbound or outbound
* @param messageType type of the message, i.e. request, response
* @param communicationContext context containing transport and application information
* @return new message instance, already inserted into the global message list
* @throws IOException if MessageCache was already closed
*/
public Message createMessageStream(
final CommunicationLog.TransportType path,
final CommunicationLog.Direction direction,
final CommunicationLog.MessageType messageType,
final CommunicationContext communicationContext)
throws IOException {
if (this.closed.get()) {
LOG.error(CREATE_MESSAGE_STREAM_CALLED_ON_CLOSED_STORAGE);
throw new IOException(CREATE_MESSAGE_STREAM_CALLED_ON_CLOSED_STORAGE);
}
return messageFactory.create(direction, messageType, communicationContext);
}
/**
* Creates a {@linkplain ManipulationInfo} object and adds it to the storage.
*
* @param startTime of the manipulation
* @param finishTime of the manipulation
* @param result of the manipulation
* @param name of the manipulation
* @param parameters of the manipulation
*/
public void createManipulationInfo(
final long startTime,
final long finishTime,
final ResponseTypes.Result result,
final String name,
final List<Pair<String, String>> parameters) {
final var manipulation = new ManipulationInfo(startTime, finishTime, result, name, parameters, this);
manipulation.addToStorage();
}
/**
* Closes the message cache, preventing any new messages from being added.
*/
@Override
public void close() {
this.closeLock.lock();
try {
if (this.closed.compareAndSet(false, true)) {
this.flush();
this.databaseInteractionThreads.forEach(DatabaseInteractionThread::setStopped);
this.databaseInteractionThreads.forEach(thread -> {
try {
thread.join();
} catch (final InterruptedException e) {
LOG.error("unable to wait for database interaction thread termination due to an interrupt", e);
testRunObserver.invalidateTestRun(e);
}
});
this.sessionFactory.close();
this.configuration.close();
}
} finally {
this.closeLock.unlock();
}
}
/**
* Sends all queued and buffered messages to the database and does a plausibility check via a select query.
*/
public synchronized void flush() {
this.databaseInteractionThreads.forEach(DatabaseInteractionThread::triggerFlush);
final List<DatabaseEntry> temp = new ArrayList<>(this.blockingQueueSize);
this.messageQueue.drainTo(temp, this.blockingQueueSize);
this.awaitFlushBarrier();
// will not block at the barrier since other threads had already arrived at the barrier
this.flush(temp);
}
private void flush(final List<DatabaseEntry> messageList) {
this.flush(messageList, false, null);
}
private void flush(
final List<DatabaseEntry> messageList,
final boolean await,
@Nullable final DatabaseInteractionThread databaseInteractionThread) {
if (!messageList.isEmpty()) {
final var firstElement = messageList.get(0);
final var lastElement = messageList.get(messageList.size() - 1);
final String firstID = firstElement.getID();
final String lastID = lastElement.getID();
this.transmit(messageList);
// only uuid shall be selected, thus it is a string criteria query
final CriteriaQuery<String> criteriaFirst = buildCriteria(firstElement.getClass(), firstID);
final CriteriaQuery<String> criteriaLast = buildCriteria(lastElement.getClass(), lastID);
long uuidCount = 0;
while (uuidCount < 2) {
uuidCount = 0;
try (final Stream<String> uuidStream = this.getStreamForQuery(criteriaLast)) {
final long lastIDCount = uuidStream.filter(lastID::equals).count();
if (lastIDCount < 2) {
uuidCount += lastIDCount;
} else {
throw new RuntimeException("duplicate uuid found in database");
}
}
try (final Stream<String> uuidStream = this.getStreamForQuery(criteriaFirst)) {
final long firstIDCount = uuidStream.filter(firstID::equals).count();
if (firstIDCount < 2) {
uuidCount += firstIDCount;
} else {
throw new RuntimeException("duplicate uuid found in database");
}
}
Thread.yield();
}
}
if (databaseInteractionThread != null) {
databaseInteractionThread.resetFlushEvent();
}
if (await) {
this.awaitFlushBarrier();
}
}
private CriteriaQuery<String> buildCriteria(final Class<? extends DatabaseEntry> entryClass, final String id) {
final CriteriaQuery<String> criteria;
try (final Session session = sessionFactory.openSession()) {
session.beginTransaction();
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
criteria = criteriaBuilder.createQuery(String.class);
if (entryClass.equals(Message.class)) {
final Root<MessageContent> messageContentRoot = criteria.from(MessageContent.class);
// here we want only the uuid, not the whole row
criteria.select(messageContentRoot.get(MessageContent_.uuid));
criteria.where(criteriaBuilder.equal(messageContentRoot.get(MessageContent_.uuid), id));
} else if (entryClass.equals(ManipulationInfo.class)) {
final Root<ManipulationData> manipulationDataRoot = criteria.from(ManipulationData.class);
// here we want only the uuid, not the whole row
criteria.select(manipulationDataRoot.get(ManipulationData_.uuid));
criteria.where(criteriaBuilder.equal(manipulationDataRoot.get(ManipulationData_.uuid), id));
}
}
return criteria;
}
private void awaitFlushBarrier() {
try {
this.flushBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
LOG.error("the message flush was interrupted", e);
testRunObserver.invalidateTestRun(e);
}
}
/**
* Retrieves all SequenceId attribute values that have been seen.
*
* @return stream of all SequenceId attribute values that have been seen
* @throws IOException if storage is closed
*/
public Stream<String> getUniqueSequenceIds() throws IOException {
if (this.closed.get()) {
LOG.error(GET_UNIQUE_SEQUENCE_IDS_CALLED_ON_CLOSED_STORAGE);
throw new IOException(GET_UNIQUE_SEQUENCE_IDS_CALLED_ON_CLOSED_STORAGE);
}
final CriteriaQuery<String> criteria;
try (final Session session = sessionFactory.openSession()) {
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
criteria = criteriaBuilder.createQuery(String.class);
final Root<MdibVersionGroupEntity> mdibVersionGroupEntityRoot = criteria.from(MdibVersionGroupEntity.class);
criteria.select(mdibVersionGroupEntityRoot.get(MdibVersionGroupEntity_.sequenceId));
criteria.distinct(true);
}
return this.getQueryResult(criteria);
}
/**
* Retrieves all incoming messages.
*
* @return container with stream of all inbound {@linkplain MessageContent}s
* @throws IOException if storage is closed
*/
public GetterResult<MessageContent> getInboundMessages() throws IOException {
if (this.closed.get()) {
LOG.error(GET_INBOUND_MESSAGES_CALLED_ON_CLOSED_STORAGE);
throw new IOException(GET_INBOUND_MESSAGES_CALLED_ON_CLOSED_STORAGE);
}
final CriteriaQuery<MessageContent> criteria;
try (final Session session = sessionFactory.openSession()) {
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
criteria = criteriaBuilder.createQuery(MessageContent.class);
final Root<MessageContent> messageContentRoot = criteria.from(MessageContent.class);
criteria.select(messageContentRoot);
criteria.where(criteriaBuilder.equal(
messageContentRoot.get(MessageContent_.direction), CommunicationLog.Direction.INBOUND));
}
final boolean present;
try (final Stream<MessageContent> countingStream = this.getQueryResult(criteria)) {