-
Notifications
You must be signed in to change notification settings - Fork 43
/
AS2SenderModule.java
837 lines (746 loc) · 33.6 KB
/
AS2SenderModule.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
/**
* The FreeBSD Copyright
* Copyright 1994-2008 The FreeBSD Project. All rights reserved.
* Copyright (C) 2013-2018 Philip Helger philip[at]helger[dot]com
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* official policies, either expressed or implied, of the FreeBSD Project.
*/
package com.helger.as2lib.processor.sender;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import org.bouncycastle.mail.smime.SMIMECompressedGenerator;
import org.bouncycastle.mail.smime.SMIMEException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.helger.as2lib.cert.ECertificatePartnershipType;
import com.helger.as2lib.cert.ICertificateFactory;
import com.helger.as2lib.crypto.ECompressionType;
import com.helger.as2lib.crypto.ECryptoAlgorithmCrypt;
import com.helger.as2lib.crypto.ECryptoAlgorithmSign;
import com.helger.as2lib.disposition.DispositionException;
import com.helger.as2lib.disposition.DispositionOptions;
import com.helger.as2lib.disposition.DispositionType;
import com.helger.as2lib.exception.OpenAS2Exception;
import com.helger.as2lib.exception.WrappedOpenAS2Exception;
import com.helger.as2lib.message.AS2Message;
import com.helger.as2lib.message.AS2MessageMDN;
import com.helger.as2lib.message.IMessage;
import com.helger.as2lib.message.IMessageMDN;
import com.helger.as2lib.params.InvalidParameterException;
import com.helger.as2lib.params.MessageParameters;
import com.helger.as2lib.partner.CPartnershipIDs;
import com.helger.as2lib.partner.Partnership;
import com.helger.as2lib.processor.CFileAttribute;
import com.helger.as2lib.processor.CNetAttribute;
import com.helger.as2lib.processor.NoModuleException;
import com.helger.as2lib.processor.storage.IProcessorStorageModule;
import com.helger.as2lib.session.ComponentNotFoundException;
import com.helger.as2lib.util.AS2DateHelper;
import com.helger.as2lib.util.AS2Helper;
import com.helger.as2lib.util.AS2HttpHelper;
import com.helger.as2lib.util.AS2IOHelper;
import com.helger.as2lib.util.CAS2Header;
import com.helger.as2lib.util.dump.IHTTPIncomingDumper;
import com.helger.as2lib.util.dump.IHTTPOutgoingDumper;
import com.helger.as2lib.util.http.AS2HttpHeaderWrapperHttpURLConnection;
import com.helger.as2lib.util.http.HTTPHelper;
import com.helger.as2lib.util.http.IAS2HttpConnection;
import com.helger.as2lib.util.http.IAS2HttpHeaderWrapper;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.annotation.OverrideOnDemand;
import com.helger.commons.http.CHttpHeader;
import com.helger.commons.http.EHttpMethod;
import com.helger.commons.io.file.FileHelper;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.commons.io.stream.NonBlockingByteArrayOutputStream;
import com.helger.commons.io.stream.StreamHelper;
import com.helger.commons.io.stream.WrappedOutputStream;
import com.helger.commons.state.ETriState;
import com.helger.commons.string.StringParser;
import com.helger.commons.timing.StopWatch;
import com.helger.mail.cte.EContentTransferEncoding;
/**
* AS2 sender module to send AS2 messages out.
*
* @author Philip Helger
*/
public class AS2SenderModule extends AbstractHttpSenderModule
{
private static final String ATTR_PENDINGMDNINFO = "pendingmdninfo";
private static final String ATTR_PENDINGMDN = "pendingmdn";
private static final Logger LOGGER = LoggerFactory.getLogger (AS2SenderModule.class);
public AS2SenderModule ()
{}
public boolean canHandle (@Nonnull final String sAction,
@Nonnull final IMessage aMsg,
@Nullable final Map <String, Object> aOptions)
{
return IProcessorSenderModule.DO_SEND.equals (sAction) && aMsg instanceof AS2Message;
}
protected void checkRequired (@Nonnull final AS2Message aMsg) throws InvalidParameterException
{
final Partnership aPartnership = aMsg.partnership ();
try
{
InvalidParameterException.checkValue (aMsg, "ContentType", aMsg.getContentType ());
InvalidParameterException.checkValue (aMsg,
"Attribute: " + CPartnershipIDs.PA_AS2_URL,
aPartnership.getAS2URL ());
InvalidParameterException.checkValue (aMsg,
"Receiver: " + CPartnershipIDs.PID_AS2,
aPartnership.getReceiverAS2ID ());
InvalidParameterException.checkValue (aMsg, "Sender: " + CPartnershipIDs.PID_AS2, aPartnership.getSenderAS2ID ());
InvalidParameterException.checkValue (aMsg, "Subject", aMsg.getSubject ());
InvalidParameterException.checkValue (aMsg,
"Sender: " + CPartnershipIDs.PID_EMAIL,
aPartnership.getSenderEmail ());
InvalidParameterException.checkValue (aMsg, "Message Data", aMsg.getData ());
}
catch (final InvalidParameterException ex)
{
ex.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
throw ex;
}
}
// Asynch MDN 2007-03-12
/**
* for storing original mic and outgoing file into pending information file
*
* @param aMsg
* AS2Message
* @param sMIC
* MIC value
* @throws OpenAS2Exception
* In case of an error
*/
protected void storePendingInfo (@Nonnull final AS2Message aMsg, @Nonnull final String sMIC) throws OpenAS2Exception
{
try
{
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Original MIC is '" + sMIC + "'" + aMsg.getLoggingText ());
final String sPendingFolder = FilenameHelper.getAsSecureValidASCIIFilename (getSession ().getMessageProcessor ()
.getAsString (ATTR_PENDINGMDNINFO));
final String sMsgFilename = AS2IOHelper.getFilenameFromMessageID (aMsg.getMessageID ());
final String sPendingFilename = FilenameHelper.getAsSecureValidASCIIFilename (getSession ().getMessageProcessor ()
.getAsString (ATTR_PENDINGMDN)) +
"/" +
sMsgFilename;
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Save Original MIC & message id information into folder '" +
sPendingFolder +
"'" +
aMsg.getLoggingText ());
// input pending folder & original outgoing file name to get and
// unique file name in order to avoid file overwriting.
try (final Writer aWriter = FileHelper.getWriter (new File (sPendingFolder + "/" + sMsgFilename),
StandardCharsets.ISO_8859_1))
{
aWriter.write (sMIC + "\n" + sPendingFilename);
}
// remember
aMsg.attrs ().putIn (CFileAttribute.MA_PENDING_FILENAME, sPendingFilename);
aMsg.attrs ().putIn (CFileAttribute.MA_STATUS, CFileAttribute.MA_STATUS_PENDING);
}
catch (final Exception ex)
{
final OpenAS2Exception we = WrappedOpenAS2Exception.wrap (ex);
we.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
throw we;
}
}
/**
* From RFC 4130 section 7.3.1:
* <ul>
* <li>For any signed messages, the MIC to be returned is calculated on the
* RFC1767/RFC3023 MIME header and content. Canonicalization on the MIME
* headers MUST be performed before the MIC is calculated, since the sender
* requesting the signed receipt was also REQUIRED to canonicalize.</li>
* <li>For encrypted, unsigned messages, the MIC to be returned is calculated
* on the decrypted RFC 1767/RFC3023 MIME header and content. The content
* after decryption MUST be canonicalized before the MIC is calculated.</li>
* <li>For unsigned, unencrypted messages, the MIC MUST be calculated over the
* message contents without the MIME or any other RFC 2822 headers, since
* these are sometimes altered or reordered by Mail Transport Agents (MTAs).
* </li>
* </ul>
* So headers must be included if signing or crypting is enabled.<br>
* <br>
* From RFC 5402 section 4.1:
* <ul>
* <li>MIC Calculation for Signed Message: For any signed message, the MIC to
* be returned is calculated over the same data that was signed in the
* original message as per [AS1]. The signed content will be a MIME bodypart
* that contains either compressed or uncompressed data.</li>
* <li>MIC Calculation for Encrypted, Unsigned Message: For encrypted,
* unsigned messages, the MIC to be returned is calculated over the
* uncompressed data content including all MIME header fields and any applied
* Content-Transfer-Encoding.</li>
* <li>MIC Calculation for Unencrypted, Unsigned Message: For unsigned,
* unencrypted messages, the MIC is calculated over the uncompressed data
* content including all MIME header fields and any applied
* Content-Transfer-Encoding</li>
* </ul>
* So headers must always be included if compression is enabled.
*
* @param aMsg
* Source message
* @return MIC value. Neither <code>null</code> nor empty.
* @throws Exception
* On security or AS2 issues
*/
@Nonnull
@Nonempty
protected String calculateAndStoreMIC (@Nonnull final AS2Message aMsg) throws Exception
{
final Partnership aPartnership = aMsg.partnership ();
final String sDispositionOptions = aPartnership.getAS2MDNOptions ();
final DispositionOptions aDispositionOptions = DispositionOptions.createFromString (sDispositionOptions);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("DispositionOptions=" + aDispositionOptions);
// Calculate and get the original mic
final boolean bIncludeHeadersInMIC = aPartnership.getSigningAlgorithm () != null ||
aPartnership.getEncryptAlgorithm () != null ||
aPartnership.getCompressionType () != null;
final String sMIC = AS2Helper.getCryptoHelper ()
.calculateMIC (aMsg.getData (),
aDispositionOptions.getFirstMICAlg (),
bIncludeHeadersInMIC);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Calculated MIC: '" + sMIC + "'");
if (aPartnership.getAS2ReceiptDeliveryOption () != null)
{
// if yes : PA_AS2_RECEIPT_OPTION != null
// then keep the original mic & message id.
// then wait for the another HTTP call by receivers
storePendingInfo (aMsg, sMIC);
}
return sMIC;
}
@Nonnull
protected MimeBodyPart compress (@Nonnull final IMessage aMsg,
@Nonnull final MimeBodyPart aData,
@Nonnull final ECompressionType eCompressionType) throws SMIMEException,
MessagingException
{
final SMIMECompressedGenerator aCompressedGenerator = new SMIMECompressedGenerator ();
// Content-Transfer-Encoding to use
final String sCTE = aMsg.partnership ()
.getContentTransferEncodingSend (EContentTransferEncoding.AS2_DEFAULT.getID ());
aCompressedGenerator.setContentTransferEncoding (sCTE);
final MimeBodyPart aCompressedBodyPart = aCompressedGenerator.generate (aData,
eCompressionType.createOutputCompressor ());
aMsg.headers ().addHeader (CHttpHeader.CONTENT_TRANSFER_ENCODING, sCTE);
// TODO use constant
aMsg.headers ().addHeader (CHttpHeader.CONTENT_TYPE, "application/octet-stream");
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Compressed data with " +
eCompressionType +
" to " +
aCompressedBodyPart.getContentType () +
":" +
aMsg.getLoggingText ());
return aCompressedBodyPart;
}
@Nonnull
protected MimeBodyPart secure (@Nonnull final IMessage aMsg) throws Exception
{
// Set up encrypt/sign variables
MimeBodyPart aDataBP = aMsg.getData ();
final Partnership aPartnership = aMsg.partnership ();
final ICertificateFactory aCertFactory = getSession ().getCertificateFactory ();
// Check compression parameters
// If compression is enabled, by default is is compressed before signing
final String sCompressionType = aPartnership.getCompressionType ();
ECompressionType eCompressionType = null;
boolean bCompressBeforeSign = true;
if (sCompressionType != null)
{
eCompressionType = ECompressionType.getFromIDCaseInsensitiveOrNull (sCompressionType);
if (eCompressionType == null)
throw new OpenAS2Exception ("The compression type '" + sCompressionType + "' is not supported!");
bCompressBeforeSign = aPartnership.isCompressBeforeSign ();
}
if (eCompressionType != null && bCompressBeforeSign)
{
// Compress before sign
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Compressing outbound message before signing...");
aDataBP = compress (aMsg, aDataBP, eCompressionType);
// Replace the message data, because it is the basis for the MIC
aMsg.setData (aDataBP);
}
// Sign the data if requested
final String sSignAlgorithm = aPartnership.getSigningAlgorithm ();
if (sSignAlgorithm != null)
{
final X509Certificate aSenderCert = aCertFactory.getCertificate (aMsg, ECertificatePartnershipType.SENDER);
final PrivateKey aSenderKey = aCertFactory.getPrivateKey (aMsg, aSenderCert);
final ECryptoAlgorithmSign eSignAlgorithm = ECryptoAlgorithmSign.getFromIDOrNull (sSignAlgorithm);
if (eSignAlgorithm == null)
throw new OpenAS2Exception ("The signing algorithm '" + sSignAlgorithm + "' is not supported!");
// Include certificate in signed content?
boolean bIncludeCertificateInSignedContent;
final ETriState eIncludeCertificateInSignedContent = aMsg.partnership ().getIncludeCertificateInSignedContent ();
if (eIncludeCertificateInSignedContent.isDefined ())
{
// Use per partnership
bIncludeCertificateInSignedContent = eIncludeCertificateInSignedContent.getAsBooleanValue ();
}
else
{
// Use global value
bIncludeCertificateInSignedContent = getSession ().isCryptoSignIncludeCertificateInBodyPart ();
}
// Use old MIC algorithms?
final boolean bUseRFC3851MICAlg = aPartnership.isRFC3851MICAlgs ();
// Main signing
aDataBP = AS2Helper.getCryptoHelper ()
.sign (aDataBP,
aSenderCert,
aSenderKey,
eSignAlgorithm,
bIncludeCertificateInSignedContent,
bUseRFC3851MICAlg);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Signed data with " +
eSignAlgorithm +
" to " +
aDataBP.getContentType () +
":" +
aMsg.getLoggingText ());
}
if (eCompressionType != null && !bCompressBeforeSign)
{
// Compress after sign
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Compressing outbound message after signing...");
aDataBP = compress (aMsg, aDataBP, eCompressionType);
}
// Encrypt the data if requested
final String sCryptAlgorithm = aPartnership.getEncryptAlgorithm ();
if (sCryptAlgorithm != null)
{
final X509Certificate aReceiverCert = aCertFactory.getCertificate (aMsg, ECertificatePartnershipType.RECEIVER);
final ECryptoAlgorithmCrypt eCryptAlgorithm = ECryptoAlgorithmCrypt.getFromIDOrNull (sCryptAlgorithm);
if (eCryptAlgorithm == null)
throw new OpenAS2Exception ("The crypting algorithm '" + sCryptAlgorithm + "' is not supported!");
aDataBP = AS2Helper.getCryptoHelper ().encrypt (aDataBP, aReceiverCert, eCryptAlgorithm);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Encrypted data with " +
eCryptAlgorithm +
" to " +
aDataBP.getContentType () +
":" +
aMsg.getLoggingText ());
}
return aDataBP;
}
/**
* Update the HTTP headers based on the provided message, before sending takes
* place.
*
* @param aConn
* The connection abstraction. Never <code>null</code>.
* @param aMsg
* The message to be send. Never <code>null</code>.
*/
protected void updateHttpHeaders (@Nonnull final IAS2HttpHeaderWrapper aConn, @Nonnull final IMessage aMsg)
{
final Partnership aPartnership = aMsg.partnership ();
// Set all custom headers first (so that they are overridden with the
// mandatory ones in here)
aMsg.headers ().forEachSingleHeader (aConn::setHttpHeader);
aConn.setHttpHeader (CHttpHeader.CONNECTION, CAS2Header.DEFAULT_CONNECTION);
aConn.setHttpHeader (CHttpHeader.USER_AGENT, CAS2Header.DEFAULT_USER_AGENT);
aConn.setHttpHeader (CHttpHeader.DATE, AS2DateHelper.getFormattedDateNow (CAS2Header.DEFAULT_DATE_FORMAT));
aConn.setHttpHeader (CHttpHeader.MESSAGE_ID, aMsg.getMessageID ());
// make sure this is the encoding used in the msg, run TBF1
aConn.setHttpHeader (CHttpHeader.MIME_VERSION, CAS2Header.DEFAULT_MIME_VERSION);
aConn.setHttpHeader (CHttpHeader.CONTENT_TYPE, aMsg.getContentType ());
aConn.setHttpHeader (CHttpHeader.AS2_VERSION, CAS2Header.DEFAULT_AS2_VERSION);
aConn.setHttpHeader (CHttpHeader.RECIPIENT_ADDRESS, aPartnership.getAS2URL ());
aConn.setHttpHeader (CHttpHeader.AS2_FROM, aPartnership.getSenderAS2ID ());
aConn.setHttpHeader (CHttpHeader.AS2_TO, aPartnership.getReceiverAS2ID ());
aConn.setHttpHeader (CHttpHeader.SUBJECT, aMsg.getSubject ());
aConn.setHttpHeader (CHttpHeader.FROM, aPartnership.getSenderEmail ());
// Set when compression is enabled
aConn.setHttpHeader (CHttpHeader.CONTENT_TRANSFER_ENCODING, aMsg.getHeader (CHttpHeader.CONTENT_TRANSFER_ENCODING));
// Determine where to send the MDN to (legacy field)
final String sDispTo = aPartnership.getAS2MDNTo ();
if (sDispTo != null)
aConn.setHttpHeader (CHttpHeader.DISPOSITION_NOTIFICATION_TO, sDispTo);
// MDN requirements
final String sDispositionNotificationOptions = aPartnership.getAS2MDNOptions ();
if (sDispositionNotificationOptions != null)
aConn.setHttpHeader (CHttpHeader.DISPOSITION_NOTIFICATION_OPTIONS, sDispositionNotificationOptions);
// Async MDN 2007-03-12
final String sReceiptDeliveryOption = aPartnership.getAS2ReceiptDeliveryOption ();
if (sReceiptDeliveryOption != null)
aConn.setHttpHeader (CHttpHeader.RECEIPT_DELIVERY_OPTION, sReceiptDeliveryOption);
// As of 2007-06-01
final String sContententDisposition = aMsg.getContentDisposition ();
if (sContententDisposition != null)
aConn.setHttpHeader (CHttpHeader.CONTENT_DISPOSITION, sContententDisposition);
}
/**
* @param aMsg
* AS2Message
* @param aConn
* URLConnection
* @param sOriginalMIC
* mic value from original msg
* @throws OpenAS2Exception
* in case of an error
* @throws IOException
* in case of an IO error
*/
protected void receiveSyncMDN (@Nonnull final AS2Message aMsg,
@Nonnull final IAS2HttpConnection aConn,
@Nonnull final String sOriginalMIC) throws OpenAS2Exception, IOException
{
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Receiving synchronous MDN for message" + aMsg.getLoggingText ());
try
{
// Create a MessageMDN and copy HTTP headers
final IMessageMDN aMDN = new AS2MessageMDN (aMsg);
aMDN.headers ().addAllHeaders (aConn.getHeaderFields ());
// Receive the MDN data
final InputStream aConnIS = aConn.getInputStream ();
final NonBlockingByteArrayOutputStream aMDNStream = new NonBlockingByteArrayOutputStream ();
try
{
// Retrieve the whole MDN content
final long nContentLength = StringParser.parseLong (aMDN.getHeader (CHttpHeader.CONTENT_LENGTH), -1);
if (nContentLength >= 0)
StreamHelper.copyInputStreamToOutputStreamWithLimit (aConnIS, aMDNStream, nContentLength);
else
StreamHelper.copyInputStreamToOutputStream (aConnIS, aMDNStream);
}
finally
{
StreamHelper.close (aMDNStream);
}
final IHTTPIncomingDumper aIncomingDumper = HTTPHelper.getHTTPIncomingDumper ();
if (aIncomingDumper != null)
aIncomingDumper.dumpIncomingRequest (aMDN.headers ().getAllHeaderLines (), aMDNStream.toByteArray (), aMDN);
if (LOGGER.isTraceEnabled ())
{
// Debug print the whole MDN stream
LOGGER.trace ("Retrieved MDN stream data:\n" + aMDNStream.getAsString (StandardCharsets.ISO_8859_1));
}
final MimeBodyPart aPart = new MimeBodyPart (AS2HttpHelper.getAsInternetHeaders (aMDN.headers ()),
aMDNStream.toByteArray ());
aMsg.getMDN ().setData (aPart);
// get the MDN partnership info
aMDN.partnership ().setSenderAS2ID (aMDN.getHeader (CHttpHeader.AS2_FROM));
aMDN.partnership ().setReceiverAS2ID (aMDN.getHeader (CHttpHeader.AS2_TO));
// Set the appropriate key store aliases
aMDN.partnership ().setSenderX509Alias (aMsg.partnership ().getReceiverX509Alias ());
aMDN.partnership ().setReceiverX509Alias (aMsg.partnership ().getSenderX509Alias ());
// Update the partnership
getSession ().getPartnershipFactory ().updatePartnership (aMDN, false);
final ICertificateFactory aCertFactory = getSession ().getCertificateFactory ();
final X509Certificate aSenderCert = aCertFactory.getCertificate (aMDN, ECertificatePartnershipType.SENDER);
boolean bUseCertificateInBodyPart;
final ETriState eUseCertificateInBodyPart = aMsg.partnership ().getVerifyUseCertificateInBodyPart ();
if (eUseCertificateInBodyPart.isDefined ())
{
// Use per partnership
bUseCertificateInBodyPart = eUseCertificateInBodyPart.getAsBooleanValue ();
}
else
{
// Use global value
bUseCertificateInBodyPart = getSession ().isCryptoVerifyUseCertificateInBodyPart ();
}
AS2Helper.parseMDN (aMsg, aSenderCert, bUseCertificateInBodyPart);
try
{
getSession ().getMessageProcessor ().handle (IProcessorStorageModule.DO_STOREMDN, aMsg, null);
}
catch (final ComponentNotFoundException | NoModuleException ex)
{
// No message processor found
// Or no module found in message processor
}
final String sDisposition = aMsg.getMDN ().attrs ().getAsString (AS2MessageMDN.MDNA_DISPOSITION);
if (LOGGER.isInfoEnabled ())
LOGGER.info ("received MDN [" + sDisposition + "]" + aMsg.getLoggingText ());
// Asynch MDN 2007-03-12
// Verify if the original mic is equal to the mic in returned MDN
final String sReturnMIC = aMsg.getMDN ().attrs ().getAsString (AS2MessageMDN.MDNA_MIC);
// Catch ReturnMIC == null in case the attribute is simply missing
if (sReturnMIC == null || !sReturnMIC.replaceAll ("\\s+", "").equals (sOriginalMIC.replaceAll ("\\s+", "")))
{
// file was sent completely but the returned mic was not matched,
// don't know it needs or needs not to be resent ? it's depended on
// what! anyway, just log the warning message here.
if (LOGGER.isInfoEnabled ())
LOGGER.info ("MIC IS NOT MATCHED, original mic: '" +
sOriginalMIC +
"' return mic: '" +
sReturnMIC +
"'" +
aMsg.getLoggingText ());
}
else
{
if (LOGGER.isInfoEnabled ())
LOGGER.info ("MIC is matched, MIC: " + sReturnMIC + aMsg.getLoggingText ());
}
try
{
DispositionType.createFromString (sDisposition).validate ();
}
catch (final DispositionException ex)
{
ex.setText (aMsg.getMDN ().getText ());
if (ex.getDisposition ().isWarning ())
{
// Warning
ex.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
ex.terminate ();
}
else
{
// Error
throw ex;
}
}
}
catch (final IOException ex)
{
throw ex;
}
catch (final Exception ex)
{
final OpenAS2Exception we = WrappedOpenAS2Exception.wrap (ex);
we.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
throw we;
}
}
/**
* Handler for errors in MDN processing.
*
* @param aMsg
* The source message that was send
* @param ex
* The exception that was caught
* @throws OpenAS2Exception
* In case an overload wants to throw the exception
*/
@OverrideOnDemand
protected void onReceivedMDNError (@Nonnull final AS2Message aMsg,
@Nonnull final OpenAS2Exception ex) throws OpenAS2Exception
{
final OpenAS2Exception oae2 = new OpenAS2Exception ("Message was sent but an error occured while receiving the MDN",
ex);
oae2.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
oae2.terminate ();
}
private void _sendViaHTTP (@Nonnull final AS2Message aMsg,
@Nonnull final MimeBodyPart aSecuredMimePart,
@Nonnull final String sMIC) throws OpenAS2Exception, IOException, MessagingException
{
final Partnership aPartnership = aMsg.partnership ();
// Create the HTTP connection
final String sUrl = aPartnership.getAS2URL ();
final EHttpMethod eRequestMethod = EHttpMethod.POST;
// decide on the connection type to use according to the MimeBodyPart:
// If it contains the data, (and no DataHandler), then use HttpUrlClient,
// otherwise, use HttpClient
final IAS2HttpConnection aConn;
if (!getAsBoolean (MessageParameters.ATTR_LARGE_FILE_SUPPORT_ON))
{
final boolean bOutput = true;
final boolean bInput = true;
final boolean bUseCaches = false;
aConn = getHttpURLConnection (sUrl, bOutput, bInput, bUseCaches, eRequestMethod, getSession ().getHttpProxy ());
}
else
{
aConn = getHttpClient (sUrl, eRequestMethod, getSession ().getHttpProxy ());
}
try (final IHTTPOutgoingDumper aOutgoingDumper = HTTPHelper.getHTTPOutgoingDumper (aMsg))
{
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Connecting to " + sUrl + aMsg.getLoggingText ());
updateHttpHeaders (new AS2HttpHeaderWrapperHttpURLConnection (aConn, aOutgoingDumper), aMsg);
if (aOutgoingDumper != null)
aOutgoingDumper.finishedHeaders ();
aMsg.attrs ().putIn (CNetAttribute.MA_DESTINATION_IP, aConn.getURL ().getHost ());
aMsg.attrs ().putIn (CNetAttribute.MA_DESTINATION_PORT, aConn.getURL ().getPort ());
final InputStream aMsgIS = aSecuredMimePart.getInputStream ();
if (!getAsBoolean (MessageParameters.ATTR_LARGE_FILE_SUPPORT_ON))
{
// Note: closing this stream causes connection abort errors on some AS2
// servers
OutputStream aMsgOS = aConn.getOutputStream ();
// This stream dumps the HTTP
if (aOutgoingDumper != null)
{
// Overwrite the used OutputStream to additionally log to the debug
// OutputStream
aMsgOS = new WrappedOutputStream (aMsgOS)
{
@Override
public final void write (final int b) throws IOException
{
super.write (b);
aOutgoingDumper.dumpPayload (b);
}
};
}
// Transfer the data
final StopWatch aSW = StopWatch.createdStarted ();
// Main transmission - closes InputStream
final long nBytes = AS2IOHelper.copy (aMsgIS, aMsgOS);
aSW.stop ();
LOGGER.info ("transferred " + AS2IOHelper.getTransferRate (nBytes, aSW) + aMsg.getLoggingText ());
}
else
{
// HttpClient option
// Transfer the data
final StopWatch aSW = StopWatch.createdStarted ();
aConn.send (aMsgIS);
aSW.stop ();
// TODO: count sent bytes
// LOGGER.info ("transferred " + AS2IOHelper.getTransferRate (nBytes,
// aSW) + aMsg.getLoggingText ());
}
if (aOutgoingDumper != null)
aOutgoingDumper.finishedPayload ();
// Check the HTTP Response code
final int nResponseCode = aConn.getResponseCode ();
// Accept most of 2xx HTTP response codes
if (nResponseCode != HttpURLConnection.HTTP_OK &&
nResponseCode != HttpURLConnection.HTTP_CREATED &&
nResponseCode != HttpURLConnection.HTTP_ACCEPTED &&
nResponseCode != HttpURLConnection.HTTP_NO_CONTENT &&
nResponseCode != HttpURLConnection.HTTP_PARTIAL)
{
if (LOGGER.isErrorEnabled ())
LOGGER.error ("Error URL '" + sUrl + "' - HTTP " + nResponseCode + " " + aConn.getResponseMessage ());
throw new HttpResponseException (sUrl, nResponseCode, aConn.getResponseMessage ());
}
// Asynch MDN 2007-03-12
// Receive an MDN
try
{
// Receive an MDN
if (aMsg.isRequestingMDN ())
{
// Check if the AsyncMDN is required
if (aPartnership.getAS2ReceiptDeliveryOption () == null)
{
// go ahead to receive sync MDN
receiveSyncMDN (aMsg, aConn, sMIC);
if (LOGGER.isInfoEnabled ())
LOGGER.info ("message sent" + aMsg.getLoggingText ());
}
}
}
catch (final DispositionException ex)
{
// If a disposition error hasn't been handled, the message transfer
// was not successful
throw ex;
}
catch (final OpenAS2Exception ex)
{
// Don't re-send or fail, just log an error if one occurs while
// receiving the MDN
onReceivedMDNError (aMsg, ex);
}
}
finally
{
aConn.disconnect ();
}
}
public void handle (@Nonnull final String sAction,
@Nonnull final IMessage aBaseMsg,
@Nullable final Map <String, Object> aOptions) throws OpenAS2Exception
{
final AS2Message aMsg = (AS2Message) aBaseMsg;
if (LOGGER.isInfoEnabled ())
LOGGER.info ("Submitting message" + aMsg.getLoggingText ());
// verify all required information is present for sending
checkRequired (aMsg);
final int nRetries = getRetryCount (aMsg.partnership (), aOptions);
try
{
// compress and/or sign and/or encrypt the message if needed
final MimeBodyPart aSecuredData = secure (aMsg);
// Calculate MIC after compress/sign/crypt was handled, because the
// message data might change if compression before signing is active.
final String sMIC = calculateAndStoreMIC (aMsg);
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Setting message content type to '" + aSecuredData.getContentType () + "'");
aMsg.setContentType (aSecuredData.getContentType ());
_sendViaHTTP (aMsg, aSecuredData, sMIC);
}
catch (final HttpResponseException ex)
{
if (LOGGER.isErrorEnabled ())
LOGGER.error ("Http Response Error " + ex.getMessage ());
ex.terminate ();
if (!doResend (IProcessorSenderModule.DO_SEND, aMsg, ex, nRetries))
throw ex;
}
catch (final IOException ex)
{
// Re-send if a network error occurs during transmission
final OpenAS2Exception wioe = WrappedOpenAS2Exception.wrap (ex);
wioe.addSource (OpenAS2Exception.SOURCE_MESSAGE, aMsg);
wioe.terminate ();
if (!doResend (IProcessorSenderModule.DO_SEND, aMsg, wioe, nRetries))
throw wioe;
}
catch (final Exception ex)
{
// Propagate error if it can't be handled by a re-send
throw WrappedOpenAS2Exception.wrap (ex);
}
}
}