eu.eco2clouds.accounting.testbedclient.ClientHC.java Source code

Java tutorial

Introduction

Here is the source code for eu.eco2clouds.accounting.testbedclient.ClientHC.java

Source

package eu.eco2clouds.accounting.testbedclient;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import eu.eco2clouds.accounting.datamodel.Testbed;

/**
 * 
 * Copyright 2014 ATOS SPAIN S.A. 
 * 
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author: David Garcia Perez. Atos Research and Innovation, Atos SPAIN SA
 * e-mail david.garciaperez@atos.net
 *
 * HTTP Client to access to the OpenNebula host information provided
 * by each Testbed in BonFIRE
 */
public class ClientHC implements Client {
    private static Logger logger = Logger.getLogger(ClientHC.class);

    /**
     * Connects to an testbed URL to get the XML representations of Host Info
     * And it returns it as a null
     * @param testbed object representing the testbed from which we want to know the host info
     * @return A XML string with the Host info, if there was any HTTP error, it returns empty String... 
     */
    public String getHostInfo(Testbed testbed) {
        // Create an instance of HttpClient.
        HttpClient client = new HttpClient();

        // Create a method instance.
        GetMethod method = new GetMethod(testbed.getUrl());

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        String response = "";

        try {
            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) { //TODO test for this case... 
                logger.warn("Get host information of testbed: " + testbed.getName() + " failed: "
                        + method.getStatusLine());
            } else {
                // Read the response body.
                byte[] responseBody = method.getResponseBody();
                response = new String(responseBody);
            }

        } catch (HttpException e) {
            logger.warn("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            logger.warn("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Release the connection.
            method.releaseConnection();
        }

        return response;
    }
}