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