source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorServlet.java

Last change on this file was 1991, checked in by pharms, 9 years ago
  • added ordering id for requests and responses
File size: 4.3 KB
Line 
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
15package de.ugoe.cs.autoquest.httpmonitor;
16
17import java.io.IOException;
18
19import javax.servlet.ServletException;
20import javax.servlet.http.HttpServlet;
21import javax.servlet.http.HttpServletRequest;
22import javax.servlet.http.HttpServletResponse;
23import javax.xml.bind.JAXBContext;
24import javax.xml.bind.JAXBElement;
25import javax.xml.bind.JAXBException;
26import javax.xml.bind.Unmarshaller;
27
28import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
29import 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. 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.
39 * </p>
40 *
41 * @author Patrick Harms
42 */
43class 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;
52   
53    /**
54     * the id generator used to return new unique ids.
55     */
56    private transient IdGenerator idGenerator;
57
58    /**
59     * <p>
60     * initializes the servlet with the exchange handler to which all exchanges shall be forwarded
61     * and the id generator to be used for returning new ids
62     * </p>
63     *
64     * @param exchangeHandler the exchange handler that shall receive all exchanges
65     * @param idGenerator     the id generator used to generate new ids
66     */
67    HttpMonitorServlet(HttpMonitorExchangeHandler exchangeHandler, IdGenerator idGenerator) {
68        this.exchangeHandler = exchangeHandler;
69        this.idGenerator = idGenerator;
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    }
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    }
114   
115}
Note: See TracBrowser for help on using the repository browser.