Search in sources :

Example 66 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project CloudStack-archive by CloudStack-extras.

the class StressTestDirectAttach method queryAsyncJobResult.

public static Element queryAsyncJobResult(String host, InputStream inputStream) {
    Element returnBody = null;
    Map<String, String> values = getSingleValueFromXML(inputStream, new String[] { "jobid" });
    String jobId = values.get("jobid");
    if (jobId == null) {
        s_logger.error("Unable to get a jobId");
        return null;
    }
    //s_logger.info("Job id is " + jobId); 
    String resultUrl = host + "?command=queryAsyncJobResult&jobid=" + jobId;
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(resultUrl);
    while (true) {
        try {
            client.executeMethod(method);
            //s_logger.info("Method is executed successfully. Following url was sent " + resultUrl);
            InputStream is = method.getResponseBodyAsStream();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(is);
            returnBody = doc.getDocumentElement();
            doc.getDocumentElement().normalize();
            Element jobStatusTag = (Element) returnBody.getElementsByTagName("jobstatus").item(0);
            String jobStatus = jobStatusTag.getTextContent();
            if (jobStatus.equals("0")) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            } else {
                break;
            }
        } catch (Exception ex) {
            s_logger.error(ex);
        }
    }
    return returnBody;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Document(org.w3c.dom.Document) HttpMethod(org.apache.commons.httpclient.HttpMethod) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 67 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project CloudStack-archive by CloudStack-extras.

the class StressTestDirectAttach method executeCleanup.

