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.smsp;
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  import javax.xml.parsers.ParserConfigurationException;
24  
25  import org.apache.commons.lang.StringEscapeUtils;
26  import org.w3c.dom.Document;
27  import org.xml.sax.SAXException;
28  
29  import uk.nhs.interoperability.infrastructure.ITKAddress;
30  import uk.nhs.interoperability.infrastructure.ITKAddressImpl;
31  import uk.nhs.interoperability.util.Logger;
32  import uk.nhs.interoperability.util.xml.DomUtils;
33  
34  /**
35   * The Class GetNHSNumberServlet.
36   *
37   * @author Michael Odling-Smee
38   * @author Nicholas Jones
39   * @since 0.1
40   */
41  public class GetNHSNumberServlet extends HttpServlet {
42  	
43  	/** The Constant serialVersionUID. */
44  	private static final long serialVersionUID = 1L;
45         
46      /**
47       * Instantiates a new verify nhs number servlet.
48       */
49      public GetNHSNumberServlet() {
50          super();
51      }
52  
53  	/* (non-Javadoc)
54  	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
55  	 */
56  	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57  		processRequest(request, response);
58  	}
59  
60  	/* (non-Javadoc)
61  	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
62  	 */
63  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64  		processRequest(request, response);
65  	}
66  	
67  	/**
68  	 * Process request.
69  	 *
70  	 * @param request the request
71  	 * @param response the response
72  	 * @throws ServletException the servlet exception
73  	 * @throws IOException Signals that an I/O exception has occurred.
74  	 */
75  	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
76  
77  		System.out.println("this is GetNHSNumberServlet.processRequest");
78  		String surname = request.getParameter("surname");
79  		String givenName = request.getParameter("givenName");
80  		String dateOfBirth = request.getParameter("dateOfBirth");
81  		String gender = request.getParameter("gender");
82  		String postcode = request.getParameter("postcode");
83  		String localIdentifier = request.getParameter("localIdentifier");
84  		Logger.info("localIdentifier is:"+localIdentifier);
85  		String serviceAddress = request.getParameter("serviceAddress");
86  		ITKAddress itkServiceAddress = new ITKAddressImpl(serviceAddress);
87  
88          HttpSession session = request.getSession(true);
89          session.removeAttribute("nhsNumber");
90          session.removeAttribute("localIdentifier");
91          session.removeAttribute("responseCode");
92          session.removeAttribute("errorMessage");
93          session.removeAttribute("responseMessage");
94  		
95  		SpineMiniServicesClient client = new SpineMiniServicesClient();
96  		
97  		GetNHSNumberRequest doc = new GetNHSNumberRequest();
98  		doc.givenName = givenName;
99  		doc.surname = surname;
100 		doc.dateOfBirth = dateOfBirth;
101 		doc.gender = gender;
102 		doc.postcode = postcode;
103 		doc.localIdentifier = localIdentifier;
104 		GetNHSNumberResponse resp = client.getNHSNumber(doc,itkServiceAddress);
105 		if (resp == null) {
106 			Logger.info("SMSP GNN FAILED - No response document");
107             session.setAttribute("errorMessage", "Sorry this hasn't worked out this time. Please try again later.");
108 		} else {
109 			Logger.info("SMSP GNN COMPLETE. RESPONSE CODE:" + resp.getResponseCode());
110 			Logger.info("SMSP call succeeded");
111 			
112 	        //Obtain the session object, create a new session if doesn't exist
113             session.setAttribute("nhsNumber", resp.getNhsNumber());
114             session.setAttribute("localIdentifier", resp.getLocalIdentifier());
115             session.setAttribute("responseCode", resp.getResponseCode());
116 			try {
117 				Document responseDoc = DomUtils.parse(resp.getPayload());
118 				String prettyPayload = DomUtils.serialiseToXML(responseDoc, DomUtils.PRETTY_PRINT);
119  	            session.setAttribute("responseMessage", StringEscapeUtils.escapeHtml(prettyPayload));
120 			} catch (SAXException e) {
121 				Logger.error("SMSP GNN FAILED - SAXException",e);
122 	            session.setAttribute("errorMessage", "Sorry this really hasn't worked out this time. Please try again later.");
123 			} catch (ParserConfigurationException e) {
124 				Logger.error("SMSP GNN FAILED - ParserConfigurationException",e);
125 	            session.setAttribute("errorMessage", "Sorry this really hasn't worked out this time. Please try again later.");
126 			}
127 		}
128 		response.sendRedirect("./GetNHSNumberResponse.jsp");
129 
130 	}
131 
132 }