Example usage for org.apache.http.client.methods HttpRequestBase getRequestLine

List of usage examples for org.apache.http.client.methods HttpRequestBase getRequestLine

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpRequestBase getRequestLine.

Prototype

public RequestLine getRequestLine() 

Source Link

Usage

From source file:org.jenkinsci.plugins.skytap.SkytapUtils.java

/**
 * Utility method to execute any type of http request (except delete), to
 * catch any exceptions thrown and return the response string.
 * //from   w w  w  .j  a v a2 s .c  om
 * @param hr
 * @return
 * @throws SkytapException
 * @throws IOException
 * @throws ParseException
 */
public static String executeHttpRequest(HttpRequestBase hr) throws SkytapException {

    boolean retryHttpRequest = true;
    int retryCount = 1;
    String responseString = "";
    while (retryHttpRequest == true) {
        HttpClient httpclient = new DefaultHttpClient();
        //
        // Set timeouts for httpclient requests to 60 seconds
        //
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 60000);
        HttpConnectionParams.setSoTimeout(httpclient.getParams(), 60000);
        //
        responseString = "";
        HttpResponse response = null;
        try {
            Date myDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
            String myDateString = sdf.format(myDate);

            JenkinsLogger.log(myDateString + "\n" + "Executing Request: " + hr.getRequestLine());
            response = httpclient.execute(hr);

            String responseStatusLine = response.getStatusLine().toString();
            if (responseStatusLine.contains("423 Locked")) {
                retryCount = retryCount + 1;
                if (retryCount > 5) {
                    retryHttpRequest = false;
                    JenkinsLogger.error("Object busy too long - giving up.");
                } else {
                    JenkinsLogger.log("Object busy - Retrying...");
                    try {
                        Thread.sleep(15000);
                    } catch (InterruptedException e1) {
                        JenkinsLogger.error(e1.getMessage());
                    }
                }
            } else if (responseStatusLine.contains("409 Conflict")) {

                throw new SkytapException(responseStatusLine);

            } else {

                JenkinsLogger.log(response.getStatusLine().toString());
                HttpEntity entity = response.getEntity();
                responseString = EntityUtils.toString(entity, "UTF-8");
                retryHttpRequest = false;
            }

        } catch (HttpResponseException e) {
            retryHttpRequest = false;
            JenkinsLogger.error("HTTP Response Code: " + e.getStatusCode());

        } catch (ParseException e) {
            retryHttpRequest = false;
            JenkinsLogger.error(e.getMessage());

        } catch (InterruptedIOException e) {
            Date myDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
            String myDateString = sdf.format(myDate);

            retryCount = retryCount + 1;
            if (retryCount > 5) {
                retryHttpRequest = false;
                JenkinsLogger.error("API Timeout - giving up. " + e.getMessage());
            } else {
                JenkinsLogger.log(myDateString + "\n" + e.getMessage() + "\n" + "API Timeout - Retrying...");
            }
        } catch (IOException e) {
            retryHttpRequest = false;
            JenkinsLogger.error(e.getMessage());
        } finally {
            if (response != null) {
                // response will be null if this is a timeout retry
                HttpEntity entity = response.getEntity();
                try {
                    responseString = EntityUtils.toString(entity, "UTF-8");
                } catch (IOException e) {
                    // JenkinsLogger.error(e.getMessage());
                }
            }

            httpclient.getConnectionManager().shutdown();
        }
    }

    return responseString;

}

From source file:org.rapidoid.http.HttpClient.java

public Future<byte[]> request(String verb, String uri, Map<String, String> headers, Map<String, String> data,
        Map<String, String> files, byte[] body, String contentType, Callback<byte[]> callback,
        boolean fullResponse) {

    headers = U.safe(headers);/*from  www  .  j  ava  2 s.c  om*/
    data = U.safe(data);
    files = U.safe(files);

    HttpRequestBase req;
    boolean canHaveBody = false;

    if ("GET".equalsIgnoreCase(verb)) {
        req = new HttpGet(uri);
    } else if ("DELETE".equalsIgnoreCase(verb)) {
        req = new HttpDelete(uri);
    } else if ("OPTIONS".equalsIgnoreCase(verb)) {
        req = new HttpOptions(uri);
    } else if ("HEAD".equalsIgnoreCase(verb)) {
        req = new HttpHead(uri);
    } else if ("TRACE".equalsIgnoreCase(verb)) {
        req = new HttpTrace(uri);
    } else if ("POST".equalsIgnoreCase(verb)) {
        req = new HttpPost(uri);
        canHaveBody = true;
    } else if ("PUT".equalsIgnoreCase(verb)) {
        req = new HttpPut(uri);
        canHaveBody = true;
    } else if ("PATCH".equalsIgnoreCase(verb)) {
        req = new HttpPatch(uri);
        canHaveBody = true;
    } else {
        throw U.illegalArg("Illegal HTTP verb: " + verb);
    }

    for (Entry<String, String> e : headers.entrySet()) {
        req.addHeader(e.getKey(), e.getValue());
    }

    if (canHaveBody) {
        HttpEntityEnclosingRequestBase entityEnclosingReq = (HttpEntityEnclosingRequestBase) req;

        if (body != null) {

            NByteArrayEntity entity = new NByteArrayEntity(body);

            if (contentType != null) {
                entity.setContentType(contentType);
            }

            entityEnclosingReq.setEntity(entity);
        } else {

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            for (Entry<String, String> entry : files.entrySet()) {
                String filename = entry.getValue();
                File file = IO.file(filename);
                builder = builder.addBinaryBody(entry.getKey(), file, ContentType.DEFAULT_BINARY, filename);
            }

            for (Entry<String, String> entry : data.entrySet()) {
                builder = builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.DEFAULT_TEXT);
            }

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                builder.build().writeTo(stream);
            } catch (IOException e) {
                throw U.rte(e);
            }

            byte[] bytes = stream.toByteArray();
            NByteArrayEntity entity = new NByteArrayEntity(bytes, ContentType.MULTIPART_FORM_DATA);

            entityEnclosingReq.setEntity(entity);
        }
    }

    Log.debug("Starting HTTP request", "request", req.getRequestLine());

    return execute(client, req, callback, fullResponse);
}