[1374] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
| 16 | |
---|
| 17 | import java.io.IOException; |
---|
| 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.xml.bind.JAXBContext; |
---|
| 24 | import javax.xml.bind.JAXBElement; |
---|
| 25 | import javax.xml.bind.JAXBException; |
---|
| 26 | import javax.xml.bind.Unmarshaller; |
---|
| 27 | |
---|
| 28 | import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; |
---|
| 29 | import de.ugoe.cs.util.console.Console; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * <p> |
---|
| 33 | * the servlet deployed in the web server that receives all recorded exchanges from the proxy. The |
---|
| 34 | * exchanges are parsed and forwarded to the provided exchange handler. If an exchange can not |
---|
| 35 | * be parsed it is discarded. Exchanges are only received via the POST HTTP method. |
---|
| 36 | * </p> |
---|
| 37 | * |
---|
| 38 | * @author Patrick Harms |
---|
| 39 | */ |
---|
| 40 | class HttpMonitorServlet extends HttpServlet { |
---|
| 41 | |
---|
| 42 | /** */ |
---|
| 43 | private static final long serialVersionUID = 1L; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * the exchange handler to forward received exchanges to. |
---|
| 47 | */ |
---|
| 48 | private transient HttpMonitorExchangeHandler exchangeHandler; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * <p> |
---|
| 52 | * initializes the servlet with the exchange handler to which all exchanges shall be forwarded |
---|
| 53 | * </p> |
---|
| 54 | * |
---|
| 55 | * @param exchangeHandler the exchange handler that shall receive all exchanges |
---|
| 56 | */ |
---|
| 57 | HttpMonitorServlet(HttpMonitorExchangeHandler exchangeHandler) { |
---|
| 58 | this.exchangeHandler = exchangeHandler; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * this implements handling of doPost. For this servlet this means that |
---|
| 63 | * the data from the post request will be parsed and the contained exchanges forwarded to the |
---|
| 64 | * exchange handler. |
---|
| 65 | * |
---|
| 66 | * (non-Javadoc) |
---|
| 67 | * @see org.mortbay.jetty.servlet.DefaultServlet#doPost(HttpServletRequest, HttpServletResponse) |
---|
| 68 | */ |
---|
| 69 | @Override |
---|
| 70 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
| 71 | throws ServletException, IOException |
---|
| 72 | { |
---|
| 73 | try { |
---|
| 74 | JAXBContext jaxbContext = |
---|
| 75 | JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
| 76 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
| 77 | |
---|
| 78 | @SuppressWarnings("unchecked") |
---|
| 79 | JAXBElement<HttpExchange> jaxObject = |
---|
| 80 | (JAXBElement<HttpExchange>) unmarshaller.unmarshal(request.getReader()); |
---|
| 81 | |
---|
| 82 | exchangeHandler.handleHttpExchange(jaxObject.getValue()); |
---|
| 83 | } |
---|
| 84 | catch (JAXBException e) { |
---|
| 85 | Console.printerrln |
---|
| 86 | ("could not parse incoming data --> discarding it (" + e.toString() + ")"); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | } |
---|