1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.consumer;
15
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServlet;
18
19 import uk.nhs.interoperability.capabilities.ITKProfileManager;
20 import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
21 import uk.nhs.interoperability.infrastructure.ITKMessagingException;
22 import uk.nhs.interoperability.payload.DEWrappedMessage;
23 import uk.nhs.interoperability.payload.ITKMessage;
24 import uk.nhs.interoperability.payload.ITKSimpleMessageResponse;
25 import uk.nhs.interoperability.service.ITKService;
26 import uk.nhs.interoperability.service.ITKSimpleDOS;
27 import uk.nhs.interoperability.transport.ITKInfrastructureAck;
28 import uk.nhs.interoperability.util.ITKApplicationProperties;
29 import uk.nhs.interoperability.util.Logger;
30 import uk.nhs.interoperability.util.MessageQueue;
31
32
33
34
35
36
37
38
39 @SuppressWarnings("serial")
40 public abstract class ITKServlet extends HttpServlet {
41
42
43 private ITKProfileManager profileManger;
44
45
46 private MessageQueue messageQueue;
47
48
49 private String auditIdentity;
50
51
52 protected ITKSimpleDOS dos;
53
54
55
56
57 @Override
58 public void init() throws ServletException {
59 try {
60 this.dos = new ITKSimpleDOS();
61 this.messageQueue = new MessageQueue();
62 this.profileManger = new ITKProfileManagerImpl();
63
64 this.auditIdentity = ITKApplicationProperties.getProperty("audit.identity");
65 } catch (Throwable t) {
66 throw new ServletException("Could not instantiate ITKMessageConsumer instance", t);
67 }
68 super.init();
69 }
70
71
72
73
74
75
76
77
78 protected void sendInfrastructureAck(ITKMessageProperties requestMessageProperties) throws ITKMessagingException {
79 Logger.debug("Creating and sending an infrastructureAck");
80 ITKMessage ack = new ITKInfrastructureAck(requestMessageProperties, this.auditIdentity);
81
82 this.messageQueue.queue(ack);
83 }
84
85
86
87
88
89
90
91
92
93 protected void checkProfileId(ITKMessageProperties itkMessageProperties) throws ITKMessagingException {
94 if (itkMessageProperties != null && itkMessageProperties.getProfileId() != null) {
95 String profileId = itkMessageProperties.getProfileId();
96 int profileSupportLevel = this.profileManger.getProfileSupportLevel(profileId);
97 if (profileSupportLevel == ITKProfileManager.DEPRECATED) {
98 Logger.warn("Profile \"" + profileId + "\" is deprecated - it will be processed for now but sender should be informed that support may be withdrawn in the near future");
99 } else if (profileSupportLevel != ITKProfileManager.ACCEPTED) {
100 Logger.warn("Profile \"" + profileId + "\" is not accepted - message will be rejected");
101 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.INVALID_MESSAGE_CODE, "Profile \"" + profileId + "\" is not supported - rejecting message " + itkMessageProperties.getBusinessPayloadId());
102 }
103
104 return;
105 }
106 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.INVALID_MESSAGE_CODE, "Request message profileId could not be determined - rejecting message");
107 }
108
109
110
111
112
113
114
115 protected void checkServiceId(ITKMessageProperties itkMessageProperties) throws ITKMessagingException {
116 String serviceId = itkMessageProperties.getServiceId();
117 ITKService service = dos.getService(serviceId);
118 if (service==null){
119 Logger.warn("Service \"" + serviceId + "\" is not accepted - message will be rejected");
120 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE,
121 "Service \"" + serviceId + "\" is not supported - rejecting message " + itkMessageProperties.getBusinessPayloadId());
122 }
123 }
124
125
126
127
128
129
130
131
132 protected void validateDistributionEnvelope(ITKMessageProperties itkMessageProperties)
133 throws ITKMessagingException {
134
135
136 this.checkProfileId(itkMessageProperties);
137
138
139 this.checkServiceId(itkMessageProperties);
140
141
142
143
144 if (itkMessageProperties.getAuditIdentity().getURI().length()==0) {
145 Logger.warn("Audit Identity not populated - message will be rejected");
146 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.INVALID_MESSAGE_CODE,
147 "Audit Identity not populated - message will be rejected" );
148 }
149
150 if (itkMessageProperties.getAuditIdentity().getURI().contains("INVALID")) {
151 Logger.warn("Audit Identity not populated - message will be rejected");
152 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.INVALID_MESSAGE_CODE,
153 "Audit Identity not populated - message will be rejected" );
154 }
155
156
157 if (itkMessageProperties.getBusinessPayloadId().length()==0) {
158 Logger.warn("Business Payload Id not populated - message will be rejected");
159 throw new ITKMessagingException(itkMessageProperties, ITKMessagingException.INVALID_MESSAGE_CODE,
160 "Business Payload Id not populated - message will be rejected" );
161
162 }
163 }
164
165
166
167
168
169
170
171
172
173 protected ITKMessage addITKWrappers(ITKMessage itkMessage) throws ITKMessagingException {
174
175 ITKMessage wrappedMessage = itkMessage ;
176
177
178
179
180
181
182 if (itkMessage != null && !(itkMessage instanceof ITKSimpleMessageResponse)) {
183
184
185
186
187 ITKService service = dos.getService(itkMessage.getMessageProperties().getServiceId());
188 if (service==null){
189 Logger.warn("Response Service not configured - message will be rejected");
190 throw new ITKMessagingException(ITKMessagingException.PROCESSING_ERROR_NOT_RETRYABLE_CODE,
191 "Response Service not configured - message will be rejected" );
192 }
193
194
195
196
197
198
199
200 Logger.trace("Adding distribution envelope wrapper");
201 if(service.isBase64()){
202 String b64DocText = javax.xml.bind.DatatypeConverter
203 .printBase64Binary(itkMessage.getBusinessPayload().getBytes());
204 itkMessage.setBusinessPayload(b64DocText);
205 }
206 wrappedMessage = new DEWrappedMessage(service, itkMessage, true);
207
208
209
210
211 }
212 return wrappedMessage;
213 }
214
215 }