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.notification;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpSession;
23  
24  import uk.nhs.interoperability.infrastructure.ITKAddress;
25  import uk.nhs.interoperability.infrastructure.ITKAddressImpl;
26  import uk.nhs.interoperability.infrastructure.ITKIdentity;
27  import uk.nhs.interoperability.infrastructure.ITKIdentityImpl;
28  import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
29  import uk.nhs.interoperability.infrastructure.ITKMessagePropertiesImpl;
30  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
31  import uk.nhs.interoperability.payload.ITKMessage;
32  import uk.nhs.interoperability.payload.SimpleMessage;
33  import uk.nhs.interoperability.source.ITKMessageSender;
34  import uk.nhs.interoperability.source.ITKMessageSenderImpl;
35  import uk.nhs.interoperability.util.Logger;
36  
37  /**
38   * The Class NotificationSenderServlet.
39   *
40   * @author Adam Hatherly
41   * @since 0.1
42   */
43  public class NotificationSenderServlet extends HttpServlet {
44  	
45  	/** The Constant serialVersionUID. */
46  	private static final long serialVersionUID = 1L;
47  	
48  	/** The Constant ORGID. */
49  	private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
50  
51  	/** The Constant AUDITID. */
52  	private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
53  	
54  	/** The Constant FROMADDRESS. */
55  	private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:R59:oncology";
56  
57  	/** The Constant NOTIFICATION_SERVICENAME. */
58  	private static final String NOTIFICATION_SERVICENAME = "urn:nhs-itk:services:201005:SendEventNotification-v1-0";
59  	
60  	/** The Constant NOTIFICATION_PROFILEID. */
61  	private static final String NOTIFICATION_PROFILEID = "urn:nhs-en:profile:EventNotification-v1-0";
62  
63         
64      /**
65       * Instantiates a new notification sender servlet.
66       */
67      public NotificationSenderServlet() {
68          super();
69      }
70  
71  	/* (non-Javadoc)
72  	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
73  	 */
74  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
75  		processRequest(request, response);
76  	}
77  
78  	/* (non-Javadoc)
79  	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
80  	 */
81  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
82  		processRequest(request, response);
83  	}
84  	
85  	/**
86  	 * Process request.
87  	 *
88  	 * @param request the request
89  	 * @param response the response
90  	 * @throws ServletException the servlet exception
91  	 * @throws IOException Signals that an I/O exception has occurred.
92  	 */
93  	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
94  
95  		Logger.debug("this is NotificationSenderServlet.processRequest");
96  		String messageId = request.getParameter("messageId");
97  		String nhsNumber = request.getParameter("nhsNumber");
98  		String dateOfBirth = request.getParameter("dateOfBirth");
99  		
100 		String senderOrg = request.getParameter("senderOrg");
101 		String recipientOrg = request.getParameter("recipientOrg");
102 		String recipientAddress = request.getParameter("recipientAddress");
103 		String patientName = request.getParameter("patientName");
104 		String patientAddress = request.getParameter("patientAddress");
105 		String contactName = request.getParameter("contactName");
106 		String contactAddress = request.getParameter("contactAddress");
107 		String contactOrg = request.getParameter("contactOrg");
108 		
109 		String businessAckRequired = request.getParameter("businessAckRequired");
110 		String serviceAddress = request.getParameter("serviceAddress");
111 		ITKAddress itkServiceAddress = new ITKAddressImpl(serviceAddress);
112 
113 		HttpSession session = request.getSession(true);
114         session.removeAttribute("errorMessage");
115         session.removeAttribute("outcomeMessage");
116         session.removeAttribute("responseMessage");
117 		
118 		Notification n = new Notification();
119 		n.setMessageId(messageId);
120 		n.setNHSNumber(nhsNumber);
121 		n.setDateOfBirth(dateOfBirth);
122 		
123 		n.setEffectiveTime("20121127151500+0000");
124 		n.setSenderOrg(senderOrg);
125 		n.setRecipientOrg(recipientOrg);
126 		n.setRecipientAddress(recipientAddress);
127 		n.setPatientName(patientName);
128 		n.setPatientAddress(patientAddress);
129 		
130 		n.setEventTypeCode("01");
131 		n.setEventTypeDesc("DocumentEvent");
132 		
133 		n.setContactName(contactName);
134 		n.setContactAddress(contactAddress);
135 		n.setContactOrg(contactOrg);
136 		
137 		// Create the message
138 		ITKMessage msg = new SimpleMessage();
139 		msg.setBusinessPayload(n.serialise());
140 
141 		// Build the message properties.
142 		ITKMessageProperties mp = new ITKMessagePropertiesImpl();
143 		mp.setAuditIdentity(AUDITID);
144 		mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
145 		mp.setToAddress(itkServiceAddress);
146 		mp.setServiceId(NOTIFICATION_SERVICENAME);
147 		mp.setBusinessPayloadId(n.getMessageId());
148 		mp.setProfileId(NOTIFICATION_PROFILEID);
149 		
150 		if (businessAckRequired.equalsIgnoreCase("Y")){
151 			mp.addHandlingSpecification(ITKMessageProperties.BUSINESS_ACK_HANDLING_SPECIFICATION_KEY, "true");
152 		}
153 
154 		// Add the properties to the message
155 		msg.setMessageProperties(mp);
156 		
157 		// Create the sender
158 		ITKMessageSender sender = new ITKMessageSenderImpl();
159 
160 		try {
161 			// Send this message asynchronously. The response message will be returned
162 			sender.sendAsync(msg);
163 		
164 			Logger.info("Notification Message Sent.");
165 			session.setAttribute("outcomeMessage", "Notification Message Sent.");
166 			
167 		} catch (ITKMessagingException e) {
168 			Logger.error("Error Sending ITK Message",e);
169 	        session.setAttribute("errorMessage", "Sorry - I couldn't send the Notification this time.");
170 		}
171 
172 		response.sendRedirect("./NotificationSent.jsp");
173 		
174 		Logger.trace("*** NotificationSenderServlet: Ending send");
175 	}
176 
177 }