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.payload;
15  
16  import java.util.Map;
17  import java.util.UUID;
18  
19  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
20  import uk.nhs.interoperability.service.ITKService;
21  import uk.nhs.interoperability.transform.TransformManager;
22  import uk.nhs.interoperability.util.Logger;
23  
24  import com.xmlsolutions.annotation.Requirement;
25  
26  /**
27   * The Class DEWrappedMessage.
28   *
29   * @author Michael Odling-Smee
30   * @author Nicholas Jones
31   * @since 0.1
32   */
33  public class DEWrappedMessage extends ITKMessageImpl {
34  	
35  	/**
36  	 * Constant to indicate that the on the wire Distribution
37  	 * envelope XML contains logical originating and destination
38  	 * addresses
39  	 */
40  	public static final boolean DE_ADDRESSED = true;
41  	
42  	/**
43  	 * Constant to indicate that the on-the-wire Distribution
44  	 * envelope XML omits logical addressing (for instance
45  	 * this is not required in the case of synchronous calls)
46  	 */
47  	public static final boolean DE_UNADDRESSED = false;
48  	
49  	/** The tracking id. */
50  	private String trackingId;
51  	
52  	/**
53  	 * Whether or not the serialised XML Distribution Envelope contains logical addressing.
54  	 * Note this is on strictly required for a routed message 
55  	 * */
56  	private boolean addressed;
57  	
58  	/** The is base64. */
59  	private boolean isBase64;
60  	
61  	/** The mime type. */
62  	private String mimeType;
63  	
64  	/**
65  	 * Instantiates a new dE wrapped message.
66  	 */
67  	@Requirement(traceTo="COR-DEH-02", status="implemented")
68  	public DEWrappedMessage() {
69  		//Note UUID must be in upper case with no prefix
70  		this.trackingId = UUID.randomUUID().toString().toUpperCase();
71  	}
72  	
73  	/**
74  	 * Instantiates a new dE wrapped message.
75  	 *
76  	 * @param message the message
77  	 */
78  	public DEWrappedMessage(ITKMessage message){
79  		this();
80  		this.setBusinessPayload(message.getBusinessPayload());
81  		this.messageProperties = message.getMessageProperties();
82  		this.addressed = true;
83  	}
84  	
85  	/**
86  	 * Instantiates a new dE wrapped message.
87  	 *
88  	 * @param service the service
89  	 * @param message the message
90  	 * @param addressed the addressed
91  	 */
92  	public DEWrappedMessage(ITKService service, ITKMessage message, boolean addressed){
93  		this();
94  		this.setBusinessPayload(message.getBusinessPayload());
95  		this.messageProperties = message.getMessageProperties();
96  		this.addressed = addressed;
97  		this.isBase64 = service.isBase64();
98  		this.mimeType = service.getMimeType();
99  	}
100 	
101 	/* (non-Javadoc)
102 	 * @see uk.nhs.interoperability.payload.ITKMessage#getFullPayload()
103 	 */
104 	@Requirement(traceTo={"COR-DEH-01", "COR-DEH-04"}, status="implemented")
105 	public String getFullPayload() throws ITKMessagingException {
106 		String fullPayload = "";
107 		
108 		// TODO: Create a DistributionEnvelope object with a serialize method
109 		//       to do this job.
110 		String deXML = "<ITKMessage>";
111 		deXML += "<Service>"+this.messageProperties.getServiceId()+"</Service>";
112 		deXML += "<TrackingId>" + this.trackingId + "</TrackingId>";
113 		deXML += "<Sender>"+this.messageProperties.getFromAddress().getURI()+"</Sender>";
114 		deXML += "<SenderType>"+this.messageProperties.getFromAddress().getType()+"</SenderType>";
115 		deXML += "<Recipient>"+this.messageProperties.getToAddress().getURI()+"</Recipient>";
116 		deXML += "<RecipientType>"+this.messageProperties.getToAddress().getType()+"</RecipientType>";
117 		deXML += "<Author>"+this.messageProperties.getAuditIdentity().getURI()+"</Author>";
118 		deXML += "<AuthorType>"+this.messageProperties.getAuditIdentity().getType()+"</AuthorType>";
119 		deXML += "<Manifest id=\""+this.messageProperties.getBusinessPayloadId()+
120 					"\" type=\""+this.mimeType+"\""+
121 				    " profileid=\""+this.messageProperties.getProfileId()+"\" ";
122 		if (this.isBase64){
123 			deXML += " base64=\"true\" ";
124 		}
125 		deXML += " />";
126 		//Add handling specifications if set
127 		if (!this.getMessageProperties().getHandlingSpecifications().isEmpty()) {
128 			deXML += "<HandlingSpecs>";
129 			for (Map.Entry<String, String> entry: this.getMessageProperties().getHandlingSpecifications().entrySet()) {
130 				//Add handling specifications
131 				deXML += "<Spec key=\"" + entry.getKey() + "\" value=\"" + entry.getValue() + "\"/>";
132 			}
133 			deXML += "</HandlingSpecs>";
134 		}
135 		deXML += "<Payload id=\""+this.messageProperties.getBusinessPayloadId()+
136 				"\">"+this.getBusinessPayload()+"</Payload>";
137 		if (this.addressed){
138 			deXML += "<Addressed/>";
139 		}
140 		deXML += "</ITKMessage>";
141 		Logger.trace("DEXML:"+deXML);
142 		fullPayload = TransformManager.doTransform("ToDistributionEnvelope.xslt", deXML);
143 
144 		return fullPayload;
145 		
146 	}
147 	
148 	/* (non-Javadoc)
149 	 * @see uk.nhs.interoperability.payload.ITKMessageImpl#setBusinessPayload(java.lang.String)
150 	 */
151 	public void setBusinessPayload(String businessPayload) {
152 		if (businessPayload != null) {
153 			if (businessPayload.startsWith("<?xml")) {
154 				//Remove any XML declarations
155 				this.businessPayload = businessPayload.substring(businessPayload.indexOf(">")+1);
156 			} else {
157 				this.businessPayload = businessPayload;
158 			}
159 			
160 		}
161 	}
162 }