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.cda;
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 HelloWorldCDASourceServlet.
39   *
40   * @author Michael Odling-Smee
41   * @author Nicholas Jones
42   * @since 0.1
43   */
44  public class HelloWorldCDASourceServlet extends HttpServlet {
45  	
46  	/** The Constant serialVersionUID. */
47  	private static final long serialVersionUID = 1L;
48  	
49  	/** The Constant ORGID. */
50  	private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
51  
52  	/** The Constant AUDITID. */
53  	private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
54  	
55  	/** The Constant FROMADDRESS. */
56  	private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:R59:oncology";
57  
58  	/** The Constant NON_CODED_CDA. */
59  	private static final String NON_CODED_CDA = "urn:nhs-itk:services:201005:SendCDADocument-v2-0";
60  	
61  	/** The Constant NCDA_PROFILEID. */
62  	private static final String NCDA_PROFILEID = "urn:nhs-en:profile:nonCodedCDADocument-v2-0";
63         
64      /**
65       * Instantiates a new hello world cda source servlet.
66       */
67      public HelloWorldCDASourceServlet() {
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  		System.out.println("this is HelloWorldCDASource.processRequest");
96  		String docId = request.getParameter("docId");
97  		String docText = request.getParameter("docText");
98  		String nhsNumber = request.getParameter("nhsNumber");
99  		String dateOfBirth = request.getParameter("dateOfBirth");
100 		String businessAckRequired = request.getParameter("businessAckRequired");
101 		String serviceAddress = request.getParameter("serviceAddress");
102 		ITKAddress itkServiceAddress = new ITKAddressImpl(serviceAddress);
103 
104 		HttpSession session = request.getSession(true);
105         session.removeAttribute("errorMessage");
106         session.removeAttribute("outcomeMessage");
107         session.removeAttribute("responseMessage");
108 		
109 		NonCodedCDA doc = new NonCodedCDA();
110 		doc.messageId = docId;
111 		doc.nhsNumber = nhsNumber;
112 		doc.dateOfBirth = dateOfBirth;
113 		doc.presentationText = docText;
114 		
115 		// Create the message
116 		ITKMessage msg = new SimpleMessage();
117 		msg.setBusinessPayload(doc.serialise());
118 
119 		// Build the message properties.
120 		ITKMessageProperties mp = new ITKMessagePropertiesImpl();
121 		mp.setAuditIdentity(AUDITID);
122 		mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
123 		mp.setToAddress(itkServiceAddress);
124 		mp.setServiceId(NON_CODED_CDA);
125 		mp.setBusinessPayloadId(doc.getMessageId());
126 		mp.setProfileId(NCDA_PROFILEID);
127 		
128 		if (businessAckRequired.equalsIgnoreCase("Y")){
129 			mp.addHandlingSpecification(ITKMessageProperties.BUSINESS_ACK_HANDLING_SPECIFICATION_KEY, "true");
130 		}
131 
132 		// Add the properties to the message
133 		msg.setMessageProperties(mp);
134 		
135 		// Create the sender
136 		ITKMessageSender sender = new ITKMessageSenderImpl();
137 
138 		try {
139 			// Send this message asynchronously. The response message will be returned
140 			sender.sendAsync(msg);
141 	        
142 			Logger.info("CDA Message Sent.");
143 			session.setAttribute("outcomeMessage", "CDA Message Sent.");
144 			
145 		} catch (ITKMessagingException e) {
146 			Logger.error("Error Sending ITK Message",e);
147 	        session.setAttribute("errorMessage", "Sorry - I couldn't send the message this time.");
148 		}
149 
150 		response.sendRedirect("./CDASent.jsp");
151 		Logger.trace("*** HelloWorldCDASource: Ending send");
152 	}
153 
154 }