587
|
1 |
/*
|
628
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
587
|
17 |
|
|
18 |
package com.nokia.helium.diamonds;
|
|
19 |
|
628
|
20 |
import java.io.File;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.InputStream;
|
587
|
23 |
import org.apache.commons.httpclient.HttpClient;
|
|
24 |
import org.apache.commons.httpclient.HttpException;
|
|
25 |
import org.apache.commons.httpclient.methods.FileRequestEntity;
|
|
26 |
import org.apache.commons.httpclient.methods.PostMethod;
|
|
27 |
import org.apache.commons.httpclient.methods.RequestEntity;
|
628
|
28 |
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
|
587
|
29 |
import org.apache.commons.lang.StringUtils;
|
|
30 |
import org.apache.log4j.Logger;
|
628
|
31 |
|
587
|
32 |
import com.nokia.helium.core.EmailDataSender;
|
628
|
33 |
import com.nokia.helium.core.EmailSendException;
|
587
|
34 |
|
|
35 |
/**
|
628
|
36 |
* Diamonds client used to connect to get build id and also to send the build results
|
587
|
37 |
*
|
|
38 |
*/
|
|
39 |
public class DiamondsClient {
|
|
40 |
|
|
41 |
private static final int INT_SERV_ERROR = 500;
|
|
42 |
|
|
43 |
private static final int SERV_NOT_FOUND = 404;
|
|
44 |
|
|
45 |
private static final int SERV_OK = 200;
|
628
|
46 |
|
587
|
47 |
private boolean isRecordOnly;
|
|
48 |
|
|
49 |
private Logger log = Logger.getLogger(DiamondsClient.class);
|
|
50 |
|
|
51 |
private String host;
|
|
52 |
|
|
53 |
private String port;
|
|
54 |
|
|
55 |
private String path;
|
|
56 |
|
|
57 |
private String emailID;
|
|
58 |
|
|
59 |
private HttpClient httpClient;
|
|
60 |
|
|
61 |
public DiamondsClient(String hst, String prt, String pth, String mailID) {
|
|
62 |
host = hst;
|
|
63 |
port = prt;
|
|
64 |
path = pth;
|
|
65 |
emailID = mailID;
|
|
66 |
httpClient = new HttpClient();
|
|
67 |
}
|
|
68 |
|
|
69 |
private int executeMethod(PostMethod postMethod) throws DiamondsException {
|
|
70 |
int result = 0;
|
|
71 |
try {
|
|
72 |
result = httpClient.executeMethod(postMethod);
|
628
|
73 |
}
|
|
74 |
catch (IOException e) {
|
587
|
75 |
isRecordOnly = true;
|
|
76 |
throw new DiamondsException("IOException while sending http request." + e.getMessage());
|
|
77 |
// e.printStackTrace();
|
|
78 |
}
|
|
79 |
return result;
|
|
80 |
}
|
|
81 |
|
|
82 |
private String checkForProtocol(String url) throws DiamondsException {
|
|
83 |
String retURL = url;
|
|
84 |
if (!StringUtils.containsIgnoreCase(url, "http://")) {
|
|
85 |
retURL = "http://" + url;
|
|
86 |
}
|
|
87 |
return retURL;
|
|
88 |
}
|
|
89 |
|
|
90 |
private String getURL() throws DiamondsException {
|
|
91 |
return getURL(null);
|
|
92 |
}
|
|
93 |
|
|
94 |
private String getURL(String subPath) throws DiamondsException {
|
|
95 |
String urlPath = path;
|
|
96 |
if (subPath != null) {
|
|
97 |
urlPath = subPath;
|
|
98 |
}
|
|
99 |
return checkForProtocol("http://" + host + ":" + port + urlPath);
|
|
100 |
}
|
|
101 |
|
|
102 |
private PostMethod getPostMethod(String fileName, String urlPath) {
|
|
103 |
|
|
104 |
// Get the Diamonds XML-file which is to be exported
|
|
105 |
File input = new File(fileName);
|
|
106 |
|
|
107 |
// Prepare HTTP post
|
|
108 |
PostMethod post = new PostMethod(urlPath);
|
|
109 |
|
|
110 |
// Request content will be retrieved directly
|
|
111 |
// from the input stream
|
|
112 |
|
628
|
113 |
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
|
587
|
114 |
post.setRequestEntity(entity);
|
|
115 |
return post;
|
|
116 |
}
|
|
117 |
|
|
118 |
private int processPostMethodResult(int result) {
|
|
119 |
// Log status code
|
|
120 |
switch (result) {
|
628
|
121 |
case INT_SERV_ERROR:
|
|
122 |
// log.error("Internal server error");
|
|
123 |
break;
|
|
124 |
case SERV_NOT_FOUND:
|
|
125 |
// log.error("Server not found");
|
|
126 |
break;
|
|
127 |
case SERV_OK:
|
|
128 |
// log.info("Connection to diamonds server - OK");
|
|
129 |
break;
|
|
130 |
default:
|
|
131 |
// log.debug("Response code: " + result);
|
587
|
132 |
}
|
|
133 |
return result;
|
|
134 |
}
|
628
|
135 |
|
|
136 |
public String getBuildId(InputStream stream) throws DiamondsException {
|
|
137 |
String diamondsBuildID = null;
|
|
138 |
PostMethod postMethod = null;
|
|
139 |
try {
|
|
140 |
if (!isRecordOnly) {
|
|
141 |
String strURL = getURL();
|
|
142 |
log.debug("strURL:" + strURL);
|
|
143 |
postMethod = getPostMethod(stream, strURL);
|
|
144 |
log.debug("postmethod:" + postMethod);
|
|
145 |
int postMethodResult = httpClient.executeMethod(postMethod);
|
|
146 |
log.debug("postmethod-result:" + postMethodResult);
|
587
|
147 |
|
628
|
148 |
int result = processPostMethodResult(postMethodResult);
|
|
149 |
|
|
150 |
if (result == SERV_OK) {
|
|
151 |
// Display and save response code which functions as a id for
|
|
152 |
// the build.
|
|
153 |
diamondsBuildID = postMethod.getResponseBodyAsString();
|
|
154 |
log.debug("diamondsBuildID: " + diamondsBuildID);
|
|
155 |
}
|
|
156 |
else {
|
|
157 |
isRecordOnly = true;
|
|
158 |
log.error("Diamonds data not sent, because of connection failure.");
|
|
159 |
// throw new DiamondsException("Connection Failed");
|
|
160 |
}
|
|
161 |
}
|
|
162 |
}
|
|
163 |
catch (HttpException ex) {
|
|
164 |
isRecordOnly = true;
|
|
165 |
log.debug("Diamonds data not sent:s", ex);
|
|
166 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
|
167 |
}
|
|
168 |
catch (IOException ex) {
|
|
169 |
isRecordOnly = true;
|
|
170 |
log.debug("Diamonds data not sent:", ex);
|
|
171 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
|
172 |
}
|
|
173 |
finally {
|
|
174 |
// Release current connection to the connection pool once you are
|
|
175 |
// done
|
|
176 |
if (postMethod != null) {
|
|
177 |
postMethod.releaseConnection();
|
|
178 |
}
|
|
179 |
}
|
|
180 |
return diamondsBuildID;
|
|
181 |
}
|
587
|
182 |
|
|
183 |
/**
|
|
184 |
*
|
628
|
185 |
* @param fileName Filename to export to Diamonds
|
587
|
186 |
* @return diamonds build id
|
|
187 |
*/
|
|
188 |
public String getBuildId(String fileName) throws DiamondsException {
|
|
189 |
String diamondsBuildID = null;
|
|
190 |
PostMethod postMethod = null;
|
|
191 |
|
|
192 |
// Get HTTP client
|
|
193 |
// MyHttpClient httpclient = createHttpClient();
|
|
194 |
|
|
195 |
// Execute post request
|
|
196 |
try {
|
|
197 |
if (!isRecordOnly) {
|
|
198 |
String strURL = getURL();
|
|
199 |
log.debug("strURL:" + strURL);
|
|
200 |
postMethod = getPostMethod(fileName, strURL);
|
|
201 |
log.debug("postmethod:" + postMethod);
|
|
202 |
int postMethodResult = httpClient.executeMethod(postMethod);
|
|
203 |
log.debug("postmethod-result:" + postMethodResult);
|
628
|
204 |
|
587
|
205 |
int result = processPostMethodResult(postMethodResult);
|
|
206 |
|
|
207 |
if (result == SERV_OK) {
|
|
208 |
// Display and save response code which functions as a id for
|
|
209 |
// the build.
|
|
210 |
diamondsBuildID = postMethod.getResponseBodyAsString();
|
|
211 |
log.debug("diamondsBuildID: " + diamondsBuildID);
|
628
|
212 |
}
|
|
213 |
else {
|
587
|
214 |
isRecordOnly = true;
|
|
215 |
log.error("Diamonds data not sent, because of connection failure.");
|
628
|
216 |
// throw new DiamondsException("Connection Failed");
|
587
|
217 |
}
|
|
218 |
}
|
628
|
219 |
}
|
|
220 |
catch (HttpException ex) {
|
587
|
221 |
isRecordOnly = true;
|
|
222 |
log.debug("Diamonds data not sent:s", ex);
|
|
223 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
628
|
224 |
}
|
|
225 |
catch (IOException ex) {
|
|
226 |
isRecordOnly = true;
|
587
|
227 |
log.debug("Diamonds data not sent:", ex);
|
628
|
228 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
|
229 |
}
|
|
230 |
finally {
|
587
|
231 |
// Release current connection to the connection pool once you are
|
|
232 |
// done
|
|
233 |
if (postMethod != null) {
|
|
234 |
postMethod.releaseConnection();
|
|
235 |
}
|
|
236 |
}
|
|
237 |
return diamondsBuildID;
|
|
238 |
}
|
|
239 |
|
628
|
240 |
private PostMethod getPostMethod(InputStream stream, String urlPath) {
|
|
241 |
|
|
242 |
// Get the Diamonds XML-file which is to be exported
|
|
243 |
//File input = new File(fileName);
|
|
244 |
|
|
245 |
// Prepare HTTP post
|
|
246 |
PostMethod post = new PostMethod(urlPath);
|
|
247 |
|
|
248 |
RequestEntity entity = new InputStreamRequestEntity(stream, "text/xml");
|
|
249 |
post.setRequestEntity(entity);
|
|
250 |
return post;
|
|
251 |
}
|
|
252 |
|
|
253 |
public int sendData(InputStream stream, String urlPath) {
|
|
254 |
PostMethod postMethod = null;
|
|
255 |
int result = -1;
|
|
256 |
if (urlPath != null && !isRecordOnly) {
|
|
257 |
try {
|
|
258 |
String strURL = getURL(urlPath);
|
|
259 |
postMethod = getPostMethod(stream, strURL);
|
|
260 |
result = processPostMethodResult(httpClient.executeMethod(postMethod));
|
|
261 |
}
|
|
262 |
catch (IllegalArgumentException e) {
|
|
263 |
// Catching this exception is needed because it is raised by httpclient
|
|
264 |
// library if the server is under update.
|
|
265 |
log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
|
|
266 |
}
|
|
267 |
catch (DiamondsException e) {
|
|
268 |
log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
|
|
269 |
}
|
|
270 |
catch (IOException e) {
|
|
271 |
log.error("sendData:The final data via http not sent because errors:IOException ", e);
|
|
272 |
}
|
|
273 |
}
|
|
274 |
return result;
|
|
275 |
}
|
|
276 |
|
587
|
277 |
public int sendData(String fileName, String urlPath) {
|
|
278 |
PostMethod postMethod = null;
|
|
279 |
int result = -1;
|
|
280 |
if (urlPath != null && !isRecordOnly) {
|
|
281 |
try {
|
|
282 |
String strURL = getURL(urlPath);
|
|
283 |
postMethod = getPostMethod(fileName, strURL);
|
628
|
284 |
result = processPostMethodResult(httpClient.executeMethod(postMethod));
|
|
285 |
}
|
|
286 |
catch (IllegalArgumentException e) {
|
588
|
287 |
// Catching this exception is needed because it is raised by httpclient
|
|
288 |
// library if the server is under update.
|
|
289 |
log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
|
628
|
290 |
}
|
|
291 |
catch (DiamondsException e) {
|
587
|
292 |
log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
|
628
|
293 |
}
|
|
294 |
catch (IOException e) {
|
587
|
295 |
log.error("sendData:The final data via http not sent because errors:IOException ", e);
|
|
296 |
}
|
|
297 |
}
|
|
298 |
return result;
|
|
299 |
}
|
|
300 |
|
628
|
301 |
public int sendDataByMail(String fileName, String smtpServer, String ldapServer) throws EmailSendException {
|
587
|
302 |
log.debug("DiamondsClient:sendDataByEmail:emailID" + emailID);
|
628
|
303 |
EmailDataSender emailSender = new EmailDataSender(emailID, smtpServer, ldapServer);
|
587
|
304 |
log.debug("DiamondsClient:sendDataByEmail: " + fileName);
|
628
|
305 |
emailSender.sendData("diamonds", new File(fileName), "application/xml", "[DIAMONDS_DATA]", null);
|
587
|
306 |
log.debug("DiamondsClient:sendDataByEmail:succeeds");
|
|
307 |
return 0;
|
|
308 |
}
|
|
309 |
} |