HTTP

360 阅读2分钟
public class GlobalProperties {

   //10 minutes
    public  static final String SCROLL_TIME_VALUE = "10m";
    public static final String INDEX_SETTINGS = "settings";
    public static final String INDEX = "index";
    public static final String NUMBER_OF_SHARDS = "number_of_shards";
    public static final String NUMBER_OF_REPLICAS = "number_of_replicas";
    public static final String CREATION_DATE = "creation_date";
    public static final String VERSION = "version";
    public static final String CREATED = "created";
    public static final String MAPPINGS = "mappings";
    public static final String ALIASES = "aliases";

    public static final String HTTP = "http://";
    public static final String HTTPS = "https://";
    public static final String CAT_INDEX = "/_cat/indices?h=index";
    public static final String STATS = "/_stats";
    public static final String SEPARATOR = "/";
    public static final String ALL = "_all";
    public static final String PRIMARIES = "primaries";
    public static final String INDICES = "indices";
    public static final String TOTAL = "total";
    public static final String DOCS = "docs";
    public static final String COUNT = "count";
    public static final String DELETED = "deleted";
    public static final String STORE = "store";
    public static final String SIZE_IN_BYTES = "size_in_bytes";
    public  static final String UUID="uuid";

    public static final String MAX_RESULT_WINDOW = "max_result_window";
    public static final String SEARCH_FILTER_TOTAL = "filter_path=hits.total.value";
    public static final String SEARCH_FILTER_HITS = "filter_path=hits.hits";
    public static final String SEARCH = "/_search?";
    public static final String SCROLL = "scroll=";
    public static final String SIZE = "size=";
    public static final String SETTINGS = "_settings";


}



public class HttpData {

    private String httpType;

    private CloseableHttpClient httpClient;

    private boolean connectionStatus;

    public String getHttpType() {
        return httpType;
    }

    public void setHttpType(String httpType) {
        this.httpType = httpType;
    }

    public CloseableHttpClient getHttpClient() {
        return httpClient;
    }

    public void setHttpClient(CloseableHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public boolean isConnectionStatus() {
        return connectionStatus;
    }

    public void setConnectionStatus(boolean connectionStatus) {
        this.connectionStatus = connectionStatus;
    }


}
public class ESClientUtils {


    public static HttpData getClient(String url, String userName, String password) {
        HttpData httpData = new HttpData();
        boolean flag = testConnection(GlobalProperties.HTTP, url, userName, password);
        if (flag) {
            httpData.setHttpType(GlobalProperties.HTTP);
            httpData.setConnectionStatus(true);
            httpData.setHttpClient(getHttpClient(userName, password));
            return httpData;
        }
        else {
            httpData.setHttpType(GlobalProperties.HTTPS);
            httpData.setConnectionStatus(true);
            httpData.setHttpClient(getHttpsClient(userName, password));
            return httpData;
        }
    }

    private static boolean testConnection(String httpType, String url, String userName, String password) {
        boolean flag = false;
        CloseableHttpClient httpClient = null;
        HttpGet httpGet = null;
        switch (httpType) {
            case GlobalProperties.HTTP:
                httpClient = getHttpClient(userName, password);
                break;
            case GlobalProperties.HTTPS:
                httpClient = getHttpsClient(userName, password);
                break;
            default:
                break;
        }
        try {
            httpGet = new HttpGet(httpType + url);
            httpClient.execute(httpGet);
        } catch (NoHttpResponseException e) {
            return flag;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("url was not correct ,please check url");
        }
        return true;
    }


    public static CloseableHttpClient getHttpClient(String userName, String password) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
        provider.setCredentials(AuthScope.ANY, credentials);
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setDefaultCredentialsProvider(provider);
        CloseableHttpClient client = builder.build();
        return client;
    }

    public static CloseableHttpClient getHttpsClient(String userName, String password) {
        try {
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslContext, null, null,
                    new NoopHostnameVerifier());
            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("https", sslcsf)
                    .build();

            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(100);
            CredentialsProvider provider = new BasicCredentialsProvider();
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
            provider.setCredentials(AuthScope.ANY, credentials);
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setSSLSocketFactory(sslcsf)
                    .setDefaultCookieStore(new BasicCookieStore())
                    .setDefaultCredentialsProvider(provider)
                    .setConnectionManager(cm).build();
            return httpclient;
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }
}





  private boolean getJsonNode(String httpType, String url, String index, String params, HttpClient client) throws IOException {
        HttpGet httpGet = new HttpGet(httpType + url + index + params);
        HttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        return 200 == statusCode;
    }

    private JsonNode getJsonNodeIndex(String httpType, String url, String indexName, String params, HttpClient client) throws IOException {
        HttpGet httpGet = new HttpGet(httpType + url + "/" + indexName + "/" + params);
        HttpResponse response = client.execute(httpGet);
        HttpEntity httpEntity = response.getEntity();
        ObjectMapper objMapper = new ObjectMapper();
        return objMapper.readTree(EntityUtils.toString(httpEntity, "utf-8"));
    }

    private JsonNode scrollGetAllDocument(String httpType, String host, String indexName, int maxResultWindowNum, HttpClient client) throws IOException {

        HttpGet httpGet = new HttpGet(httpType + host + "/" + indexName + GlobalProperties.SEARCH + GlobalProperties.SEARCH_FILTER_HITS + "&" + GlobalProperties.SCROLL + GlobalProperties.SCROLL_TIME_VALUE + "&" + GlobalProperties.SIZE + maxResultWindowNum);
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        ObjectMapper objMapper = new ObjectMapper();
        JsonNode total = objMapper.readTree(EntityUtils.toString(httpEntity, "utf-8"));
        JsonNode hits = total.get("hits").get("hits");
        return hits;
    }

    private JsonNode searchGetAllDocument(String httpType, String host, String indexName, int index, HttpClient client, int dataSize, String sort) throws IOException {
        HttpPost request = new HttpPost(httpType + host + "/" + indexName + GlobalProperties.SEARCH + GlobalProperties.SEARCH_FILTER_HITS);
        String body = "";
        if (index == 1) {
            body = "{\n" +
                    "    \"size\": " + dataSize + ",\n" +
                    "    \"query\": {\n" +
                    "        \"match_all\": {}\n" +
                    "    },\n" +
                    "    \"sort\": [\n" +
                    "        {\"_id\": \"asc\"}    \n" +
                    "    ]\n" +
                    "}";
        } else {
            body = "{\n" +
                    "    \"size\":" + dataSize + ",\n" +
                    "    \"query\": {\n" +
                    "        \"match_all\": {}\n" +
                    "    },\n" +
                    "    \"search_after\":\n" +
                    "          " + sort + ",\n" +
                    "    \"sort\": [\n" +
                    "        {\"_id\": \"asc\"}    \n" +
                    "    ]\n" +
                    "}";
        }
        request.setEntity(new StringEntity(body, "UTF-8"));
        request.setHeader("content-type", "application/json");

        HttpResponse response = client.execute(request);

        HttpEntity httpEntity = response.getEntity();
        ObjectMapper objMapper = new ObjectMapper();
        JsonNode total = objMapper.readTree(EntityUtils.toString(httpEntity, "utf-8"));
        JsonNode hits = total.get("hits").get("hits");
        return hits;
    }