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.proxy; |
---|
16 | |
---|
17 | import java.io.IOException; |
---|
18 | import java.io.OutputStreamWriter; |
---|
19 | import java.util.logging.Level; |
---|
20 | |
---|
21 | import javax.xml.bind.JAXBContext; |
---|
22 | import javax.xml.bind.JAXBException; |
---|
23 | import javax.xml.bind.Marshaller; |
---|
24 | |
---|
25 | import org.eclipse.jetty.client.api.Request; |
---|
26 | import org.eclipse.jetty.client.api.Result; |
---|
27 | import org.eclipse.jetty.client.util.OutputStreamContentProvider; |
---|
28 | import org.eclipse.jetty.http.HttpMethod; |
---|
29 | import org.eclipse.jetty.http.HttpVersion; |
---|
30 | |
---|
31 | import de.ugoe.cs.autoquest.httpmonitor.HttpMonitor; |
---|
32 | import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler; |
---|
33 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
34 | import de.ugoe.cs.autoquest.plugin.http.logdata.ObjectFactory; |
---|
35 | import de.ugoe.cs.util.console.Console; |
---|
36 | |
---|
37 | /** |
---|
38 | * <p> |
---|
39 | * If the exchanges recorded by the proxy are to be transmitted to a central {@link HttpMonitor}, |
---|
40 | * this exchanges handler is used. It is called by the exchange listener on completion of a proxied |
---|
41 | * request/response. It then creates an HTTP request to the central monitor and sends it there. |
---|
42 | * It is initialized with the name of the server and the port on which the central monitor runs. |
---|
43 | * If the exchanges cannot be forwarded to the central server, they are discarded. |
---|
44 | * </p> |
---|
45 | * |
---|
46 | * @author Patrick Harms |
---|
47 | */ |
---|
48 | public class HttpMonitorRemoteExchangeHandler |
---|
49 | extends HttpMonitorRemoteConnection |
---|
50 | implements HttpMonitorExchangeHandler |
---|
51 | { |
---|
52 | |
---|
53 | /** |
---|
54 | * <p> |
---|
55 | * initializes the exchange handler with the host and port of the central server |
---|
56 | * </p> |
---|
57 | * |
---|
58 | * @param httpMonitorServer the host name of the central server |
---|
59 | * @param httpMonitorPort the port of the central server |
---|
60 | */ |
---|
61 | public HttpMonitorRemoteExchangeHandler(String httpMonitorServer, int httpMonitorPort) { |
---|
62 | super(httpMonitorServer, httpMonitorPort); |
---|
63 | } |
---|
64 | |
---|
65 | /* (non-Javadoc) |
---|
66 | * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler#handleHttpExchange() |
---|
67 | */ |
---|
68 | @Override |
---|
69 | public synchronized void handleHttpExchange(HttpExchange httpExchange) { |
---|
70 | // send the exchange to the server and wait for completion |
---|
71 | Request httpMonitorRequest = super.newRequest(); |
---|
72 | httpMonitorRequest.method(HttpMethod.POST); |
---|
73 | httpMonitorRequest.version(HttpVersion.HTTP_1_1); |
---|
74 | |
---|
75 | OutputStreamContentProvider out = new OutputStreamContentProvider(); |
---|
76 | httpMonitorRequest.content(out); |
---|
77 | |
---|
78 | super.sendRequest(httpMonitorRequest); |
---|
79 | |
---|
80 | try { |
---|
81 | JAXBContext jaxbContext = |
---|
82 | JAXBContext.newInstance(HttpExchange.class.getPackage().getName()); |
---|
83 | Marshaller marshaller = jaxbContext.createMarshaller(); |
---|
84 | |
---|
85 | OutputStreamWriter writer = new OutputStreamWriter(out.getOutputStream(), "UTF-8"); |
---|
86 | marshaller.marshal(new ObjectFactory().createHttpExchange(httpExchange), writer); |
---|
87 | |
---|
88 | out.getOutputStream().close(); |
---|
89 | } |
---|
90 | catch (JAXBException e) { |
---|
91 | Console.printerrln("could not convert request and response to XML string: " + e); |
---|
92 | Console.logException(e); |
---|
93 | } |
---|
94 | catch (IOException e) { |
---|
95 | Console.printerrln |
---|
96 | ("could not close the stream for sending data to the HTML monitor: " + e); |
---|
97 | Console.logException(e); |
---|
98 | } |
---|
99 | |
---|
100 | // wait to ensure that the request was received |
---|
101 | Result result = super.getResult(httpMonitorRequest); |
---|
102 | |
---|
103 | if (result.isFailed()) { |
---|
104 | Console.traceln |
---|
105 | (Level.WARNING, "could not log exchange correctly: " + result.getFailure()); |
---|
106 | Console.logException((Exception) result.getFailure()); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|