/
HQConnection.java
279 lines (248 loc) · 10.5 KB
/
HQConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/
package org.hyperic.hq.hqapi1;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperic.hq.hqapi1.types.ResponseStatus;
import org.hyperic.hq.hqapi1.types.ServiceError;
import javax.xml.bind.JAXBException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.SocketException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
class HQConnection {
private static Log _log = LogFactory.getLog(HQConnection.class);
private String _host;
private int _port;
private boolean _isSecure;
private String _user;
private String _password;
HQConnection(String host, int port, boolean isSecure, String user,
String password) {
_host = host;
_port = port;
_isSecure = isSecure;
_user = user;
_password = password;
if (_isSecure) {
// To allow for self signed certificates
UntrustedSSLProtocolSocketFactory.register();
}
}
/**
* Generate an response object with the given Error. In some cases the
* HQ server will not give us a result, so we generate one ourselves.
* XXX: It would be nice here if we could get JAXB to generate an
* interface for all response objects so we don't need to use
* reflection here.
*
* @param res The return Class
* @param error The ServiceError to include in the response
* @return A response object of the given type with the given service error.
* @throws IOException If an error occurs generating the error object.
*/
private <T> T getErrorResponse(Class<T> res, ServiceError error)
throws IOException
{
try {
T ret = res.newInstance();
Method setResponse = res.getMethod("setStatus", ResponseStatus.class);
setResponse.invoke(ret, ResponseStatus.FAILURE);
Method setError = res.getMethod("setError", ServiceError.class);
setError.invoke(ret, error);
return ret;
} catch (Exception e) {
// This shouldn't happen unless programmer error. For instance,
// a result object not containing a Status or Error field.
if (_log.isDebugEnabled()) {
_log.debug("Error generating error response", e);
}
throw new IOException("Error generating Error response");
}
}
private String urlEncode(String s)
throws IOException
{
return URLEncoder.encode(s, "UTF-8");
}
/**
* Issue a GET against the API.
*
* @param path The web service endpoint.
* @param params A Map of key value pairs that are converted into query
* arguments.
* @param resultClass The response object type.
* @return The response object from the operation. This response will be of
* the type given in the resultClass argument.
* @throws IOException If a network error occurs during the request.
*/
<T> T doGet(String path, Map<String, String[]> params, Class<T> resultClass)
throws IOException
{
GetMethod method = new GetMethod();
method.setDoAuthentication(true);
StringBuffer uri = new StringBuffer(path);
if (uri.charAt(uri.length() - 1) != '?') {
uri.append("?");
}
boolean append = false;
for (Map.Entry<String,String[]> e : params.entrySet()) {
for (String val : e.getValue()) {
if (val != null) {
if (append) {
uri.append("&");
}
uri.append(e.getKey()).append("=").append(urlEncode(val));
append = true;
}
}
}
return runMethod(method, uri.toString(), resultClass);
}
/**
* Issue a POST against the API.
*
* @param path The web service endpoint
* @param o The object to POST. This object will be serialized into XML
* prior to being sent.
* @param resultClass The result object type.
* @return The response object from the operation. This response will be
* of the type given in the resultClass argument.
* @throws IOException If a network error occurs during the request.
*/
<T> T doPost(String path, Object o, Class<T> resultClass)
throws IOException
{
PostMethod method = new PostMethod();
method.setDoAuthentication(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
XmlUtil.serialize(o, bos, Boolean.FALSE);
} catch (JAXBException e) {
ServiceError error = new ServiceError();
error.setErrorCode("UnexpectedError");
error.setReasonText("Unable to serialize response");
if (_log.isDebugEnabled()) {
_log.debug("Unable to serialize response", e);
}
return getErrorResponse(resultClass, error);
}
Part[] parts = {
new StringPart("postdata", bos.toString())
};
method.setRequestEntity(new MultipartRequestEntity(parts,
method.getParams()));
return runMethod(method, path, resultClass);
}
private <T> T runMethod(HttpMethodBase method, String uri,
Class<T> resultClass)
throws IOException
{
String protocol = _isSecure ? "https" : "http";
ServiceError error;
URL url = new URL(protocol, _host, _port, uri);
method.setURI(new URI(url.toString(), true));
try {
HttpClient client = new HttpClient();
// Validate user & password inputs
if (_user == null || _user.length() == 0) {
error = new ServiceError();
error.setErrorCode("LoginFailure");
error.setReasonText("User name cannot be null or empty");
return getErrorResponse(resultClass, error);
}
if (_password == null || _password.length() == 0) {
error = new ServiceError();
error.setErrorCode("LoginFailure");
error.setReasonText("Password cannot be null or empty");
return getErrorResponse(resultClass, error);
}
// Set Basic auth creds
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(_user,
_password);
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
// Disable re-tries
DefaultHttpMethodRetryHandler retryhandler =
new DefaultHttpMethodRetryHandler(0, true);
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
retryhandler);
switch (client.executeMethod(method)) {
case 200:
// We only deal with HTTP_OK responses
InputStream is = method.getResponseBodyAsStream();
try {
return XmlUtil.deserialize(resultClass, is);
} catch (JAXBException e) {
error = new ServiceError();
error.setErrorCode("UnexpectedError");
error.setReasonText("Unable to deserialize result");
if (_log.isDebugEnabled()) {
_log.debug("Unable to deserialize result", e);
}
return getErrorResponse(resultClass, error);
}
case 401:
// Unauthorized
error = new ServiceError();
error.setErrorCode("LoginFailure");
error.setReasonText("The given username and password could " +
"not be validated");
return getErrorResponse(resultClass, error);
default:
// Some other server blow up.
error = new ServiceError();
error.setErrorCode("UnexpectedError");
error.setReasonText("An unexpected error occured");
return getErrorResponse(resultClass, error);
}
} catch (SocketException e) {
throw new HttpException("Error issuing request", e);
} finally {
method.releaseConnection();
}
}
}