[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 | |
---|
[1561] | 28 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
[1374] | 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 |
---|
[1991] | 35 | * be parsed it is discarded. Exchanges are only received via the POST HTTP method. Via the GET |
---|
| 36 | * method, the servlet provides unique ordering ids as a header parameter in the response. The |
---|
| 37 | * header parameter is named "X-AutoQUEST-OrderingId". For creating the ids it uses a provided id |
---|
| 38 | * generator. |
---|
[1374] | 39 | * </p> |
---|
| 40 | * |
---|
| 41 | * @author Patrick Harms |
---|
| 42 | */ |
---|
| 43 | class HttpMonitorServlet extends HttpServlet { |
---|
| 44 | |
---|
| 45 | /** */ |
---|
| 46 | private static final long serialVersionUID = 1L; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * the exchange handler to forward received exchanges to. |
---|
| 50 | */ |
---|
| 51 | private transient HttpMonitorExchangeHandler exchangeHandler; |
---|
[1991] | 52 | |
---|
| 53 | /** |
---|
| 54 | * the id generator used to return new unique ids. |
---|
| 55 | */ |
---|
| 56 | private transient IdGenerator idGenerator; |
---|
[1374] | 57 | |
---|
| 58 | /** |
---|
| 59 | * <p> |
---|
| 60 | * initializes the servlet with the exchange handler to which all exchanges shall be forwarded |
---|
[1991] | 61 | * and the id generator to be used for returning new ids |
---|
[1374] | 62 | * </p> |
---|
| 63 | * |
---|
| 64 | * @param exchangeHandler the exchange handler that shall receive all exchanges |
---|
[1991] | 65 | * @param idGenerator the id generator used to generate new ids |
---|
[1374] | 66 | */ |
---|
[1991] | 67 | HttpMonitorServlet(HttpMonitorExchangeHandler exchangeHandler, IdGenerator idGenerator) { |
---|
[1374] | 68 | this.exchangeHandler = exchangeHandler; |
---|
[1991] | 69 | this.idGenerator = idGenerator; |
---|
[1374] | 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * this implements handling of doPost. For this servlet this means that |
---|
| 74 | * the data from the post request will be parsed and the contained exchanges forwarded to the |
---|
| 75 | * exchange handler. |
---|
| 76 | * |
---|
| 77 | * (non-Javadoc) |
---|
| 78 | * @see org.mortbay.jetty.servlet.DefaultServlet#doPost(HttpServletRequest, HttpServletResponse) |
---|
| 79 | */ |
---|
| 80 | @Override |
---|
| 81 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
| 82 | throws ServletException, IOException |
---|
| 83 | { |
---|
| 84 | try { |
---|
| 85 | JAXBContext jaxbContext = |
---|
| 86 | JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
| 87 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
---|
| 88 | |
---|
| 89 | @SuppressWarnings("unchecked") |
---|
| 90 | JAXBElement<HttpExchange> jaxObject = |
---|
| 91 | (JAXBElement<HttpExchange>) unmarshaller.unmarshal(request.getReader()); |
---|
| 92 | |
---|
| 93 | exchangeHandler.handleHttpExchange(jaxObject.getValue()); |
---|
| 94 | } |
---|
| 95 | catch (JAXBException e) { |
---|
| 96 | Console.printerrln |
---|
| 97 | ("could not parse incoming data --> discarding it (" + e.toString() + ")"); |
---|
| 98 | } |
---|
| 99 | } |
---|
[1991] | 100 | |
---|
| 101 | /** |
---|
| 102 | * this implements handling of doGet. For this servlet this means that we return a unique |
---|
| 103 | * ordering id which can be used by proxies as ordering ids for requests and responses. |
---|
| 104 | * |
---|
| 105 | * (non-Javadoc) (non-Javadoc) |
---|
| 106 | * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse) |
---|
| 107 | */ |
---|
| 108 | @Override |
---|
| 109 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
| 110 | throws ServletException, IOException |
---|
| 111 | { |
---|
| 112 | response.setHeader("X-AutoQUEST-OrderingId", Long.toString(idGenerator.getNextId())); |
---|
| 113 | } |
---|
[1374] | 114 | |
---|
| 115 | } |
---|