source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitorRemoteExchangeHandler.java @ 1374

Last change on this file since 1374 was 1374, checked in by pharms, 10 years ago

Initial import.

File size: 6.6 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.proxy;
16
17import java.io.IOException;
18import java.io.OutputStreamWriter;
19import java.util.HashSet;
20import java.util.Set;
21import java.util.logging.Level;
22
23import javax.xml.bind.JAXBContext;
24import javax.xml.bind.JAXBException;
25import javax.xml.bind.Marshaller;
26
27import org.eclipse.jetty.client.HttpClient;
28import org.eclipse.jetty.client.api.Request;
29import org.eclipse.jetty.client.api.Response.CompleteListener;
30import org.eclipse.jetty.client.api.Result;
31import org.eclipse.jetty.client.util.OutputStreamContentProvider;
32import org.eclipse.jetty.http.HttpMethod;
33import org.eclipse.jetty.http.HttpVersion;
34import org.eclipse.jetty.util.thread.QueuedThreadPool;
35
36import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent;
37import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException;
38import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler;
39import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange;
40import de.ugoe.cs.autoquest.httpmonitor.exchange.ObjectFactory;
41import de.ugoe.cs.util.console.Console;
42
43/**
44 * <p>
45 * TODO comment
46 * </p>
47 *
48 * @author Patrick Harms
49 */
50public class HttpMonitorRemoteExchangeHandler
51    implements CompleteListener, HttpMonitorExchangeHandler, HttpMonitorComponent
52{
53
54    /** */
55    private HttpClient httpClient;
56   
57    /** */
58    private String httpMonitorServer;
59   
60    /** */
61    private int httpMonitorPort;
62
63    /** */
64    private Set<Request> openRequests = new HashSet<Request>();
65   
66    /**
67     * <p>
68     * TODO: comment
69     * </p>
70     *
71     * @param httpMonitorServer2
72     * @param httpMonitorPort2
73     */
74    public HttpMonitorRemoteExchangeHandler(String httpMonitorServer, int httpMonitorPort) {
75        this.httpMonitorServer = httpMonitorServer;
76        this.httpMonitorPort = httpMonitorPort;
77    }
78
79    /* (non-Javadoc)
80     * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#init()
81     */
82    @Override
83    public void init() throws IllegalStateException, HttpMonitorException {
84        httpClient = createHttpClient();
85    }
86
87    /* (non-Javadoc)
88     * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#start()
89     */
90    @Override
91    public void start() throws IllegalStateException, HttpMonitorException {
92        try {
93            httpClient.start();
94            httpClient.getContentDecoderFactories().clear();
95        }
96        catch (Exception e) {
97            throw new HttpMonitorException("could not start client for HTTP-Monitor", e);
98        }
99    }
100
101    /* (non-Javadoc)
102     * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent#stop()
103     */
104    @Override
105    public void stop() {
106        if (httpClient != null) {
107            try {
108                httpClient.stop();
109            }
110            catch (Exception e) {
111                Console.traceln(Level.WARNING, "could not stop client for HTTP-Monitor");
112                Console.logException(e);
113            }
114        }
115       
116        httpClient = null;
117    }
118
119    /* (non-Javadoc)
120     * @see de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler#handleHttpExchange()
121     */
122    @Override
123    public void handleHttpExchange(HttpExchange httpExchange) {
124        // send the exchange to the server and wait for completion
125        synchronized (openRequests) {
126            Request httpMonitorRequest = httpClient.newRequest(httpMonitorServer, httpMonitorPort);
127            httpMonitorRequest.method(HttpMethod.POST);
128            httpMonitorRequest.version(HttpVersion.HTTP_1_1);
129
130            OutputStreamContentProvider out = new OutputStreamContentProvider();
131            httpMonitorRequest.content(out);
132            httpMonitorRequest.send(this);
133
134            try {
135                JAXBContext jaxbContext =
136                        JAXBContext.newInstance(HttpExchange.class.getPackage().getName());
137                Marshaller marshaller = jaxbContext.createMarshaller();
138
139                OutputStreamWriter writer = new OutputStreamWriter(out.getOutputStream());
140                marshaller.marshal(new ObjectFactory().createHttpExchange(httpExchange), writer);
141
142                out.getOutputStream().close();
143            }
144            catch (JAXBException e) {
145                Console.printerrln("could not convert request and response to XML string: " + e);
146                Console.logException(e);
147            }
148            catch (IOException e) {
149                Console.printerrln
150                ("could not close the stream for sending data to the HTML monitor: " + e);
151                Console.logException(e);
152            }
153           
154            openRequests.add(httpMonitorRequest);
155           
156            try {
157                // wait for the request to be removed fromt the list asynchronously
158                while (openRequests.contains(httpMonitorRequest)) {
159                    openRequests.wait();
160                }
161            }
162            catch (InterruptedException e) {
163                // ignore, as this may only happen on system shutdown
164            }
165        }
166    }
167
168    /* (non-Javadoc)
169     * @see org.eclipse.jetty.client.api.Response.CompleteListener#onComplete(Result)
170     */
171    @Override
172    public void onComplete(Result result) {
173        if (result.isFailed()) {
174            Console.traceln
175                (Level.WARNING, "could not log exchange correctly: " + result.getFailure());
176            Console.logException((Exception) result.getFailure());
177        }
178       
179        synchronized (openRequests) {
180            openRequests.remove(result.getRequest());
181            openRequests.notify();
182        }
183    }
184
185    /**
186     *
187     */
188    private HttpClient createHttpClient() {
189        HttpClient client = new HttpClient();
190       
191        QueuedThreadPool executor = new QueuedThreadPool(10);
192        executor.setName("HttpMonitorClients");
193        client.setExecutor(executor);
194
195        client.setMaxConnectionsPerDestination(10000);
196        client.setIdleTimeout(30000);
197        client.setRequestBufferSize(1024*1024);
198        client.setResponseBufferSize(1024*1024);
199       
200        return client;
201    }
202}
Note: See TracBrowser for help on using the repository browser.