use of org.apache.commons.httpclient.HttpMethod in project bamboobsc by billchen198318.
the class CxfServerBean method shutdownOrReloadCallOneSystem.
@SuppressWarnings("unchecked")
public static Map<String, Object> shutdownOrReloadCallOneSystem(HttpServletRequest request, String system, String type) throws ServiceException, Exception {
if (StringUtils.isBlank(system) || StringUtils.isBlank(type)) {
throw new ServiceException(SysMessageUtil.get(GreenStepSysMsgConstants.PARAMS_BLANK));
}
String urlStr = ApplicationSiteUtils.getBasePath(system, request) + "config-services?type=" + type + "&value=" + createParamValue();
logger.info("shutdownOrReloadCallSystem , url=" + urlStr);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(urlStr);
client.executeMethod(method);
byte[] responseBody = method.getResponseBody();
if (null == responseBody) {
throw new Exception("no response!");
}
String content = new String(responseBody, Constants.BASE_ENCODING);
logger.info("shutdownOrReloadCallSystem , system=" + system + " , type=" + type + " , response=" + content);
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> dataMap = null;
try {
dataMap = (Map<String, Object>) mapper.readValue(content, HashMap.class);
} catch (JsonParseException e) {
logger.error(e.getMessage().toString());
} catch (JsonMappingException e) {
logger.error(e.getMessage().toString());
}
if (null == dataMap) {
throw new Exception("response content error!");
}
return dataMap;
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class RegionsApiUtil method makeDomainAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns Domain object on success
* @param region
* @param command
* @param params
* @return
*/
protected static RegionDomain makeDomainAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
XStream xstream = new XStream(new DomDriver());
//Translate response to Domain object
xstream.alias("domain", RegionDomain.class);
xstream.aliasField("id", RegionDomain.class, "uuid");
xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid");
xstream.aliasField("networkdomain", DomainVO.class, "networkDomain");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (RegionDomain) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class RegionsApiUtil method makeUserAccountAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns UserAccount object on success
* @param region
* @param command
* @param params
* @return
*/
protected static UserAccount makeUserAccountAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
XStream xstream = new XStream(new DomDriver());
xstream.alias("useraccount", UserAccountVO.class);
xstream.aliasField("id", UserAccountVO.class, "uuid");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (UserAccountVO) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class RegionsApiUtil method makeAccountAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns Account object on success
* @param region
* @param command
* @param params
* @return
*/
protected static RegionAccount makeAccountAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
//Translate response to Account object
XStream xstream = new XStream(new DomDriver());
xstream.alias("account", RegionAccount.class);
xstream.alias("user", RegionUser.class);
xstream.aliasField("id", RegionAccount.class, "uuid");
xstream.aliasField("name", RegionAccount.class, "accountName");
xstream.aliasField("accounttype", RegionAccount.class, "type");
xstream.aliasField("domainid", RegionAccount.class, "domainUuid");
xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain");
xstream.aliasField("id", RegionUser.class, "uuid");
xstream.aliasField("accountId", RegionUser.class, "accountUuid");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (RegionAccount) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpMethod in project cloudstack by apache.
the class ApiCommand method verifyEvents.
public static boolean verifyEvents(String fileName, String level, String host, String account) {
boolean result = false;
HashMap<String, Integer> expectedEvents = new HashMap<String, Integer>();
HashMap<String, Integer> actualEvents = new HashMap<String, Integer>();
String key = "";
File file = new File(fileName);
if (file.exists()) {
Properties pro = new Properties();
try {
// get expected events
FileInputStream in = new FileInputStream(file);
pro.load(in);
Enumeration<?> en = pro.propertyNames();
while (en.hasMoreElements()) {
key = (String) en.nextElement();
expectedEvents.put(key, Integer.parseInt(pro.getProperty(key)));
}
// get actual events
String url = host + "/?command=listEvents&account=" + account + "&level=" + level + "&domainid=1&pagesize=100";
s_logger.info("Getting events with the following url " + url);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
int responseCode = client.executeMethod(method);
if (responseCode == 200) {
InputStream is = method.getResponseBodyAsStream();
ArrayList<HashMap<String, String>> eventValues = UtilsForTest.parseMulXML(is, new String[] { "event" });
for (int i = 0; i < eventValues.size(); i++) {
HashMap<String, String> element = eventValues.get(i);
if (element.get("level").equals(level)) {
if (actualEvents.containsKey(element.get("type")) == true) {
actualEvents.put(element.get("type"), actualEvents.get(element.get("type")) + 1);
} else {
actualEvents.put(element.get("type"), 1);
}
}
}
}
method.releaseConnection();
// compare actual events with expected events
// compare expected result and actual result
Iterator<?> iterator = expectedEvents.keySet().iterator();
Integer expected;
Integer actual;
int fail = 0;
while (iterator.hasNext()) {
expected = null;
actual = null;
String type = iterator.next().toString();
expected = expectedEvents.get(type);
actual = actualEvents.get(type);
if (actual == null) {
s_logger.error("Event of type " + type + " and level " + level + " is missing in the listEvents response. Expected number of these events is " + expected);
fail++;
} else if (expected.compareTo(actual) != 0) {
fail++;
s_logger.info("Amount of events of " + type + " type and level " + level + " is incorrect. Expected number of these events is " + expected + ", actual number is " + actual);
}
}
if (fail == 0) {
result = true;
}
} catch (Exception ex) {
s_logger.error(ex);
}
} else {
s_logger.info("File " + fileName + " not found");
}
return result;
}
Aggregations