use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class SecurityGroupHttpClient method echo.
public boolean echo(String agentIp, long l, long m) {
boolean ret = false;
int count = 1;
while (true) {
try {
Thread.sleep(m);
count++;
} catch (InterruptedException e1) {
logger.warn("", e1);
break;
}
PostMethod post = new PostMethod(String.format("http://%s:%s/", agentIp, getPort()));
try {
post.addRequestHeader("command", "echo");
if (httpClient.executeMethod(post) != 200) {
logger.debug(String.format("echoing baremetal security group agent on %s got error: %s", agentIp, post.getResponseBodyAsString()));
} else {
ret = true;
}
break;
} catch (Exception e) {
if (count * m >= l) {
logger.debug(String.format("ping security group agent on vm[%s] timeout after %s minutes, starting vm failed, count=%s", agentIp, TimeUnit.MILLISECONDS.toSeconds(l), count));
break;
} else {
logger.debug(String.format("Having pinged security group agent on vm[%s] %s times, continue to wait...", agentIp, count));
}
} finally {
if (post != null) {
post.releaseConnection();
}
}
}
return ret;
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class SecurityGroupHttpClient method sync.
public HashMap<String, Pair<Long, Long>> sync(String vmName, Long vmId, String agentIp) {
HashMap<String, Pair<Long, Long>> states = new HashMap<String, Pair<Long, Long>>();
PostMethod post = new PostMethod(String.format("http://%s:%s/", agentIp, getPort()));
try {
post.addRequestHeader("command", "sync");
if (httpClient.executeMethod(post) != 200) {
logger.debug(String.format("echoing baremetal security group agent on %s got error: %s", agentIp, post.getResponseBodyAsString()));
} else {
String res = post.getResponseBodyAsString();
// res = ';'.join([vmName, vmId, seqno])
String[] rulelogs = res.split(",");
if (rulelogs.length != 6) {
logger.debug(String.format("host[%s] returns invalid security group sync document[%s], reset rules", agentIp, res));
states.put(vmName, new Pair<Long, Long>(vmId, -1L));
return states;
}
Pair<Long, Long> p = new Pair<Long, Long>(Long.valueOf(rulelogs[1]), Long.valueOf(rulelogs[5]));
states.put(rulelogs[0], p);
return states;
}
} catch (SocketTimeoutException se) {
logger.warn(String.format("unable to sync security group rules on host[%s], %s", agentIp, se.getMessage()));
} catch (Exception e) {
logger.warn(String.format("unable to sync security group rules on host[%s]", agentIp), e);
} finally {
if (post != null) {
post.releaseConnection();
}
}
return states;
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class SecurityGroupHttpClient method call.
public SecurityGroupRuleAnswer call(String agentIp, SecurityGroupRulesCmd cmd) {
PostMethod post = new PostMethod(String.format("http://%s:%s", agentIp, getPort()));
try {
SecurityGroupVmRuleSet rset = new SecurityGroupVmRuleSet();
rset.getEgressRules().addAll(generateRules(cmd.getEgressRuleSet()));
rset.getIngressRules().addAll(generateRules(cmd.getIngressRuleSet()));
rset.setVmName(cmd.getVmName());
rset.setVmIp(cmd.getGuestIp());
rset.setVmMac(cmd.getGuestMac());
rset.setVmId(cmd.getVmId());
rset.setSignature(cmd.getSignature());
rset.setSequenceNumber(cmd.getSeqNum());
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(rset, writer);
String xmlContents = writer.toString();
logger.debug(xmlContents);
post.addRequestHeader("command", "set_rules");
StringRequestEntity entity = new StringRequestEntity(xmlContents);
post.setRequestEntity(entity);
if (httpClient.executeMethod(post) != 200) {
return new SecurityGroupRuleAnswer(cmd, false, post.getResponseBodyAsString());
} else {
return new SecurityGroupRuleAnswer(cmd);
}
} catch (Exception e) {
return new SecurityGroupRuleAnswer(cmd, false, e.getMessage());
} finally {
if (post != null) {
post.releaseConnection();
}
}
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class UcsHttpClient method call.
public String call(String xml) {
PostMethod post = new PostMethod(url);
post.setRequestEntity(new StringRequestEntity(xml));
post.setRequestHeader("Content-type", "text/xml");
//post.setFollowRedirects(true);
try {
int result = client.executeMethod(post);
if (result == 302) {
// Handle HTTPS redirect
// Ideal way might be to configure from add manager API
// for using either HTTP / HTTPS
// Allow only one level of redirect
String redirectLocation;
Header locationHeader = post.getResponseHeader("location");
if (locationHeader != null) {
redirectLocation = locationHeader.getValue();
} else {
throw new CloudRuntimeException("Call failed: Bad redirect from UCS Manager");
}
post.setURI(new URI(redirectLocation));
result = client.executeMethod(post);
}
// Check for errors
if (result != 200) {
throw new CloudRuntimeException("Call failed: " + post.getResponseBodyAsString());
}
String res = post.getResponseBodyAsString();
if (res.contains("errorCode")) {
String err = String.format("ucs call failed:\nsubmitted doc:%s\nresponse:%s\n", xml, res);
throw new CloudRuntimeException(err);
}
return res;
} catch (Exception e) {
throw new CloudRuntimeException(e.getMessage(), e);
} finally {
post.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.PostMethod in project cloudstack by apache.
the class CiscoVnmcConnectionImpl method sendRequest.
private String sendRequest(String service, String xmlRequest) throws ExecutionException {
HttpClient client = new HttpClient();
String response = null;
PostMethod method = new PostMethod("/xmlIM/" + service);
method.setRequestBody(xmlRequest);
try {
org.apache.commons.httpclient.protocol.Protocol myhttps = new org.apache.commons.httpclient.protocol.Protocol("https", new EasySSLProtocolSocketFactory(), 443);
client.getHostConfiguration().setHost(_ip, 443, myhttps);
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Error code : " + statusCode);
}
response = method.getResponseBodyAsString();
} catch (Exception e) {
System.out.println(e.getMessage());
throw new ExecutionException(e.getMessage());
}
System.out.println(response);
return response;
}
Aggregations