1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.client.samples.adt;
15
16 import java.io.IOException;
17 import java.util.UUID;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.servlet.http.HttpSession;
24
25 import org.apache.commons.lang.StringEscapeUtils;
26
27 import uk.nhs.interoperability.infrastructure.ITKAddress;
28 import uk.nhs.interoperability.infrastructure.ITKAddressImpl;
29 import uk.nhs.interoperability.infrastructure.ITKIdentity;
30 import uk.nhs.interoperability.infrastructure.ITKIdentityImpl;
31 import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
32 import uk.nhs.interoperability.infrastructure.ITKMessagePropertiesImpl;
33 import uk.nhs.interoperability.infrastructure.ITKMessagingException;
34 import uk.nhs.interoperability.payload.ITKMessage;
35 import uk.nhs.interoperability.payload.SimpleMessage;
36 import uk.nhs.interoperability.source.ITKMessageSender;
37 import uk.nhs.interoperability.source.ITKMessageSenderImpl;
38 import uk.nhs.interoperability.util.Logger;
39
40
41
42
43
44
45
46
47 public class ADTServlet extends HttpServlet {
48
49
50 private static final long serialVersionUID = 1L;
51
52
53 private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
54
55
56 private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
57
58
59 private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:TESTORGS:ORGA";
60
61
62 private static final String SERVICE_PREFIX = "urn:nhs-itk:services:201005:";
63
64
65 private static final String ADT_PROFILEID = "ITKv1.0";
66
67
68
69
70 public ADTServlet() {
71 super();
72 }
73
74
75
76
77 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
78 processRequest(request, response);
79 }
80
81
82
83
84 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85 processRequest(request, response);
86 }
87
88
89
90
91
92
93
94
95
96 private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
97
98 System.out.println("this is ADTServlet.processRequest");
99 String docText = request.getParameter("docText");
100 String serviceId = request.getParameter("serviceId");
101 String serviceAddress = request.getParameter("serviceAddress");
102 String responseType = request.getParameter("responseType");
103 ITKAddress itkServiceAddress = new ITKAddressImpl(serviceAddress);
104
105 HttpSession session = request.getSession(true);
106 session.removeAttribute("errorMessage");
107 session.removeAttribute("outcomeMessage");
108 session.removeAttribute("responseMessage");
109
110
111 ITKMessage msg = new SimpleMessage();
112
113 msg.setBusinessPayload(docText);
114
115 UUID messageUUID = UUID.randomUUID();
116 String messageId = messageUUID.toString();
117
118
119 ITKMessageProperties mp = new ITKMessagePropertiesImpl();
120 mp.setAuditIdentity(AUDITID);
121
122
123
124
125
126 mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
127 mp.setToAddress(itkServiceAddress);
128
129
130 mp.setServiceId(SERVICE_PREFIX+serviceId);
131 mp.setBusinessPayloadId(messageId);
132
133
134 mp.setProfileId(ADT_PROFILEID);
135
136
137 msg.setMessageProperties(mp);
138
139 String responseMsgTxt = "";
140
141 ITKMessageSender sender = new ITKMessageSenderImpl();
142
143 try {
144
145 if (responseType.equalsIgnoreCase("SYNC")){
146
147
148 ITKMessage responseMsg = sender.sendSync(msg);
149 responseMsgTxt = responseMsg.getBusinessPayload();
150 responseMsgTxt = StringEscapeUtils.escapeHtml(responseMsgTxt);
151 session.setAttribute("responseMessage", responseMsgTxt);
152 session.setAttribute("outcomeMessage", "ADT Message Sent Successfully.");
153 } else {
154
155 sender.sendAsync(msg);
156 session.setAttribute("outcomeMessage", "ADT Message Sent Asynchronously.");
157
158 }
159
160 Logger.trace(responseMsgTxt);
161 } catch (ITKMessagingException e) {
162 Logger.error("Error Sending ITK Message",e);
163 session.setAttribute("errorMessage", "Sorry this hasn't worked out this time. Please try again later.");
164 }
165 Logger.trace(responseMsgTxt);
166 response.sendRedirect("./ADTResponse.jsp");
167
168 Logger.info("*** ADTServlet: Ending.");
169 }
170
171 }