remove utility classes (#1733)
* remove utility classes * remove httpclient, extend http * move code to core-java
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.http;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class HttpRequestBuilder {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(HttpRequestBuilder.class);
|
||||
|
||||
public HttpResponseWrapper sendRequest(String urlString, String method, Map<String, String> parameters, Map<String, String> properties) throws IOException{
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod(method);
|
||||
if (properties != null) {
|
||||
properties.forEach((key, value) -> con.setRequestProperty(key, value));
|
||||
}
|
||||
if (parameters != null) {
|
||||
con.setDoOutput(true);
|
||||
DataOutputStream out = new DataOutputStream(con.getOutputStream());
|
||||
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
int status = con.getResponseCode();
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
|
||||
responseWrapper.setStatus(status);
|
||||
responseWrapper.setContent(content.toString());
|
||||
|
||||
return responseWrapper;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.http;
|
||||
|
||||
public class HttpResponseWrapper {
|
||||
private int status;
|
||||
private String content;
|
||||
|
||||
public HttpResponseWrapper(){ }
|
||||
|
||||
public HttpResponseWrapper(int status, String content) {
|
||||
super();
|
||||
this.status = status;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.http;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParameterStringBuilder {
|
||||
public static String getParamsString(Map<String, String> params) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
params.forEach((key, value) -> {
|
||||
try {
|
||||
result.append(URLEncoder.encode(key, "UTF-8"));
|
||||
result.append("=");
|
||||
result.append(URLEncoder.encode(value, "UTF-8"));
|
||||
result.append("&");
|
||||
} catch (UnsupportedEncodingException exc) {
|
||||
}
|
||||
});
|
||||
|
||||
String resultString = result.toString();
|
||||
if (resultString.length() > 0) {
|
||||
resultString = resultString.substring(0, resultString.length() - 1);
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import com.baeldung.http.HttpResponseWrapper;
|
||||
import com.baeldung.http.ParameterStringBuilder;
|
||||
|
||||
public class HttpClientRequestBuilder {
|
||||
|
||||
public HttpResponseWrapper sendGetRequest(String url, Map<String, String> parameters) {
|
||||
HttpClient client = HttpClientBuilder.create()
|
||||
.build();
|
||||
if (parameters != null) {
|
||||
url += "?" + ParameterStringBuilder.getParamsString(parameters);
|
||||
}
|
||||
HttpGet request = new HttpGet(url);
|
||||
try {
|
||||
HttpResponse response = client.execute(request);
|
||||
|
||||
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
|
||||
responseWrapper.setStatus(response.getStatusLine()
|
||||
.getStatusCode());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
|
||||
.getContent()));
|
||||
|
||||
String line = "", content = "";
|
||||
while ((line = in.readLine()) != null) {
|
||||
content += line;
|
||||
}
|
||||
responseWrapper.setContent(content);
|
||||
return responseWrapper;
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public HttpResponseWrapper sendPostRequestWithParameters(String url, Map<String, String> parameters) {
|
||||
HttpClient client = HttpClientBuilder.create()
|
||||
.build();
|
||||
HttpPost request = new HttpPost(url);
|
||||
|
||||
try {
|
||||
if (parameters != null) {
|
||||
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
||||
parameters.forEach((key, value) -> nameValuePairs.add(new BasicNameValuePair(key, value)));
|
||||
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
||||
}
|
||||
|
||||
HttpResponse response = client.execute(request);
|
||||
|
||||
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
|
||||
responseWrapper.setStatus(response.getStatusLine()
|
||||
.getStatusCode());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
|
||||
.getContent()));
|
||||
|
||||
String line = "", content = "";
|
||||
while ((line = in.readLine()) != null) {
|
||||
content += line;
|
||||
}
|
||||
responseWrapper.setContent(content);
|
||||
return responseWrapper;
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public HttpResponseWrapper sendPostRequestWithJson(String url, String json) {
|
||||
HttpClient client = HttpClientBuilder.create()
|
||||
.build();
|
||||
HttpPost request = new HttpPost(url);
|
||||
|
||||
try {
|
||||
request.addHeader("Content-Type", "application/json");
|
||||
request.setEntity(new StringEntity(json));
|
||||
|
||||
HttpResponse response = client.execute(request);
|
||||
|
||||
HttpResponseWrapper responseWrapper = new HttpResponseWrapper();
|
||||
responseWrapper.setStatus(response.getStatusLine()
|
||||
.getStatusCode());
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
|
||||
.getContent()));
|
||||
|
||||
String line = "", content = "";
|
||||
while ((line = in.readLine()) != null) {
|
||||
content += line;
|
||||
}
|
||||
responseWrapper.setContent(content);
|
||||
return responseWrapper;
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user