3737import org .ccsds .moims .mo .mal .*;
3838import org .ccsds .moims .mo .mal .encoding .MALElementStreamFactory ;
3939import org .ccsds .moims .mo .mal .structures .*;
40- import org .ccsds .moims .mo .mal .structures .InteractionType ;
4140import org .ccsds .moims .mo .mal .transport .*;
4241
4342/**
@@ -95,6 +94,11 @@ public abstract class Transport<I, O> implements MALTransport {
9594 * transport routing name.
9695 */
9796 protected final EndpointRegistry endpoints = new EndpointRegistry ();
97+ /**
98+ * Builds and sends the error messages that answer messages which could not
99+ * be delivered or processed.
100+ */
101+ protected final ErrorReplyBuilder errorReplies ;
98102 /**
99103 * Map of QoS properties.
100104 */
@@ -164,6 +168,7 @@ public Transport(final String protocol,
164168 this .addressing = new TransportAddressing (protocol , protocolDelim ,
165169 serviceDelim , routingDelim , supportsRouting );
166170 this .qosProperties = properties ;
171+ this .errorReplies = new ErrorReplyBuilder (this , endpoints , properties );
167172
168173 streamFactory = MALElementStreamFactory .newFactory (protocol , properties );
169174 LOGGER .log (Level .FINE , "Created element stream: {0}" ,
@@ -440,12 +445,16 @@ public void receiveIncomingMessage(final IncomingMessageHolder malMsg) {
440445 * @param smsg The message in a string representation for logging.
441446 */
442447 public void dispatchMessage (final GENMessage msg , PacketToString smsg ) {
448+ // Held outside the try so that, if the delivery below throws, the error
449+ // can be returned from the endpoint the message was destined for.
450+ Endpoint endpoint = null ;
451+
443452 try {
444453 LOGGER .log (Level .FINE , "Processing message : {0} : {1}" ,
445454 new Object []{msg .getHeader ().getTransactionId (), smsg });
446455
447456 String endpointUriPart = getRoutingPart (msg .getHeader ().getTo ().getValue ());
448- final Endpoint endpoint = endpoints .getByRoutingName (endpointUriPart );
457+ endpoint = endpoints .getByRoutingName (endpointUriPart );
449458
450459 if (endpoint != null ) {
451460 LOGGER .log (Level .FINE , "Passing message to endpoint {0} : {1}" ,
@@ -466,7 +475,7 @@ public void dispatchMessage(final GENMessage msg, PacketToString smsg) {
466475 e .printStackTrace (new PrintWriter (wrt ));
467476
468477 try {
469- returnErrorMessage (msg .getHeader (), MALHelper .INTERNAL_ERROR_NUMBER ,
478+ returnErrorMessage (endpoint , msg .getHeader (), MALHelper .INTERNAL_ERROR_NUMBER ,
470479 "Error occurred: " + e .toString () + " : " + wrt .toString ());
471480 } catch (MALException ex ) {
472481 LOGGER .log (Level .SEVERE ,
@@ -482,7 +491,7 @@ public void dispatchMessage(final GENMessage msg, PacketToString smsg) {
482491 e .printStackTrace (new PrintWriter (wrt ));
483492
484493 try {
485- returnErrorMessage (msg .getHeader (), MALHelper .INTERNAL_ERROR_NUMBER ,
494+ returnErrorMessage (endpoint , msg .getHeader (), MALHelper .INTERNAL_ERROR_NUMBER ,
486495 "Error occurred: " + e .toString () + " : " + wrt .toString ());
487496 } catch (MALException ex ) {
488497 LOGGER .log (Level .SEVERE , "Error occurred when return error data : {0}" , ex );
@@ -500,56 +509,23 @@ public void dispatchMessage(final GENMessage msg, PacketToString smsg) {
500509 */
501510 protected void returnErrorMessage (final MALMessageHeader srcHdr ,
502511 final UInteger errorNumber , final String errorMsg ) throws MALException {
503- try {
504- InteractionType interactionType = srcHdr .getInteractionType ();
505- final short stage = (null != srcHdr .getInteractionStage ())
506- ? srcHdr .getInteractionStage ().getValue () : 0 ;
507-
508- // first check that message should be responded to
509- if (((interactionType .equals (InteractionType .SUBMIT )) && (stage == MALSubmitOperation ._SUBMIT_STAGE ))
510- || ((interactionType .equals (InteractionType .REQUEST )) && (stage == MALRequestOperation ._REQUEST_STAGE ))
511- || ((interactionType .equals (InteractionType .INVOKE )) && (stage == MALInvokeOperation ._INVOKE_STAGE ))
512- || ((interactionType .equals (InteractionType .PROGRESS )) && (stage == MALProgressOperation ._PROGRESS_STAGE ))
513- || ((interactionType .equals (InteractionType .PUBSUB )) && (stage == MALPubSubOperation ._REGISTER_STAGE ))
514- || ((interactionType .equals (InteractionType .PUBSUB )) && (stage == MALPubSubOperation ._DEREGISTER_STAGE ))
515- || ((interactionType .equals (InteractionType .PUBSUB )) && (stage == MALPubSubOperation ._PUBLISH_REGISTER_STAGE ))
516- || ((interactionType .equals (InteractionType .PUBSUB )) && (stage == MALPubSubOperation ._PUBLISH_DEREGISTER_STAGE ))) {
517-
518- Endpoint endpoint = endpoints .any ();
519-
520- if (endpoint != null ) {
521- final GENMessage retMsg = (GENMessage ) endpoint .createMessage (srcHdr .getAuthenticationId (),
522- srcHdr .getFromURI (),
523- Time .now (),
524- srcHdr .getInteractionType (),
525- new UOctet ((short ) (srcHdr .getInteractionStage ().getValue () + 1 )),
526- srcHdr .getTransactionId (),
527- srcHdr .getServiceArea (),
528- srcHdr .getService (),
529- srcHdr .getOperation (),
530- srcHdr .getAreaVersion (),
531- true ,
532- srcHdr .getSupplements (),
533- qosProperties ,
534- errorNumber , new Union (errorMsg ));
535-
536- sendMessage (null , true , retMsg );
537- } else {
538- LOGGER .log (Level .WARNING , "(1) Unable to return error"
539- + " number ({0}) as no endpoint supplied: {1}" ,
540- new Object []{errorNumber , srcHdr });
541- }
542- } else {
543- LOGGER .log (Level .WARNING , "An MO Error will not be returned because this "
544- + "combination of type/stage does not have an MO Error to "
545- + "be returned! For interaction type: {0} - and stage: {1}" ,
546- new Object []{interactionType .toString (), stage });
547- }
548- } catch (MALTransmitErrorException ex ) {
549- LOGGER .log (Level .WARNING ,
550- "Error occurred when attempting to return previous error!" ,
551- ex );
552- }
512+ returnErrorMessage (null , srcHdr , errorNumber , errorMsg );
513+ }
514+
515+ /**
516+ * Creates a return error message based on a received message, sent from the
517+ * endpoint the received message was being delivered to.
518+ *
519+ * @param endpoint The endpoint the message was being delivered to, or null
520+ * if it is not known.
521+ * @param srcHdr The source header
522+ * @param errorNumber The error number
523+ * @param errorMsg The error message.
524+ * @throws MALException if cannot encode a response message
525+ */
526+ protected void returnErrorMessage (final Endpoint endpoint , final MALMessageHeader srcHdr ,
527+ final UInteger errorNumber , final String errorMsg ) throws MALException {
528+ errorReplies .returnError (endpoint , srcHdr , errorNumber , errorMsg );
553529 }
554530
555531 /**
0 commit comments