Mirth - How to handle response when having multiple destinations - mirth

I created a channel with 2 destinations.
Each one of the destination has a different response and i decide if to use it by filtering message source.
Example 1:
Sending [Message] ->
Destination 1 FILTERED return no response
Destination 2 SENT return [response x]
I want source response to be [response x] but it is not
Example 2:
Sending [Message] ->
Destination 1 SENT return [response y]
Destination 2 FILTERED return no response
I want source response to be [response y] but it is not.
How can i set source response to match SENT destination?
I tried to set Source settings Response on each of the mirth drop-down options, with no success.

You can't use a destination as your response in your situation, as it must always pick the same destination. You do have a couple options, though.
You can use the post-processor for your response. The post-processor can see the results of all destinations and determine an appropriate value to return.
You can use a responseMap variable. In the response transformer of each destination place the value you want to use as the response into the responseMap. Use the same key for all destinations. You will be able to select that variable as your response in the source tab of your channel. You may want to set a default response in your pre-processor or source transformer in case your destination has an error before setting the variable.

Related

How stop execution and send ack/nack with error message in mirth 3.4?

I'm new in mirth so sorry if my question may seems naive.
I've a mirth channel that recives hl7 messages, and this is fine, also I've some filters and transformers both in Source and Destination.
When all is fine at the end of destination I send an ACK with a message, for this for this purpose I've made this function in code Templates:
function getAck(success, detailMessage, statusMessage) {
if (!detailMessage)
detailMessage = success ? "Operation completed successfully" : "Some error occours";
if(!statusMessage)
statusMessage = detailMessage;
if (success) {
ack = ACKGenerator.generateAckResponse(connectorMessage.getRawData(), "AA", detailMessage);
resp = new Response(com.mirth.connect.userutil.Status.SENT, ack, statusMessage);
} else {
ack = ACKGenerator.generateAckResponse(connectorMessage.getRawData(), "AE", detailMessage);
resp = new Response(com.mirth.connect.userutil.Status.ERROR, ack, statusMessage, detailMessage);
}
return resp;
}
So I use ACKGenerator.generateAckResponse for creating an Ack and Response for send response at client. This work but only in destination and that's my problem.
If I get an error before destination (e.g. in filters, transformer, ...) I don't be able to stop execution and send an NACK with an explaination of the error and this is what I would like to do.
Am I wrong doing things in this way?
You can store a Response in the responseMap in any filters or transformers. Once you define a key in the responseMap, it should be available as a selection in the response drop down on the source tab of your channel (instead of picking a destination.)
Your current connector should stop processing a message with an ERROR status if you manually throw an exception after setting the desired value in the responseMap. If you are in a filter, you could also filter the message instead of throwing the exception.
If you are worried about an uncaught exception, you could initialize the responseMap variable with an "Unknown Error" message at the first point in the channel where your custom code is defined that affects messages directly (likely the source filter from your description, but could possibly be the pre-processor or attachment handler if you use those.) The expectation is that this would be replaced with a more descriptive error or a success if the message makes it all the way through to the end, but the channel will always have something to return.
There are filter and transformer in the "Source" tab. If your expecting an error there or on other destinations, you could try:
Adding a try-catch code block in your filter and transformer.
Use your custom code template function in your filter and transformer to catch the error or issue.
Create a separate channel that will receive the ACK/NACK which will be responsible in forwarding that message to the client.
In your try-catch code block or custom code template, use the method router.routeMessageByChannelId to forward the ACK/NACK to the other channel (step 3).
Note: You'll need to disable the default response in your original channel since you have the other channel that'll forward the ACK/NACK. You'll also need to consider if the client's machine expects a valid ACK/NACK immediately when they sent the HL7 message, depends on their setup.

Trying to do some assertion from request, which will be present in response

