1717package com .helger .phase4 .peppol .server .api ;
1818
1919import java .nio .charset .StandardCharsets ;
20- import java .time .OffsetDateTime ;
2120import java .util .Map ;
2221
2322import javax .annotation .Nonnull ;
2827
2928import com .helger .commons .annotation .Nonempty ;
3029import com .helger .commons .collection .ArrayHelper ;
31- import com .helger .commons .datetime .PDTFactory ;
32- import com .helger .commons .datetime .PDTWebDateHelper ;
3330import com .helger .commons .io .stream .StreamHelper ;
34- import com .helger .commons .lang .StackTraceHelper ;
3531import com .helger .commons .string .StringHelper ;
3632import com .helger .commons .system .EJavaVersion ;
3733import com .helger .commons .timing .StopWatch ;
3834import com .helger .commons .wrapper .Wrapper ;
39- import com .helger .json .IJsonArray ;
40- import com .helger .json .IJsonObject ;
41- import com .helger .json .JsonArray ;
42- import com .helger .json .JsonObject ;
43- import com .helger .json .serialize .JsonWriterSettings ;
4435import com .helger .peppol .servicedomain .EPeppolNetwork ;
4536import com .helger .peppol .sml .ESML ;
4637import com .helger .peppol .sml .ISMLInfo ;
4738import com .helger .peppol .utils .PeppolCAChecker ;
4839import com .helger .peppol .utils .PeppolCertificateChecker ;
49- import com .helger .peppol . utils . PeppolCertificateHelper ;
40+ import com .helger .peppolid . IDocumentTypeIdentifier ;
5041import com .helger .peppolid .IParticipantIdentifier ;
42+ import com .helger .peppolid .IProcessIdentifier ;
5143import com .helger .peppolid .factory .IIdentifierFactory ;
5244import com .helger .peppolid .factory .PeppolIdentifierFactory ;
5345import com .helger .phase4 .client .IAS4ClientBuildMessageCallback ;
5446import com .helger .phase4 .dump .AS4RawResponseConsumerWriteToFile ;
55- import com .helger .phase4 .ebms3header .Ebms3Error ;
56- import com .helger .phase4 .marshaller .Ebms3SignalMessageMarshaller ;
5747import com .helger .phase4 .model .message .AS4UserMessage ;
5848import com .helger .phase4 .model .message .AbstractAS4Message ;
5949import com .helger .phase4 .peppol .Phase4PeppolSender ;
6050import com .helger .phase4 .peppol .Phase4PeppolSender .PeppolUserMessageBuilder ;
51+ import com .helger .phase4 .peppol .Phase4PeppolSendingReport ;
6152import com .helger .phase4 .peppol .server .APConfig ;
6253import com .helger .phase4 .profile .peppol .Phase4PeppolHttpClientSettings ;
6354import com .helger .phase4 .sender .EAS4UserMessageSendResult ;
6455import com .helger .phase4 .util .Phase4Exception ;
6556import com .helger .photon .api .IAPIDescriptor ;
66- import com .helger .security .certificate .CertificateHelper ;
6757import com .helger .servlet .response .UnifiedResponse ;
6858import com .helger .smpclient .peppol .SMPClientReadOnly ;
6959import com .helger .web .scope .IRequestWebScopeWithoutResponse ;
@@ -132,16 +122,10 @@ protected void verifiedInvokeAPI (@Nonnull final IAPIDescriptor aAPIDescriptor,
132122 final PeppolCAChecker aAPCAChecker = m_eStage .isTest () ? PeppolCertificateChecker .peppolTestAP ()
133123 : PeppolCertificateChecker .peppolProductionAP ();
134124 final String sMyPeppolSeatID = APConfig .getMyPeppolSeatID ();
135- final OffsetDateTime aNowUTC = PDTFactory .getCurrentOffsetDateTimeUTC ();
136125
137- final IJsonObject aJson = new JsonObject ();
138- aJson .add ("currentDateTimeUTC" , PDTWebDateHelper .getAsStringXSD (aNowUTC ));
139- aJson .add ("senderId" , sSenderID );
140- aJson .add ("receiverId" , sReceiverID );
141- aJson .add ("docTypeId" , sDocTypeID );
142- aJson .add ("processId" , sProcessID );
143- aJson .add ("countryC1" , sCountryCodeC1 );
144- aJson .add ("senderPartyId" , sMyPeppolSeatID );
126+ final Phase4PeppolSendingReport aSendingReport = new Phase4PeppolSendingReport (aSmlInfo );
127+ aSendingReport .setCountryC1 (sCountryCodeC1 );
128+ aSendingReport .setSenderPartyID (sMyPeppolSeatID );
145129
146130 EAS4UserMessageSendResult eResult = null ;
147131 boolean bExceptionCaught = false ;
@@ -154,7 +138,26 @@ protected void verifiedInvokeAPI (@Nonnull final IAPIDescriptor aAPIDescriptor,
154138 throw new IllegalStateException ("Failed to read provided payload as XML" );
155139
156140 // Start configuring here
141+ final IParticipantIdentifier aSenderID = aIF .createParticipantIdentifierWithDefaultScheme (sSenderID );
142+ aSendingReport .setSenderID (aSenderID );
143+
157144 final IParticipantIdentifier aReceiverID = aIF .createParticipantIdentifierWithDefaultScheme (sReceiverID );
145+ aSendingReport .setReceiverID (aReceiverID );
146+
147+ IDocumentTypeIdentifier aDocTypeID = aIF .parseDocumentTypeIdentifier (sDocTypeID );
148+ if (aDocTypeID == null )
149+ {
150+ // Fallback to default scheme
151+ aDocTypeID = aIF .createDocumentTypeIdentifierWithDefaultScheme (sDocTypeID );
152+ }
153+ aSendingReport .setDocTypeID (aDocTypeID );
154+ IProcessIdentifier aProcessID = aIF .parseProcessIdentifier (sProcessID );
155+ if (aProcessID == null )
156+ {
157+ // Fallback to default scheme
158+ aProcessID = aIF .createProcessIdentifierWithDefaultScheme (sProcessID );
159+ }
160+ aSendingReport .setProcessID (aProcessID );
158161
159162 final SMPClientReadOnly aSMPClient = new SMPClientReadOnly (Phase4PeppolSender .URL_PROVIDER ,
160163 aReceiverID ,
@@ -177,76 +180,48 @@ protected void verifiedInvokeAPI (@Nonnull final IAPIDescriptor aAPIDescriptor,
177180 final PeppolUserMessageBuilder aBuilder ;
178181 aBuilder = Phase4PeppolSender .builder ()
179182 .httpClientFactory (aHCS )
180- .documentTypeID (aIF . createDocumentTypeIdentifierWithDefaultScheme ( sDocTypeID ) )
181- .processID (aIF . createProcessIdentifierWithDefaultScheme ( sProcessID ) )
182- .senderParticipantID (aIF . createParticipantIdentifierWithDefaultScheme ( sSenderID ) )
183+ .documentTypeID (aDocTypeID )
184+ .processID (aProcessID )
185+ .senderParticipantID (aSenderID )
183186 .receiverParticipantID (aReceiverID )
184187 .senderPartyID (sMyPeppolSeatID )
185188 .countryC1 (sCountryCodeC1 )
186189 .payload (aDoc .getDocumentElement ())
187190 .peppolAP_CAChecker (aAPCAChecker )
188191 .smpClient (aSMPClient )
189192 .rawResponseConsumer (new AS4RawResponseConsumerWriteToFile ())
193+ .sbdDocumentConsumer (sbdDoc -> {
194+ // The created SBDH Instance Identifier
195+ aSendingReport .setSBDHInstanceIdentifier (sbdDoc .getStandardBusinessDocumentHeader ()
196+ .getDocumentIdentification ()
197+ .getInstanceIdentifier ());
198+ })
190199 .endpointURLConsumer (endpointUrl -> {
191200 // Determined by SMP lookup
192- aJson . add ( "c3EndpointUrl" , endpointUrl );
201+ aSendingReport . setC3EndpointURL ( endpointUrl );
193202 })
194203 .certificateConsumer ( (aAPCertificate , aCheckDT , eCertCheckResult ) -> {
195204 // Determined by SMP lookup
196- aJson .add ("c3Cert" , CertificateHelper .getPEMEncodedCertificate (aAPCertificate ));
197- aJson .add ("c3CertSubjectCN" ,
198- PeppolCertificateHelper .getSubjectCN (aAPCertificate ));
199- aJson .add ("c3CertCheckDT" , PDTWebDateHelper .getAsStringXSD (aCheckDT ));
200- aJson .add ("c3CertCheckResult" , eCertCheckResult );
205+ aSendingReport .setC3Cert (aAPCertificate );
206+ aSendingReport .setC3CertCheckDT (aCheckDT );
207+ aSendingReport .setC3CertCheckResult (eCertCheckResult );
201208 })
202209 .buildMessageCallback (new IAS4ClientBuildMessageCallback ()
203210 {
204211 public void onAS4Message (@ Nonnull final AbstractAS4Message <?> aMsg )
205212 {
206213 // Created AS4 fields
207214 final AS4UserMessage aUserMsg = (AS4UserMessage ) aMsg ;
208- aJson . add ( "as4MessageId" ,
209- aUserMsg . getEbms3UserMessage () .getMessageInfo (). getMessageId ());
210- aJson . add ( "as4ConversationId" ,
211- aUserMsg .getEbms3UserMessage ()
212- .getCollaborationInfo ()
213- .getConversationId ());
215+ aSendingReport . setAS4MessageID ( aUserMsg . getEbms3UserMessage ()
216+ .getMessageInfo ()
217+ . getMessageId ());
218+ aSendingReport . setAS4ConversationID ( aUserMsg .getEbms3UserMessage ()
219+ .getCollaborationInfo ()
220+ .getConversationId ());
214221 }
215222 })
216223 .signalMsgConsumer ( (aSignalMsg , aMessageMetadata , aState ) -> {
217- aJson .add ("as4ReceivedSignalMsg" ,
218- new Ebms3SignalMessageMarshaller ().getAsString (aSignalMsg ));
219-
220- if (aSignalMsg .hasErrorEntries ())
221- {
222- final IJsonArray aErrors = new JsonArray ();
223- for (final Ebms3Error err : aSignalMsg .getError ())
224- {
225- final IJsonObject aErrorDetails = new JsonObject ();
226- if (err .getDescription () != null )
227- aErrorDetails .add ("description" , err .getDescriptionValue ());
228- if (err .getErrorDetail () != null )
229- aErrorDetails .add ("errorDetails" , err .getErrorDetail ());
230- if (err .getCategory () != null )
231- aErrorDetails .add ("category" , err .getCategory ());
232- if (err .getRefToMessageInError () != null )
233- aErrorDetails .add ("refToMessageInError" , err .getRefToMessageInError ());
234- if (err .getErrorCode () != null )
235- aErrorDetails .add ("errorCode" , err .getErrorCode ());
236- if (err .getOrigin () != null )
237- aErrorDetails .add ("origin" , err .getOrigin ());
238- if (err .getSeverity () != null )
239- aErrorDetails .add ("severity" , err .getSeverity ());
240- if (err .getShortDescription () != null )
241- aErrorDetails .add ("shortDescription" , err .getShortDescription ());
242- aErrors .add (aErrorDetails );
243- LOGGER .warn ("AS4 error received: " + aErrorDetails .getAsJsonString ());
244- }
245- aJson .add ("as4ResponseErrors" , aErrors );
246- aJson .add ("as4ResponseError" , true );
247- }
248- else
249- aJson .add ("as4ResponseError" , false );
224+ aSendingReport .setAS4ReceivedSignalMsg (aSignalMsg );
250225 })
251226 .disableValidation ();
252227 final Wrapper <Phase4Exception > aCaughtEx = new Wrapper <> ();
@@ -264,43 +239,35 @@ public void onAS4Message (@Nonnull final AbstractAS4Message <?> aMsg)
264239 aBuilder .createAndStorePeppolReportingItemAfterSending (sEndUserID );
265240 }
266241
267- aJson . add ( "sendingResult" , eResult );
242+ aSendingReport . setAS4SendingResult ( eResult );
268243
269244 if (aCaughtEx .isSet ())
270245 {
271246 final Phase4Exception ex = aCaughtEx .get ();
272247 LOGGER .error ("Error sending Peppol message via AS4" , ex );
273- aJson .add ("sendingException" ,
274- new JsonObject ().add ("class" , ex .getClass ().getName ())
275- .add ("message" , ex .getMessage ())
276- .add ("stackTrace" , StackTraceHelper .getStackAsString (ex )));
248+ aSendingReport .setAS4SendingException (ex );
277249 bExceptionCaught = true ;
278250 }
279251 }
280252 catch (final Exception ex )
281253 {
282254 // Mostly errors on HTTP level
283255 LOGGER .error ("Error sending Peppol message via AS4" , ex );
284- aJson .add ("sendingException" ,
285- new JsonObject ().add ("class" , ex .getClass ().getName ())
286- .add ("message" , ex .getMessage ())
287- .add ("stackTrace" , StackTraceHelper .getStackAsString (ex )));
256+ aSendingReport .setAS4SendingException (ex );
288257 bExceptionCaught = true ;
289258 }
290259 finally
291260 {
292261 aSW .stop ();
293- aJson . add ( "overallDurationMillis" , aSW .getMillis ());
262+ aSendingReport . setOverallDurationMillis ( aSW .getMillis ());
294263 }
295264
296265 // Result may be null
297266 final boolean bSendingSuccess = eResult != null && eResult .isSuccess ();
298- aJson . add ( "sendingSuccess" , bSendingSuccess );
299- aJson . add ( "overallSuccess" , bSendingSuccess && !bExceptionCaught );
267+ aSendingReport . setSendingSuccess ( bSendingSuccess );
268+ aSendingReport . setOverallSuccess ( bSendingSuccess && !bExceptionCaught );
300269
301270 // Return result JSON
302- aUnifiedResponse .setContentAndCharset (aJson .getAsJsonString (JsonWriterSettings .DEFAULT_SETTINGS_FORMATTED ),
303- StandardCharsets .UTF_8 )
304- .disableCaching ();
271+ aUnifiedResponse .setContentAndCharset (aSendingReport .getAsJsonString (), StandardCharsets .UTF_8 ).disableCaching ();
305272 }
306273}
0 commit comments