package de.tblsoft.solr.http;

import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Created by tblsoft on 18.03.16.
 */
public class HTTPHelper {

	public static String delete(String url) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpDelete httpPost = new HttpDelete(url);

			CloseableHttpResponse response = httpclient.execute(httpPost);
			StringBuilder responseBuilder = new StringBuilder();

			responseBuilder.append(EntityUtils.toString(response.getEntity()));
			httpclient.close();
			return responseBuilder.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String put(String url, String postString) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPut httpPost = new HttpPut(url);

			if (postString != null) {
				StringEntity entity = new StringEntity(postString, "UTF-8");
				httpPost.setEntity(entity);
				// httpPost.setHeader("Content-Type",contentType);
			}

			CloseableHttpResponse response = httpclient.execute(httpPost);
			StringBuilder responseBuilder = new StringBuilder();

			responseBuilder.append(EntityUtils.toString(response.getEntity()));
			httpclient.close();
			return responseBuilder.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String post(String url, String postString) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpPost httpPost = new HttpPost(url);

			StringEntity entity = new StringEntity(postString, "UTF-8");
			httpPost.setEntity(entity);
			// httpPost.setHeader("Content-Type",contentType);

			CloseableHttpResponse response = httpclient.execute(httpPost);
			StringBuilder responseBuilder = new StringBuilder();

			responseBuilder.append(EntityUtils.toString(response.getEntity()));
			httpclient.close();
			return responseBuilder.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String get(String url) {
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpPost = new HttpGet(url);

			CloseableHttpResponse response = httpclient.execute(httpPost);
			StringBuilder responseBuilder = new StringBuilder();

			responseBuilder.append(EntityUtils.toString(response.getEntity()));
			httpclient.close();
			return responseBuilder.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

    public static int getStatusCode(String url) {
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(httpGet);
            httpclient.close();
            return response.getStatusLine().getStatusCode();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -24,16 +24,99 @@
 .http;%0A%0A
+import java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.InputStream;%0A%0A
 import o
@@ -2913,16 +2913,710 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Store the body of the url in the specified fileName.%0A%09 * %0A%09 * @param url The url where the content is fetched.%0A%09 * @param fileName The fileName where the content is stored.%0A%09 */%0A%09public static void get2File(String url, File fileName) %7B%0A%09%09try %7B%0A%09%09%09CloseableHttpClient httpclient = HttpClients.createDefault();%0A%09%09%09HttpGet httpPost = new HttpGet(url);%0A%0A%09%09%09CloseableHttpResponse response = httpclient.execute(httpPost);%0A%09%09%09InputStream is = response.getEntity().getContent();%0A%09%09%09FileOutputStream fos = new FileOutputStream(fileName);%0A%09%09%09org.apache.commons.io.IOUtils.copy(is, fos);%0A%09%09%09httpclient.close();%0A%09%09%09fos.close();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D%0A%09%7D
 %0A%0A    pu

add the possibility to write a http response payload to a file