I am passing the request as in my feature file and i am trying to do assert from request to response.
I have tried must contains queries but i am not sure if i am doing it correct, could you please help.
**Background:**
* configure headers = read('classpath:merchantServiceheaders.js')
Given url MservUrl
And path '/spapis/rest/sp-ms-engine/sp/ms/v1/engine/scanandredact'
Scenario Outline: ACH Low Value Payment Rips Services Summary
]
}***
What i would like to do is assert what i have in my request to what i will get back in response.
Since i am passing subject in request the same subject should be present in response
Possible in 0.9.3: https://github.com/intuit/karate#scenario-outline-enhancements
First change the Examples: column header to data!
And request data
When method post
Then status 200
And match response contains data
In 0.9.2 and earlier, with the Examples: column header as data
* def data = <data>
And request data
When method post
Then status 200
And match response contains data

Best Practice - API successful response message

My current project API using this response on success:
if it's create -> it will send created object (201)
if it's update -> it will send updated object (200)
if it's delete -> it will send 204 response
Most of recommendation for example http://jsonapi.org/ never said about successful message to shown to user. I've followed their rules to keep returning object or 204 on success
What if I want to show to user alert "Successfully created" or "Successfully updated"?. Should the message come directly from server or client side (type it down or use some locales)?
You should review the RFC 7231, and it's description of 200 OK
The payload sent in a 200 response depends on the request method. For the methods defined by this specification, the intended meaning of the payload can be summarized as:
POST a representation of the status of, or results obtained from, the action;
PUT, DELETE a representation of the status of the action;
So yes, using the response body to present a representation of an alert to the user is fine.
For 201 Created, the same basic rule applies
The 201 response payload typically describes and links to the resource(s) created.
204 No Content is not similar, for the simple reason that "No Content" refers to the message body; 204 is one of the ways that you can indicate to the client (and intermediary components) that the 0 byte payload is not an error.
The 204 response allows a server to indicate that the action has been successfully applied to the target resource, while implying that the user agent does not need to traverse away from its current "document view" (if any). The server assumes that the user agent will provide some indication of the success to its user, in accord with its own interface, and apply any new or updated metadata in the response to its active representation.

How handle correctly HTTP Accept header when is either empty or null?

I am working with Spring Framework about Rest.
I have the scenario about testing when the request is a GET and Accept is either empty or null.
It is closely related with:
#GetMapping is called even when Accept is defined how null
I have read the following:
How to interpret empty HTTP Accept header?
What does 'Accept: /' mean under Client section of Request Headers?
Seems the following options how solutions are valid:
400 Bad Request
406 Not Acceptable
Consider the empty or null value of Accept how */* instead
Three options, that's the reason of this post.
Between the two first, I consider more appropriate 406 Not Acceptable, it because it is specific or tight for Accept. I mean what media type represents empty or null? it is not a known media type such as xml or json
The third option seems illogic, according with the following points (according my own points of view, perhaps I am wrong or missing something):
The server has a handler method for GET that returns either XML or json, such as:
#GetMapping(path="/{id}",
produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
public #ResponseBody Persona findOneById(#PathVariable String id){
return personaService.findOneById(id);
}
The client should have clear from the beginning that it would do the request and handle the response either XML or json, according the Accept sent through the request.
Thus, for any reason if the request sent has the Accept how empty (lets assume wrong coding), It is the third point (*/*) that I can't understand: "let the server return any media type"; even with that, lets assume the server returns XML and the client just can handle JSON instead, but of course that Accept how Json wasn't specified from the beginning. So the client once received the response should receive an exception, but observe that the server did the work unnecessarily
Thus: How handle correctly HTTP Accept header when empty or null?

Mirth Connect - get sent from destination

I have 2 destinations in my channel. One is an http sender, the 2nd gets the response from the http sender but I would also like to pull what was sent from that destination also. Can't seem to find the command if one exists to grab that.
I understand your question like this:
You want to know what was sent from the second destination?
My guess is like you can fetch the output of transformer on the destination end.
Inside tranformer i used connectorMessage.getEncodedData() or on the destination we can use the channel map variable ${message.encodedData} both of them works in the same way it pulls what comes out of your destination.
You can try same connectorMessage functionality in the destination transformer or on destination writer to pull the output of what comes from the destination. Hope it works.