As2InboundHandler { public MimeMessage receive(InternetHeaders httpHeaders, MimeMessage mimeMessage, Span root) throws OxalisAs2InboundException { TransmissionIdentifier transmissionIdentifier = null; Header header = null; Path payloadPath = null; OxalisAs2InboundException exception; try { SignedMessage message = SignedMessage.load(mimeMessage); message.validate(Service.AP, certificateValidator, httpHeaders.getHeader(As2Header.AS2_FROM)[0].replace("\"", "")); Timestamp t2 = timestampProvider.generate(message.getSignature(), Direction.IN); Tag tag = tagGenerator.generate(Direction.IN); MdnBuilder mdnBuilder = MdnBuilder.newInstance(mimeMessage); mdnBuilder.addHeader(MdnHeader.DATE, t2.getDate()); transmissionIdentifier = TransmissionIdentifier.fromHeader(httpHeaders.getHeader(As2Header.MESSAGE_ID)[0]); mdnBuilder.addHeader(MdnHeader.ORIGINAL_MESSAGE_ID, httpHeaders.getHeader(As2Header.MESSAGE_ID)[0]); SMimeDigestMethod digestMethod = SMimeDigestMethod.findByIdentifier(message.getMicalg()); byte[] headerBytes = message.getBodyHeader(); mdnBuilder.addHeader(MdnHeader.ORIGINAL_CONTENT_HEADER, headerBytes); byte[] content = message.getContentBytes(); header = headerParser.parse(new ByteArrayInputStream(content)); transmissionVerifier.verify(header, Direction.IN); try (InputStream payloadInputStream = new ByteArrayInputStream(content)) { payloadPath = persisterHandler.persist(transmissionIdentifier, header, payloadInputStream); } Digest calculatedDigest = Digest.of(digestMethod.getDigestMethod(), message.getDigest()); mdnBuilder.addHeader(MdnHeader.RECEIVED_CONTENT_MIC, new Mic(calculatedDigest)); String messageId = messageIdGenerator.generate(new As2InboundMetadata(transmissionIdentifier, header, t2, null, null, message.getSigner(), null, tag)); if (!MessageIdUtil.verify(messageId)) throw new OxalisAs2InboundException( "Invalid Message-ID '" + messageId + "' generated.", Disposition.UNEXPECTED_PROCESSING_ERROR); mdnBuilder.addHeader(MdnHeader.DISPOSITION, Disposition.PROCESSED); MimeMessage mdn = sMimeMessageFactory.createSignedMimeMessage(mdnBuilder.build(), digestMethod); mdn.setHeader(As2Header.MESSAGE_ID, messageId); mdn.setHeader(As2Header.AS2_VERSION, As2Header.VERSION); mdn.setHeader(As2Header.AS2_FROM, httpHeaders.getHeader(As2Header.AS2_TO)[0]); mdn.setHeader(As2Header.AS2_TO, httpHeaders.getHeader(As2Header.AS2_FROM)[0]); ByteArrayOutputStream mdnOutputStream = new ByteArrayOutputStream(); mdn.writeTo(mdnOutputStream); As2InboundMetadata inboundMetadata = new As2InboundMetadata(transmissionIdentifier, header, t2, digestMethod.getTransportProfile(), calculatedDigest, message.getSigner(), mdnOutputStream.toByteArray(), tag); persisterHandler.persist(inboundMetadata, payloadPath); inboundService.complete(inboundMetadata); return mdn; } catch (OxalisContentException e) { exception = new OxalisAs2InboundException(Disposition.UNSUPPORTED_FORMAT, e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } catch (NoSuchAlgorithmException e) { exception = new OxalisAs2InboundException(Disposition.UNSUPPORTED_MIC_ALGORITHMS, e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } catch (VerifierException e) { exception = new OxalisAs2InboundException(Disposition.fromVerifierException(e), e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } catch (PeppolSecurityException e) { exception = new OxalisAs2InboundException(Disposition.AUTHENTICATION_FAILED, e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } catch (OxalisSecurityException e) { exception = new OxalisAs2InboundException(Disposition.INTEGRITY_CHECK_FAILED, e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } catch (Exception e) { exception = new OxalisAs2InboundException(Disposition.UNEXPECTED_PROCESSING_ERROR, e.getMessage(), e); persisterHandler.persist(transmissionIdentifier, header, payloadPath, exception); throw exception; } } @Inject  As2InboundHandler(InboundService inboundService, TimestampProvider timestampProvider,
                             OxalisCertificateValidator certificateValidator, PersisterHandler persisterHandler,
                             TransmissionVerifier transmissionVerifier, SMimeMessageFactory sMimeMessageFactory,
                             TagGenerator tagGenerator, MessageIdGenerator messageIdGenerator,
                             HeaderParser headerParser); MimeMessage receive(InternetHeaders httpHeaders, MimeMessage mimeMessage, Span root);  }
@Test(enabled = false) public void testReceive() throws Exception { InputStream inputStream = loadSampleMimeMessage(); As2InboundHandler as2InboundHandler = new As2InboundHandler(Mockito.mock(InboundService.class), mockTimestampProvider, new OxalisCertificateValidator(CertificateValidator.EMPTY, tracer), new NoopPersister(), new DefaultTransmissionVerifier(), sMimeMessageFactory, new NoopTagGenerator(), new DefaultMessageIdGenerator("test"), new SbdhHeaderParser()); MimeMessage mimeMessage = MimeMessageHelper.parse(inputStream, headers); as2InboundHandler.receive(headers, mimeMessage, tracer.buildSpan("test").start()); }