Explorar el Código

articleSensitive filter

wangyunpeng hace 11 meses
padre
commit
f3d56f24b7

+ 55 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/remote/ArticleSensitiveRemoteService.java

@@ -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;
+    }
+
+}

+ 2 - 4
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/FilterService.java

@@ -3,10 +3,7 @@ package com.tzld.longarticle.recommend.server.service.filter;
 import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.service.ServiceBeanFactory;
-import com.tzld.longarticle.recommend.server.service.filter.strategy.BadStrategy;
-import com.tzld.longarticle.recommend.server.service.filter.strategy.CategoryStrategy;
-import com.tzld.longarticle.recommend.server.service.filter.strategy.DuplicateStrategy;
-import com.tzld.longarticle.recommend.server.service.filter.strategy.HistoryTitleStrategy;
+import com.tzld.longarticle.recommend.server.service.filter.strategy.*;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.stereotype.Service;
@@ -82,6 +79,7 @@ public class FilterService {
         strategies.add(ServiceBeanFactory.getBean(DuplicateStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(CategoryStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));
+        strategies.add(ServiceBeanFactory.getBean(SensitiveStrategy.class));
 
         return strategies;
     }

+ 77 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/SensitiveStrategy.java

@@ -0,0 +1,77 @@
+package com.tzld.longarticle.recommend.server.service.filter.strategy;
+
+import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
+import com.tzld.longarticle.recommend.server.model.Content;
+import com.tzld.longarticle.recommend.server.remote.ArticleSensitiveRemoteService;
+import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
+import com.tzld.longarticle.recommend.server.service.filter.FilterResult;
+import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class SensitiveStrategy implements FilterStrategy {
+
+    @Autowired
+    private ArticleSensitiveRemoteService articleSensitiveRemoteService;
+
+    private final ExecutorService pool = ThreadPoolFactory.defaultPool();
+
+    @Override
+    public FilterResult filter(FilterParam param) {
+        FilterResult filterResult = new FilterResult();
+        List<String> result = new ArrayList<>();
+        List<Content> filterContents = new ArrayList<>();
+
+        CountDownLatch cdl = new CountDownLatch(param.getContents().size());
+        List<Future<Content>> futures = new ArrayList<>();
+
+        for (Content content : param.getContents()) {
+            Future<Content> future = pool.submit(() -> {
+                try {
+                    boolean isSensitive = articleSensitiveRemoteService.articleSensitive(content.getTitle());
+                    if (isSensitive) {
+                        content.setFilterReason("安全违规");
+                    }
+                    return content;
+                } finally {
+                    cdl.countDown();
+                }
+            });
+            futures.add(future);
+        }
+        try {
+            cdl.await(1000, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+            log.error("filter error", e);
+            return null;
+        }
+
+        for (Future<Content> f : futures) {
+            try {
+                Content content = f.get();
+                if (StringUtils.hasText(content.getFilterReason())) {
+                    filterContents.add(content);
+                } else {
+                    result.add(content.getId());
+                }
+            } catch (Exception e) {
+                log.error("future get error ", e);
+            }
+        }
+        filterResult.setContentIds(result);
+        filterResult.setFilterContent(filterContents);
+        return filterResult;
+    }
+
+}