coolmapplugin.util.HTTPRequestUtil.java Source code

Java tutorial

Introduction

Here is the source code for coolmapplugin.util.HTTPRequestUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package coolmapplugin.util;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author Keqiang Li
 */
public class HTTPRequestUtil {

    public static String executePost(String targetURL, String jsonBody) {

        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

            HttpPost request = new HttpPost(targetURL);

            StringEntity params = new StringEntity(jsonBody);
            request.addHeader("content-type", "application/json");
            request.setEntity(params);

            HttpResponse result = httpClient.execute(request);

            HttpEntity entity = result.getEntity();
            if (entity == null)
                return null;

            String jsonResult = EntityUtils.toString(result.getEntity(), "UTF-8");

            return jsonResult;

        } catch (IOException e) {
            return null;
        }
    }

    public static boolean executePut(String targetURL, String jsonBody) {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

            HttpPut request = new HttpPut(targetURL);

            if (jsonBody != null && !jsonBody.isEmpty()) {
                StringEntity params = new StringEntity(jsonBody);
                request.addHeader("content-type", "application/json");
                request.setEntity(params);
            }

            HttpResponse result = httpClient.execute(request);

            if (result.getStatusLine().getStatusCode() == 412 || result.getStatusLine().getStatusCode() == 500) {
                return false;
            }

        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public static String executeGet(String targetURL) {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

            HttpGet request = new HttpGet(targetURL);

            HttpResponse result = httpClient.execute(request);

            // TODO if there exists any other 20*
            if (result.getStatusLine().getStatusCode() != 200) {
                return null;
            }

            String jsonResult = EntityUtils.toString(result.getEntity(), "UTF-8");

            return jsonResult;

        } catch (IOException e) {
            return null;
        }
    }

    public static boolean executeDelete(String targetURL) {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

            HttpDelete request = new HttpDelete(targetURL);

            HttpResponse result = httpClient.execute(request);

            if (result.getStatusLine().getStatusCode() == 500) {
                return false;
            }

        } catch (IOException e) {
            return false;
        }

        return true;
    }

}