|
@@ -0,0 +1,55 @@
|
|
|
+package com.tzld.longarticle.recommend.server.remote;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.StatusLine;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ArticleSensitiveRemoteService {
|
|
|
+
|
|
|
+ private final CloseableHttpClient client = HttpPoolFactory.defaultPool();
|
|
|
+ private static final String articleSensitiveUrl = "http://192.168.100.31:8177/sensitive/is_sensitive";
|
|
|
+
|
|
|
+ public Boolean articleSensitive(String title) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ Boolean isSensitive = false;
|
|
|
+ JSONObject bodyParam = new JSONObject();
|
|
|
+ bodyParam.put("text", title);
|
|
|
+ try {
|
|
|
+ HttpPost httpPost = new HttpPost(articleSensitiveUrl);
|
|
|
+ StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ StatusLine statusLine = response.getStatusLine();
|
|
|
+ if (statusLine.getStatusCode() == 200) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (Objects.nonNull(responseEntity)) {
|
|
|
+ String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
+ log.info("articleSensitive 返回的数据:{}", responseBody);
|
|
|
+ JSONObject articleSensitiveResponse = JSONObject.parseObject(responseBody);
|
|
|
+ if (Objects.nonNull(articleSensitiveResponse) && articleSensitiveResponse.containsKey("is_sensitive")) {
|
|
|
+ isSensitive = articleSensitiveResponse.getBoolean("is_sensitive");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("articleSensitive error", e);
|
|
|
+ }
|
|
|
+ log.info("articleSensitive耗时:{}", System.currentTimeMillis() - start);
|
|
|
+ return isSensitive;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|