Skip to content

Commit

Permalink
httputil小改 #17
Browse files Browse the repository at this point in the history
  • Loading branch information
chembohuang committed Feb 15, 2019
1 parent 6b1320f commit 2129007
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
39 changes: 25 additions & 14 deletions pallas-core/src/main/java/com/vip/pallas/utils/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
Expand All @@ -38,7 +39,7 @@ public class HttpUtil {
private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

@SuppressWarnings("rawtypes")
private ResponseHandler<Map> urlCallback = (HttpResponse response) -> {
private static ResponseHandler<Map> urlCallback = (HttpResponse response) -> {
Map<String, String> ret = null;
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
Expand All @@ -63,7 +64,7 @@ public class HttpUtil {
};

@SuppressWarnings("rawtypes")
private ResponseHandler<Map> jsonCallback = (HttpResponse response) -> {
private static ResponseHandler<Map> jsonCallback = (HttpResponse response) -> {
Map<String, Object> ret = null;
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
Expand All @@ -83,25 +84,35 @@ public class HttpUtil {
return ret;
};

public Map<String, Object> httpGet(String url, Map<String, Object> params, boolean json) throws Exception {
Set<String> keySet = params.keySet();
public static Map<String, Object> httpGet(String url, Map<String, Object> params, boolean json) throws Exception {
return httpGet(url, params, json, null);
}

public static Map<String, Object> httpGet(String url, Map<String, Object> params, boolean json, Header[] headers)
throws Exception {
String getUrl = url;
if (url.lastIndexOf('?') <= -1) {
getUrl += "?";
}
if (params != null) {
Set<String> keySet = params.keySet();

boolean flag = false;
for (String key : keySet) {
if (flag) {
getUrl += "&";
if (url.lastIndexOf('?') <= -1) {
getUrl += "?";
}

boolean flag = false;
for (String key : keySet) {
if (flag) {
getUrl += "&";
}
getUrl += key + "=";
getUrl += ObjectMapTool.getString(params, key);
flag = true;
}
getUrl += key + "=";
getUrl += ObjectMapTool.getString(params, key);
flag = true;
}
HttpGet request = new HttpGet(new URI(getUrl));
HttpClient httpClient = HttpClientUtil.getHttpClient();
if (headers != null) {
request.setHeaders(headers);
}
return httpClient.execute(request, json ? jsonCallback : urlCallback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

package com.vip.pallas.test.core;

import com.vip.pallas.test.base.BaseEsTest;
import com.vip.pallas.utils.HttpUtil;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import com.vip.pallas.test.base.BaseEsTest;
import com.vip.pallas.utils.HttpUtil;

/**
* Created by owen on 12/7/2017.
Expand All @@ -32,13 +33,12 @@ public class HttpUtilTest extends BaseEsTest {

@Test
public void testHttpGet() throws Exception {
HttpUtil util = new HttpUtil();
Map<String, Object> params = new HashMap<String, Object>();

Map<String, Object> res = util.httpGet("http://127.0.0.1:9200/_cat/indices", params, false);
Map<String, Object> res = HttpUtil.httpGet("http://127.0.0.1:9200/_cat/indices", params, false);
Assert.assertEquals("{}", res.toString());

res = util.httpGet("http://127.0.0.1:9200/_cat/indices", params, true);
res = HttpUtil.httpGet("http://127.0.0.1:9200/_cat/indices", params, true);
Assert.assertTrue(res.size() > 0);
}
}

0 comments on commit 2129007

Please sign in to comment.