|
@@ -1,18 +1,124 @@
|
|
package com.scbfkj.uni.process;
|
|
package com.scbfkj.uni.process;
|
|
|
|
|
|
|
|
+import com.scbfkj.uni.library.DataFormatUtil;
|
|
import com.scbfkj.uni.library.UniReturnUtil;
|
|
import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
|
+import org.apache.http.Header;
|
|
|
|
+import org.apache.http.HttpHost;
|
|
|
|
+import org.apache.http.auth.AuthScope;
|
|
|
|
+import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
|
+import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.elasticsearch.client.Request;
|
|
|
|
+import org.elasticsearch.client.Response;
|
|
|
|
+import org.elasticsearch.client.RestClient;
|
|
|
|
+import org.elasticsearch.client.RestClientBuilder;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
public class Elasticsearch {
|
|
public class Elasticsearch {
|
|
|
|
|
|
- public static Map<String,Object> sendMessage(String connection,String index, List<String> datas){
|
|
|
|
- if(Objects.isNull(datas) || datas.isEmpty() ){
|
|
|
|
|
|
+ private static Map<String, RestClient> restClientMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static Map<String, Object> exec(String connection, List<String> datas) {
|
|
|
|
+ if (Objects.isNull(datas) || datas.isEmpty()) {
|
|
return UniReturnUtil.fail("数据为空");
|
|
return UniReturnUtil.fail("数据为空");
|
|
}
|
|
}
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
+ RestClient restClient = create(connection);
|
|
|
|
+ Map<?, ?> map = DataFormatUtil.toMap(connection);
|
|
|
|
+ Object endpoint = map.get("endpoint");
|
|
|
|
+ if (Objects.isNull(endpoint)) {
|
|
|
|
+ endpoint = "/";
|
|
|
|
+ }
|
|
|
|
+ Object method = map.get("method");
|
|
|
|
+ if (Objects.isNull(method)) {
|
|
|
|
+ method = "GET";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Map<String, String> parametersMap = new HashMap<>();
|
|
|
|
+ Object parameters = map.get("parameters");
|
|
|
|
+ if (Objects.nonNull(parameters)) {
|
|
|
|
+ parametersMap.putAll((Map<String, String>) parameters);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Object finalMethod = method;
|
|
|
|
+ Object finalEndpoint = endpoint;
|
|
|
|
+ List<HashMap<Object, Object>> results = datas.parallelStream().map(data -> {
|
|
|
|
+ Request request = new Request(finalMethod.toString(), finalEndpoint.toString());
|
|
|
|
+ request.addParameters(parametersMap);
|
|
|
|
+ request.setJsonEntity(data);
|
|
|
|
+ return new HashMap<>() {{
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Response response = restClient.performRequest(request);
|
|
|
|
+ put("response", response);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ put("exception", e);
|
|
|
|
+ }
|
|
|
|
+ }};
|
|
|
|
+ }).toList();
|
|
|
|
+ List<Exception> exceptions = results.stream().map(it -> it.get("exception")).filter(Objects::nonNull).map(it -> ((Exception) it)).toList();
|
|
|
|
+ List<Response> responses = results.stream().map(it -> it.get("response")).filter(Objects::nonNull).map(it -> ((Response) it)).toList();
|
|
|
|
+
|
|
|
|
+ if (exceptions.isEmpty()) {
|
|
|
|
+ return UniReturnUtil.success(responses.stream().map(response -> {
|
|
|
|
+ try {
|
|
|
|
+ return EntityUtils.toString(response.getEntity());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }).filter(Objects::nonNull));
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ return UniReturnUtil.fail(exceptions.stream().map(UniReturnUtil::getMessage).collect(Collectors.joining("\n")), responses.stream().map(response -> {
|
|
|
|
+ try {
|
|
|
|
+ return EntityUtils.toString(response.getEntity());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }).filter(Objects::nonNull));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static RestClient create(String connection) {
|
|
|
|
+ RestClient restClient = restClientMap.get(connection);
|
|
|
|
+ if (Objects.nonNull(restClient)) {
|
|
|
|
+ return restClient;
|
|
|
|
+ }
|
|
|
|
+ Map<?, ?> map = DataFormatUtil.toMap(connection);
|
|
|
|
+
|
|
|
|
+ List<String> hosts = (List<String>) map.get("host");
|
|
|
|
+ Object[] list = hosts.stream().map(HttpHost::create).toArray();
|
|
|
|
+
|
|
|
|
+ HttpHost[] httpHosts = (HttpHost[]) list;
|
|
|
|
+ RestClientBuilder builder = RestClient.builder(httpHosts);
|
|
|
|
+ Object username = map.get("username");
|
|
|
|
+ Object password = map.get("password");
|
|
|
|
+ Object token = map.get("token");
|
|
|
|
+ if (Objects.nonNull(username) && Objects.nonNull(password)) {
|
|
|
|
+ builder
|
|
|
|
+ .setHttpClientConfigCallback(httpClientBuilder -> {
|
|
|
|
+ httpClientBuilder.setDefaultCredentialsProvider(new BasicCredentialsProvider() {{
|
|
|
|
+ setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username.toString(), password.toString()));
|
|
|
|
+ }});
|
|
|
|
+ return httpClientBuilder;
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ } else if (Objects.nonNull(token)) {
|
|
|
|
+ builder.setDefaultHeaders(new Header[]{
|
|
|
|
+ new BasicHeader("Authorization", "Bearer " + token)
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ restClient = builder.build();
|
|
|
|
+ restClientMap.put(connection, restClient);
|
|
|
|
+ return restClient;
|
|
}
|
|
}
|
|
}
|
|
}
|