private static int executeCleanup(String server, String developerServer, String username) throws HttpException, IOException {
    // test steps:
    // - get user
    // - delete user
    // -----------------------------
    // GET USER
    // -----------------------------
    String userId = _userId.get().toString();
    String encodedUserId = URLEncoder.encode(userId, "UTF-8");
    String url = server + "?command=listUsers&id=" + encodedUserId;
    s_logger.info("Cleaning up resources for user: " + userId + " with url " + url);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    s_logger.info("get user response code: " + responseCode);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> userInfo = getSingleValueFromXML(is, new String[] { "username", "id", "account" });
        if (!username.equals(userInfo.get("username"))) {
            s_logger.error("get user failed to retrieve requested user, aborting cleanup test" + ". Following URL was sent: " + url);
            return -1;
        }
    } else {
        s_logger.error("get user failed with error code: " + responseCode + ". Following URL was sent: " + url);
        return responseCode;
    }
    // -----------------------------
    // UPDATE USER
    // -----------------------------
    {
        url = server + "?command=updateUser&id=" + userId + "&firstname=delete&lastname=me";
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        s_logger.info("update user response code: " + responseCode);
        if (responseCode == 200) {
            InputStream is = method.getResponseBodyAsStream();
            Map<String, String> success = getSingleValueFromXML(is, new String[] { "success" });
            s_logger.info("update user..success? " + success.get("success"));
        } else {
            s_logger.error("update user failed with error code: " + responseCode + ". Following URL was sent: " + url);
            return responseCode;
        }
    }
    // -----------------------------
    // Execute reboot/stop/start commands for the VMs before deleting the account - made to exercise xen
    // -----------------------------
    //Reboot centos VM
    String encodedApiKey = URLEncoder.encode(_apiKey.get(), "UTF-8");
    String requestToSign = "apikey=" + encodedApiKey + "&command=rebootVirtualMachine&id=" + _linuxVmId.get();
    requestToSign = requestToSign.toLowerCase();
    String signature = signRequest(requestToSign, _secretKey.get());
    String encodedSignature = URLEncoder.encode(signature, "UTF-8");
    url = developerServer + "?command=rebootVirtualMachine&id=" + _linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
    client = new HttpClient();
    method = new GetMethod(url);
    responseCode = client.executeMethod(method);
    s_logger.info("Reboot VM response code: " + responseCode);
    if (responseCode == 200) {
        InputStream input = method.getResponseBodyAsStream();
        Element el = queryAsyncJobResult(server, input);
        Map<String, String> success = getSingleValueFromXML(el, new String[] { "success" });
        s_logger.info("VM was rebooted with the status: " + success.get("success"));
    } else {
        s_logger.error(" VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
        return responseCode;
    }
    //Stop centos VM
    requestToSign = "apikey=" + encodedApiKey + "&command=stopVirtualMachine&id=" + _linuxVmId.get();
    requestToSign = requestToSign.toLowerCase();
    signature = signRequest(requestToSign, _secretKey.get());
    encodedSignature = URLEncoder.encode(signature, "UTF-8");
    url = developerServer + "?command=stopVirtualMachine&id=" + _linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
    client = new HttpClient();
    method = new GetMethod(url);
    responseCode = client.executeMethod(method);
    s_logger.info("Stop VM response code: " + responseCode);
    if (responseCode == 200) {
        InputStream input = method.getResponseBodyAsStream();
        Element el = queryAsyncJobResult(server, input);
        Map<String, String> success = getSingleValueFromXML(el, new String[] { "success" });
        s_logger.info("VM was stopped with the status: " + success.get("success"));
    } else {
        s_logger.error("Stop VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
        return responseCode;
    }
    //Start centos VM
    requestToSign = "apikey=" + encodedApiKey + "&command=startVirtualMachine&id=" + _linuxVmId.get();
    requestToSign = requestToSign.toLowerCase();
    signature = signRequest(requestToSign, _secretKey.get());
    encodedSignature = URLEncoder.encode(signature, "UTF-8");
    url = developerServer + "?command=startVirtualMachine&id=" + _linuxVmId.get() + "&apikey=" + encodedApiKey + "&signature=" + encodedSignature;
    client = new HttpClient();
    method = new GetMethod(url);
    responseCode = client.executeMethod(method);
    s_logger.info("Start VM response code: " + responseCode);
    if (responseCode == 200) {
        InputStream input = method.getResponseBodyAsStream();
        Element el = queryAsyncJobResult(server, input);
        Map<String, String> success = getSingleValueFromXML(el, new String[] { "id" });
        if (success.get("id") == null) {
            s_logger.info("Start linux vm response code: 401");
            return 401;
        } else {
            s_logger.info("Start vm response code: " + responseCode);
        }
        s_logger.info("VM was started with the status: " + success.get("success"));
    } else {
        s_logger.error("Start VM test failed with error code: " + responseCode + ". Following URL was sent: " + url);
        return responseCode;
    }
    ////		// -----------------------------
    ////		// DISABLE USER
    ////		// -----------------------------
    //		{
    //			url = server + "?command=disableUser&id=" + userId;
    //			client = new HttpClient();
    //			method = new GetMethod(url);
    //			responseCode = client.executeMethod(method);
    //			s_logger.info("disable user response code: " + responseCode);
    //			if (responseCode == 200) {
    //				InputStream input = method.getResponseBodyAsStream();
    //				Element el = queryAsyncJobResult(server, input);
    //				s_logger
    //						.info("Disabled user successfully");
    //			} else  {
    //				s_logger.error("disable user failed with error code: " + responseCode + ". Following URL was sent: " + url);
    //				return responseCode;
    //			} 
    //		}
    // -----------------------------
    // DELETE USER
    // -----------------------------
    {
        url = server + "?command=deleteUser&id=" + userId;
        client = new HttpClient();
        method = new GetMethod(url);
        responseCode = client.executeMethod(method);
        s_logger.info("delete user response code: " + responseCode);
        if (responseCode == 200) {
            InputStream input = method.getResponseBodyAsStream();
            Element el = queryAsyncJobResult(server, input);
            s_logger.info("Deleted user successfully");
        } else {
            s_logger.error("delete user failed with error code: " + responseCode + ". Following URL was sent: " + url);
            return responseCode;
        }
    }
    return responseCode;
}
Also used : InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) Element(org.w3c.dom.Element) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 68 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project CloudStack-archive by CloudStack-extras.

the class User method launchUser.

