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.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   * The Class ADTServlet.
42   *
43   * @author Michael Odling-Smee
44   * @author Nicholas Jones
45   * @since 0.1
46   */
47  public class ADTServlet extends HttpServlet {
48  	
49  	/** The Constant serialVersionUID. */
50  	private static final long serialVersionUID = 1L;
51  	
52  	/** The Constant ORGID. */
53  	private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
54  
55  	/** The Constant AUDITID. */
56  	private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
57  	
58  	/** The Constant FROMADDRESS. */
59  	private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:TESTORGS:ORGA";
60  
61  	/** The Constant SERVICE_PREFIX. */
62  	private static final String SERVICE_PREFIX = "urn:nhs-itk:services:201005:";
63  	
64  	/** The Constant ADT_PROFILEID. */
65  	private static final String ADT_PROFILEID = "ITKv1.0";
66  	       
67      /**
68       * Instantiates a new aDT servlet.
69       */
70      public ADTServlet() {
71          super();
72      }
73  
74  	/* (non-Javadoc)
75  	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
76  	 */
77  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
78  		processRequest(request, response);
79  	}
80  
81  	/* (non-Javadoc)
82  	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
83  	 */
84  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
85  		processRequest(request, response);
86  	}
87  	
88  	/**
89  	 * Process request.
90  	 *
91  	 * @param request the request
92  	 * @param response the response
93  	 * @throws ServletException the servlet exception
94  	 * @throws IOException Signals that an I/O exception has occurred.
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 		// Create the message
111 		ITKMessage msg = new SimpleMessage();
112 
113 		msg.setBusinessPayload(docText);
114 		
115 		UUID messageUUID = UUID.randomUUID();
116 		String messageId = messageUUID.toString();
117 
118 		// Build the message properties.
119 		ITKMessageProperties mp = new ITKMessagePropertiesImpl();
120 		mp.setAuditIdentity(AUDITID);
121 		
122 		// Example of sending another audit type
123 		//ITKIdentity ai = new ITKIdentityImpl("ANOTHER.AUDIT.ID","2.16.840.1.113883.0.0.1");
124 		//mp.setAuditIdentity(ai);
125 
126 		mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
127 		mp.setToAddress(itkServiceAddress);
128 		
129 		// Service is as requested on the web page
130 		mp.setServiceId(SERVICE_PREFIX+serviceId);
131 		mp.setBusinessPayloadId(messageId);
132 
133 		// Profile Id is fixed (at present) for ADT
134 		mp.setProfileId(ADT_PROFILEID);
135 
136 		// Add the properties to the message
137 		msg.setMessageProperties(mp);
138 		
139 		String responseMsgTxt = "";
140 		// Create the sender
141 		ITKMessageSender sender = new ITKMessageSenderImpl();
142 
143 		try {
144 			
145 			if (responseType.equalsIgnoreCase("SYNC")){
146 				
147 				// Send this message synchronously. The response message will be returned
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 				// Send this message synchronously. The response message will be returned
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 }