587
|
1 |
/*
|
|
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 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
package com.nokia.helium.diamonds;
|
|
20 |
|
|
21 |
import org.apache.commons.httpclient.HttpClient;
|
|
22 |
import org.apache.commons.httpclient.HttpException;
|
|
23 |
import org.apache.commons.httpclient.methods.FileRequestEntity;
|
|
24 |
import org.apache.commons.httpclient.methods.PostMethod;
|
|
25 |
import org.apache.commons.httpclient.methods.RequestEntity;
|
|
26 |
import org.apache.commons.lang.StringUtils;
|
|
27 |
import org.apache.log4j.Logger;
|
|
28 |
import com.nokia.helium.core.EmailDataSender;
|
|
29 |
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.File;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Diamonds client used to connect to get build id and also to send the build
|
|
35 |
* results
|
|
36 |
*
|
|
37 |
*/
|
|
38 |
public class DiamondsClient {
|
|
39 |
|
|
40 |
private static final int INT_SERV_ERROR = 500;
|
|
41 |
|
|
42 |
private static final int SERV_NOT_FOUND = 404;
|
|
43 |
|
|
44 |
private static final int SERV_OK = 200;
|
|
45 |
|
|
46 |
private boolean isRecordOnly;
|
|
47 |
|
|
48 |
private Logger log = Logger.getLogger(DiamondsClient.class);
|
|
49 |
|
|
50 |
private String host;
|
|
51 |
|
|
52 |
private String port;
|
|
53 |
|
|
54 |
private String path;
|
|
55 |
|
|
56 |
private String emailID;
|
|
57 |
|
|
58 |
private HttpClient httpClient;
|
|
59 |
|
|
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);
|
|
73 |
} catch (IOException e) {
|
|
74 |
isRecordOnly = true;
|
|
75 |
throw new DiamondsException("IOException while sending http request." + e.getMessage());
|
|
76 |
// e.printStackTrace();
|
|
77 |
}
|
|
78 |
return result;
|
|
79 |
}
|
|
80 |
|
|
81 |
private String checkForProtocol(String url) throws DiamondsException {
|
|
82 |
String retURL = url;
|
|
83 |
if (!StringUtils.containsIgnoreCase(url, "http://")) {
|
|
84 |
retURL = "http://" + url;
|
|
85 |
}
|
|
86 |
return retURL;
|
|
87 |
}
|
|
88 |
|
|
89 |
private String getURL() throws DiamondsException {
|
|
90 |
return getURL(null);
|
|
91 |
}
|
|
92 |
|
|
93 |
private String getURL(String subPath) throws DiamondsException {
|
|
94 |
String urlPath = path;
|
|
95 |
if (subPath != null) {
|
|
96 |
urlPath = subPath;
|
|
97 |
}
|
|
98 |
return checkForProtocol("http://" + host + ":" + port + urlPath);
|
|
99 |
}
|
|
100 |
|
|
101 |
private PostMethod getPostMethod(String fileName, String urlPath) {
|
|
102 |
|
|
103 |
// Get the Diamonds XML-file which is to be exported
|
|
104 |
File input = new File(fileName);
|
|
105 |
|
|
106 |
// Prepare HTTP post
|
|
107 |
PostMethod post = new PostMethod(urlPath);
|
|
108 |
|
|
109 |
// Request content will be retrieved directly
|
|
110 |
// from the input stream
|
|
111 |
|
|
112 |
RequestEntity entity = new FileRequestEntity(input,
|
|
113 |
"text/xml; charset=ISO-8859-1");
|
|
114 |
post.setRequestEntity(entity);
|
|
115 |
return post;
|
|
116 |
}
|
|
117 |
|
|
118 |
private int processPostMethodResult(int result) {
|
|
119 |
// Log status code
|
|
120 |
switch (result) {
|
|
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);
|
|
132 |
}
|
|
133 |
return result;
|
|
134 |
}
|
|
135 |
|
|
136 |
|
|
137 |
/**
|
|
138 |
*
|
|
139 |
* @param fileName
|
|
140 |
* Filename to export to Diamonds
|
|
141 |
* @return diamonds build id
|
|
142 |
*/
|
|
143 |
public String getBuildId(String fileName) throws DiamondsException {
|
|
144 |
String diamondsBuildID = null;
|
|
145 |
PostMethod postMethod = null;
|
|
146 |
|
|
147 |
// Get HTTP client
|
|
148 |
// MyHttpClient httpclient = createHttpClient();
|
|
149 |
|
|
150 |
// Execute post request
|
|
151 |
try {
|
|
152 |
if (!isRecordOnly) {
|
|
153 |
String strURL = getURL();
|
|
154 |
log.debug("strURL:" + strURL);
|
|
155 |
postMethod = getPostMethod(fileName, strURL);
|
|
156 |
log.debug("postmethod:" + postMethod);
|
|
157 |
int postMethodResult = httpClient.executeMethod(postMethod);
|
|
158 |
log.debug("postmethod-result:" + postMethodResult);
|
|
159 |
|
|
160 |
int result = processPostMethodResult(postMethodResult);
|
|
161 |
|
|
162 |
if (result == SERV_OK) {
|
|
163 |
// Display and save response code which functions as a id for
|
|
164 |
// the build.
|
|
165 |
diamondsBuildID = postMethod.getResponseBodyAsString();
|
|
166 |
log.debug("diamondsBuildID: " + diamondsBuildID);
|
|
167 |
} else {
|
|
168 |
isRecordOnly = true;
|
|
169 |
log.error("Diamonds data not sent, because of connection failure.");
|
|
170 |
//throw new DiamondsException("Connection Failed");
|
|
171 |
}
|
|
172 |
}
|
|
173 |
} catch (HttpException ex) {
|
|
174 |
isRecordOnly = true;
|
|
175 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
|
176 |
log.debug("Diamonds data not sent:s", ex);
|
|
177 |
} catch (IOException ex) {
|
|
178 |
isRecordOnly = true;
|
|
179 |
log.error("Diamonds data not sent: " + ex.getMessage());
|
|
180 |
log.debug("Diamonds data not sent:", ex);
|
|
181 |
} finally {
|
|
182 |
// Release current connection to the connection pool once you are
|
|
183 |
// done
|
|
184 |
if (postMethod != null) {
|
|
185 |
postMethod.releaseConnection();
|
|
186 |
}
|
|
187 |
}
|
|
188 |
return diamondsBuildID;
|
|
189 |
}
|
|
190 |
|
|
191 |
public int sendData(String fileName, String urlPath) {
|
|
192 |
PostMethod postMethod = null;
|
|
193 |
int result = -1;
|
|
194 |
if (urlPath != null && !isRecordOnly) {
|
|
195 |
try {
|
|
196 |
String strURL = getURL(urlPath);
|
|
197 |
postMethod = getPostMethod(fileName, strURL);
|
|
198 |
result = processPostMethodResult(httpClient
|
|
199 |
.executeMethod(postMethod));
|
618
|
200 |
} catch (IllegalArgumentException e) {
|
|
201 |
// Catching this exception is needed because it is raised by httpclient
|
|
202 |
// library if the server is under update.
|
|
203 |
log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
|
587
|
204 |
} catch (DiamondsException e) {
|
|
205 |
log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
|
|
206 |
} catch (IOException e) {
|
|
207 |
log.error("sendData:The final data via http not sent because errors:IOException ", e);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
return result;
|
|
211 |
}
|
|
212 |
|
|
213 |
public int sendDataByMail(String fileName, String smtpServer,
|
|
214 |
String ldapServer) {
|
|
215 |
log.debug("DiamondsClient:sendDataByEmail:emailID" + emailID);
|
|
216 |
EmailDataSender emailSender = new EmailDataSender(emailID, smtpServer,
|
|
217 |
ldapServer);
|
|
218 |
log.debug("DiamondsClient:sendDataByEmail: " + fileName);
|
|
219 |
emailSender.sendData("diamonds", fileName, "application/xml",
|
|
220 |
"[DIAMONDS_DATA]", null);
|
|
221 |
log.debug("DiamondsClient:sendDataByEmail:succeeds");
|
|
222 |
return 0;
|
|
223 |
}
|
|
224 |
} |