public void launchUser() throws IOException {
    String encodedUsername = URLEncoder.encode(this.getUserName(), "UTF-8");
    this.encryptedPassword = TestClientWithAPI.createMD5Password(this.getPassword());
    String encodedPassword = URLEncoder.encode(this.encryptedPassword, "UTF-8");
    String url = this.server + "?command=createUser&username=" + encodedUsername + "&password=" + encodedPassword + "&firstname=Test&lastname=Test&email=alena@vmops.com&domainId=1";
    String userIdStr = null;
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> userIdValues = TestClientWithAPI.getSingleValueFromXML(is, new String[] { "id" });
        userIdStr = userIdValues.get("id");
        if ((userIdStr != null) && (Long.parseLong(userIdStr) != -1)) {
            this.setUserId(userIdStr);
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 69 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project CloudStack-archive by CloudStack-extras.

the class User method retrievePublicIp.

public void retrievePublicIp(long zoneId) throws IOException {
    String encodedApiKey = URLEncoder.encode(this.apiKey, "UTF-8");
    String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
    String requestToSign = "apiKey=" + encodedApiKey + "&command=associateIpAddress" + "&zoneId=" + encodedZoneId;
    requestToSign = requestToSign.toLowerCase();
    String signature = TestClientWithAPI.signRequest(requestToSign, this.secretKey);
    String encodedSignature = URLEncoder.encode(signature, "UTF-8");
    String url = this.developerServer + "?command=associateIpAddress" + "&apiKey=" + encodedApiKey + "&zoneId=" + encodedZoneId + "&signature=" + encodedSignature;
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> values = TestClientWithAPI.getSingleValueFromXML(is, new String[] { "ipaddress" });
        this.getPublicIp().add(values.get("ipaddress"));
        s_logger.info("Ip address is " + values.get("ipaddress"));
    } else if (responseCode == 500) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> errorInfo = TestClientWithAPI.getSingleValueFromXML(is, new String[] { "errorcode", "description" });
        s_logger.error("associate ip test failed with errorCode: " + errorInfo.get("errorCode") + " and description: " + errorInfo.get("description"));
    } else {
        s_logger.error("internal error processing request: " + method.getStatusText());
    }
}
Also used : InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 70 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project CloudStack-archive by CloudStack-extras.

the class VirtualMachine method deployVM.

public void deployVM(long zoneId, long serviceOfferingId, long templateId, String server, String apiKey, String secretKey) throws IOException {
    String encodedZoneId = URLEncoder.encode("" + zoneId, "UTF-8");
    String encodedServiceOfferingId = URLEncoder.encode("" + serviceOfferingId, "UTF-8");
    String encodedTemplateId = URLEncoder.encode("" + templateId, "UTF-8");
    String encodedApiKey = URLEncoder.encode(apiKey, "UTF-8");
    String requestToSign = "apiKey=" + encodedApiKey + "&command=deployVirtualMachine&serviceOfferingId=" + encodedServiceOfferingId + "&templateId=" + encodedTemplateId + "&zoneId=" + encodedZoneId;
    requestToSign = requestToSign.toLowerCase();
    String signature = TestClientWithAPI.signRequest(requestToSign, secretKey);
    String encodedSignature = URLEncoder.encode(signature, "UTF-8");
    String url = server + "?command=deployVirtualMachine" + "&zoneId=" + encodedZoneId + "&serviceOfferingId=" + encodedServiceOfferingId + "&templateId=" + encodedTemplateId + "&apiKey=" + encodedApiKey + "&signature=" + encodedSignature;
    s_logger.info("Sending this request to deploy a VM: " + url);
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    int responseCode = client.executeMethod(method);
    s_logger.info("deploy linux vm response code: " + responseCode);
    if (responseCode == 200) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> values = TestClientWithAPI.getSingleValueFromXML(is, new String[] { "id", "ipaddress" });
        long linuxVMId = Long.parseLong(values.get("id"));
        s_logger.info("got linux virtual machine id: " + linuxVMId);
        this.setPrivateIp(values.get("ipaddress"));
    } else if (responseCode == 500) {
        InputStream is = method.getResponseBodyAsStream();
        Map<String, String> errorInfo = TestClientWithAPI.getSingleValueFromXML(is, new String[] { "errorcode", "description" });
        s_logger.error("deploy linux vm test failed with errorCode: " + errorInfo.get("errorCode") + " and description: " + errorInfo.get("description"));
    } else {
        s_logger.error("internal error processing request: " + method.getStatusText());
    }
}
Also used : InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17