1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.remote.comms;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.PrintWriter;
20 import java.net.Authenticator;
21 import java.net.HttpURLConnection;
22 import java.net.PasswordAuthentication;
23 import java.net.URL;
24 import java.net.URLConnection;
25 import java.net.URLEncoder;
26
27 import org.jdiagnose.DiagnosticMessage;
28 import org.jdiagnose.RemoteResult;
29 import org.jdiagnose.library.HttpProxyConfig;
30 import org.jdiagnose.remote.ExceptionListener;
31 import org.jdiagnose.remote.RemoteResultException;
32 import org.jdiagnose.remote.RemoteResultListener;
33 import org.jdiagnose.remote.RemoteResultSender;
34 import org.jdiagnose.remote.StdErrExceptionListener;
35
36 /***
37 * Sends Diagnostic Results to the JDiagnose Server view HTTP.
38 *
39 * @author jmccrindle
40 */
41 public class HttpSender implements RemoteResultSender, RemoteResultListener {
42
43 private String url = null;
44 private HttpProxyConfig proxyConfig = null;
45 private String method = "POST";
46 private ExceptionListener exceptionListener = StdErrExceptionListener.DEFAULT_STD_ERR_EXCEPTION_LISTENER;
47
48 /***
49 * @param url
50 */
51 public HttpSender(String url) {
52 super();
53 this.url = url;
54 }
55
56 /***
57 */
58 public HttpSender() {
59 super();
60 }
61
62
63
64
65
66
67 public void send(RemoteResult result) throws RemoteResultException {
68
69 if (url == null) {
70 throw new RemoteResultException(
71 "url property must be set for HttpSender");
72 }
73
74 try {
75 if (proxyConfig != null) {
76 System
77 .setProperty("http.proxyHost", proxyConfig
78 .getProxyHost());
79 System
80 .setProperty("http.proxyPort", proxyConfig
81 .getProxyPort());
82 if (proxyConfig.getProxyUsername() != null
83 && proxyConfig.getProxyPassword() != null) {
84 Authenticator.setDefault(new Authenticator() {
85 protected PasswordAuthentication getPasswordAuthentication() {
86 return new PasswordAuthentication(proxyConfig
87 .getProxyUsername(), proxyConfig
88 .getProxyPassword().toCharArray());
89 }
90 });
91 }
92 }
93 URL fullUrl = new URL(url);
94 URLConnection connection = fullUrl.openConnection();
95
96 HttpURLConnection urlConnection = (HttpURLConnection) connection;
97
98 urlConnection.setRequestMethod(method);
99 urlConnection.setUseCaches(false);
100 urlConnection.setDoOutput(true);
101
102 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
103
104
105 PrintWriter out = new PrintWriter(byteStream, true);
106 StringBuffer postData = new StringBuffer("host="
107 + URLEncoder.encode(result.getHost())
108 + "&agent="
109 + URLEncoder.encode(result.getAgent())
110 + "&guid="
111 + URLEncoder.encode(result.getGuid())
112 + "&sequenceNumber="
113 + result.getSequenceNumber()
114 + "&resultInfo.name="
115 + URLEncoder.encode(result.getResultInfo().getName())
116 + "&resultInfo.duration="
117 + result.getResultInfo().getDuration()
118 + "&resultInfo.startTime="
119 + result.getResultInfo().getStartTime()
120 + "&resultInfo.finishTime="
121 + result.getResultInfo().getFinishTime()
122 + "&resultInfo.state="
123 + URLEncoder.encode(result.getResultInfo().getState()
124 .toString()));
125
126 DiagnosticMessage message = result.getResultInfo()
127 .getMessage();
128
129 if (message != null) {
130 postData.append("&resultInfo.message.summary="
131 + URLEncoder.encode(message.getSummary()));
132 postData.append("&resultInfo.message.body="
133 + URLEncoder.encode(message.getBody()));
134 }
135
136
137 out.print(postData);
138 out.flush();
139
140
141 String lengthString = String.valueOf(byteStream.size());
142 connection.setRequestProperty("Content-Length", lengthString);
143
144 connection.setRequestProperty("Content-Type",
145 "application/x-www-form-urlencoded");
146
147
148 byteStream.writeTo(connection.getOutputStream());
149
150 int responseCode = urlConnection.getResponseCode();
151 if (responseCode != 200) {
152 throw new RemoteResultException(
153 "Http sending failed with response code "
154 + responseCode);
155 }
156 } catch (RemoteResultException e) {
157 throw e;
158 } catch (Throwable e) {
159 throw new RemoteResultException(e);
160 }
161 }
162
163 public String getUrl() {
164 return url;
165 }
166
167 public void setUrl(String string) {
168 url = string;
169 }
170
171 public String getMethod() {
172 return method;
173 }
174
175 public void setMethod(String method) {
176 this.method = method;
177 }
178
179 public HttpProxyConfig getProxyConfig() {
180 return proxyConfig;
181 }
182
183 public void setProxyConfig(HttpProxyConfig proxyConfig) {
184 this.proxyConfig = proxyConfig;
185 }
186
187
188
189
190 public void onRemoteResult(RemoteResult remoteResult) {
191 try {
192 send(remoteResult);
193 } catch (Throwable e) {
194 exceptionListener.onException(e);
195 }
196 }
197
198 }