View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.client.samples;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.util.Scanner;
19  import java.util.UUID;
20  
21  import uk.nhs.interoperability.infrastructure.ITKAddressImpl;
22  import uk.nhs.interoperability.infrastructure.ITKIdentity;
23  import uk.nhs.interoperability.infrastructure.ITKIdentityImpl;
24  import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
25  import uk.nhs.interoperability.infrastructure.ITKMessagePropertiesImpl;
26  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
27  import uk.nhs.interoperability.payload.ITKMessage;
28  import uk.nhs.interoperability.payload.SimpleMessage;
29  import uk.nhs.interoperability.source.ITKMessageSender;
30  import uk.nhs.interoperability.source.ITKMessageSenderImpl;
31  import uk.nhs.interoperability.util.Logger;
32  
33  /**
34   * The Class BusAckSender.
35   *
36   * @author Michael Odling-Smee
37   * @author Nicholas Jones
38   * @since 0.1
39   */
40  public class BusAckSender {
41  
42  	/** The Constant CALLBACK. */
43  	private static final String CALLBACK = "urn:nhs-uk:addressing:ods:Y88764:CALLBACK";
44  
45  	/** The Constant ORGID. */
46  	private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
47  
48  	/** The Constant AUDITID. */
49  	private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
50  	
51  	/** The Constant FROMADDRESS. */
52  	private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:R59:oncology";
53  
54  	/** The Constant BUS_ACK_SERVICE. */
55  	private static final String BUS_ACK_SERVICE = "urn:nhs-itk:services:201005:SendBusinessAck-v1-0";
56  	
57  	/** The Constant BUS_ACK_PROFILEID. */
58  	private static final String BUS_ACK_PROFILEID = "urn:nhs-itk:profile:201005:SendBusinessAck-v1-0";
59  
60  	/**
61  	 * The main method.
62  	 *
63  	 * @param args the arguments
64  	 */
65  	public static void main(String[] args) {
66  
67  		BusAckSender appA = new BusAckSender();
68  		appA.sendAsyncMessage();
69  	
70  	}
71  
72  	/**
73  	 * Send async message.
74  	 */
75  	private void sendAsyncMessage(){
76  		Logger.trace("*** BusAckSender: Starting sendSyncMessage");
77  		
78  		// Create the message
79  		ITKMessage msg = new SimpleMessage();
80  
81  		// populate the payload
82  		try {
83  			msg.setBusinessPayload(readFile("BusinessAck.xml"));
84  		} catch (IOException e1) {
85  			Logger.error("Error Loading TEST BUS ACK Message",e1);
86  			return;
87  		}
88  		
89  		UUID messageUUID = UUID.randomUUID();
90  		String messageId = messageUUID.toString();
91  		
92  		// Build the message properties.
93  		ITKMessageProperties mp = new ITKMessagePropertiesImpl();
94  		mp.setAuditIdentity(AUDITID);
95  		mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
96  		mp.setToAddress(new ITKAddressImpl(CALLBACK));
97  		mp.setServiceId(BUS_ACK_SERVICE);
98  		mp.setBusinessPayloadId(messageId);
99  		mp.setProfileId(BUS_ACK_PROFILEID);
100 
101 		// Add the properties to the message
102 		msg.setMessageProperties(mp);
103 		
104 		// Create the sender
105 		ITKMessageSender sender = new ITKMessageSenderImpl();
106 
107 		try {
108 			// Send this message synchronously. The response message will be returned
109 			sender.sendAsync(msg);
110 		
111 			Logger.trace("BusAckSender sent message:");
112 			
113 		} catch (ITKMessagingException e) {
114 			Logger.error("Error Sending ITK Message",e);
115 		}
116 		
117 		Logger.trace("*** BusAckSender: Ending sendAsyncMessage");
118 	}
119 
120 	/**
121 	 * Read file.
122 	 *
123 	 * @param fname the fname
124 	 * @return the string
125 	 * @throws IOException Signals that an I/O exception has occurred.
126 	 */
127 	private String readFile(String fname) throws IOException {
128 
129 	    InputStream tis = this.getClass().getResourceAsStream("/messages/"+fname);
130 	    StringBuilder fileContents = new StringBuilder();
131 	    Scanner scanner = new Scanner(tis);
132 	    String lineSeparator = System.getProperty("line.separator");
133 
134 	    try {
135 	        while(scanner.hasNextLine()) {        
136 	            fileContents.append(scanner.nextLine() + lineSeparator);
137 	        }
138 	        return fileContents.toString();
139 	    } finally {
140 	        scanner.close();
141 	    }
142 	}
143 
144 }