Search in sources :

Example 76 with PostMethod

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;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 77 with PostMethod

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;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) HashMap(java.util.HashMap) PostMethod(org.apache.commons.httpclient.methods.PostMethod) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SocketTimeoutException(java.net.SocketTimeoutException) Pair(com.cloud.utils.Pair)

Example 78 with PostMethod

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();
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) StringWriter(java.io.StringWriter) PostMethod(org.apache.commons.httpclient.methods.PostMethod) SecurityGroupVmRuleSet(com.cloud.baremetal.networkservice.schema.SecurityGroupVmRuleSet) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 79 with PostMethod

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();
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URI(org.apache.commons.httpclient.URI) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 80 with PostMethod

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;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) ExecutionException(com.cloud.utils.exception.ExecutionException) EasySSLProtocolSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory) HttpClient(org.apache.commons.httpclient.HttpClient) ExecutionException(com.cloud.utils.exception.ExecutionException)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11