web stats
Add attachment to routeMessage - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-04-2019, 06:14 AM
V3nn3tj3 V3nn3tj3 is offline
Mirth Newb
 
Join Date: Jul 2018
Posts: 11
V3nn3tj3 is on a distinguished road
Default Add attachment to routeMessage

Hi,

I'm trying to attach an attachment to the routeMessage function. This to route a message with attachment to another channel.
The reason i can't pick up the attachment from the channel itself is because i remove it once the message has been processed. I do keep the original message and it's attachment in memory untill the next message has come through in a globalchannelmap.

Any idea how to do this? I cannot store any attachments due to storage limits, etc.

Thanks,
Nick
Reply With Quote
  #2  
Old 10-10-2019, 05:45 AM
V3nn3tj3 V3nn3tj3 is offline
Mirth Newb
 
Join Date: Jul 2018
Posts: 11
V3nn3tj3 is on a distinguished road
Default

Found a solution,
I've build my own extension on Mirth as following:

Code:
package com.mirth.connect.dicom;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.mirth.connect.donkey.server.channel.DispatchResult;
import com.mirth.connect.server.controllers.ChannelController;
import com.mirth.connect.server.controllers.ControllerFactory;
import com.mirth.connect.server.controllers.EngineController;
import com.mirth.connect.userutil.Response;
import com.mirth.connect.userutil.Status;
import com.mirth.connect.util.ErrorMessageBuilder;
import com.mirth.connect.server.userutil.VMRouter;
import com.mirth.connect.server.userutil.RawMessage;
import com.mirth.connect.server.userutil.Attachment;

public class VMRouterExtended extends VMRouter {
	private Logger logger = Logger.getLogger(getClass());
    private ChannelController channelController = ControllerFactory.getFactory().createChannelController();
    private EngineController engineController = ControllerFactory.getFactory().createEngineController();
	
    public Response routeMessage(String channelName, String message, List<Attachment> attachments) {
        com.mirth.connect.model.Channel channel = channelController.getDeployedChannelByName(channelName);

        if (channel == null) {
            logger.error("Could not find channel to route to for channel name: " + channelName);
            return new Response(Status.ERROR, "Could not find channel to route to for channel name: " + channelName);
        }
        
        return routeMessageByChannelId(channel.getId(), new RawMessage(message), attachments);
    }
    
	public Response routeMessageByChannelId(String channelId, RawMessage rawMessage, List<Attachment> attachments) {
        try {
            DispatchResult dispatchResult = this.engineController.dispatchRawMessage(channelId, this.convertRawMessage(rawMessage, attachments), false, true);

            Response response = null;
            if (dispatchResult != null && dispatchResult.getSelectedResponse() != null) {
                response = new Response(dispatchResult.getSelectedResponse());
            }

            return response;
        } catch (Throwable e) {
            String message = "Error routing message to channel id: " + channelId;
            logger.error(message, e);
            String responseStatusMessage = ErrorMessageBuilder.buildErrorResponse(message, e);
            String responseError = ErrorMessageBuilder.buildErrorMessage(this.getClass().getSimpleName(), message, e);
            return new Response(Status.ERROR, null, responseStatusMessage, responseError);
        }   
    }
	
    private com.mirth.connect.donkey.model.message.RawMessage convertRawMessage(RawMessage message, List<Attachment> attachments) {
        if (message.isBinary()) {
            return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawBytes(), message.getDestinationMetaDataIds(), message.getSourceMap(), convertAttachments(attachments));
        } else {
            return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawData(), message.getDestinationMetaDataIds(), message.getSourceMap(), convertAttachments(attachments));
        }
    }
    
    private List<com.mirth.connect.donkey.model.message.attachment.Attachment> convertAttachments(List<Attachment> attachments) {
    	List<com.mirth.connect.donkey.model.message.attachment.Attachment> attachmentscnvrt = new ArrayList<com.mirth.connect.donkey.model.message.attachment.Attachment>();
        for(Attachment attachment : attachments){
        	attachmentscnvrt.add(new com.mirth.connect.donkey.model.message.attachment.Attachment(attachment.getId(), attachment.getContent(), attachment.getType()));
        }
        return attachmentscnvrt;
    }
}
Usage:

Code:
var routerEx = com.mirth.connect.dicom.VMRouterExtended();
routerEx.routeMessage("ChannelName", "Message", "Attachment");

message = connectorMessage.getRawData();
attachment = getAttachments();
Works like a charm .

Added the jar as attachment.

Nick
Attached Files
File Type: zip com.mirth.connect.dicom.zip (2.7 KB, 2 views)

Last edited by V3nn3tj3; 10-10-2019 at 05:46 AM. Reason: Attachment was lost in the post...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 11:33 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation