diff --git a/pom.xml b/pom.xml index d904b4a88dddb2fc71db52041ff66d1cb3202d90..15fe190b9b9df01d58bb7d64e5082cbf6bfbe057 100644 --- a/pom.xml +++ b/pom.xml @@ -176,6 +176,22 @@ 1.15 + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.17.23 + + + org.elasticsearch + elasticsearch + 7.17.22 + + + org.asynchttpclient + async-http-client + 2.12.1 + + diff --git a/src/main/java/com/easysoftware/adapter/query/CoMaintainerAdapter.java b/src/main/java/com/easysoftware/adapter/query/CoMaintainerAdapter.java index 0e5287fe4485791e0b62f0d7407026f54b4cb248..893ef7506226a2a03af12673267d07ec7db1b311 100644 --- a/src/main/java/com/easysoftware/adapter/query/CoMaintainerAdapter.java +++ b/src/main/java/com/easysoftware/adapter/query/CoMaintainerAdapter.java @@ -24,15 +24,25 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.easysoftware.application.collaboration.CoMaintainerService; +import com.easysoftware.application.collaboration.dto.PackageSearchCondition; import com.easysoftware.common.account.UserPermission; import com.easysoftware.common.annotation.CoMaintainerPermission; +import com.easysoftware.common.annotation.CoUserRepoPermission; import com.easysoftware.common.aop.RequestLimitRedis; import com.easysoftware.common.entity.MessageCode; import com.easysoftware.common.utils.ResultUtil; +import jakarta.validation.Valid; + @RestController @RequestMapping("/collaboration/maintainer") public class CoMaintainerAdapter { + /** + * Autowired service for handling package maintainer related operations. + */ + @Autowired + private CoMaintainerService coMaintainerService; /** * Logger for CoMaintainerAdapter. @@ -54,11 +64,25 @@ public class CoMaintainerAdapter { */ @GetMapping() @RequestLimitRedis() - @CoMaintainerPermission() + @CoUserRepoPermission() public ResponseEntity queryRepos(@RequestParam(value = "repo") String repo) { return ResultUtil.success(HttpStatus.OK, "success"); } + /** + * Endpoint to search for repos based on the provided search + * condition. + * + * @param condition The search condition for querying packages. + * @return ResponseEntity. + */ + @GetMapping("/user/repos") + @RequestLimitRedis() + @CoMaintainerPermission() + public ResponseEntity queryPackages(@Valid final PackageSearchCondition condition) { + return coMaintainerService.queryPackages(condition); + } + /** * Check if the user has permission to access. * diff --git a/src/main/java/com/easysoftware/application/collaboration/CoMaintainerService.java b/src/main/java/com/easysoftware/application/collaboration/CoMaintainerService.java new file mode 100644 index 0000000000000000000000000000000000000000..fe911a1f09a705d3d3838e0d1c6e01e6e6ce9edf --- /dev/null +++ b/src/main/java/com/easysoftware/application/collaboration/CoMaintainerService.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.application.collaboration; + +import org.springframework.http.ResponseEntity; + +import com.easysoftware.application.collaboration.dto.PackageSearchCondition; + +public interface CoMaintainerService { + /** + * Searches for packages based on the specified search conditions. + * + * @param condition The search conditions to filter packages. + * @return ResponseEntity. + */ + ResponseEntity queryPackages(PackageSearchCondition condition); +} diff --git a/src/main/java/com/easysoftware/application/collaboration/CoMaintainerServiceImpl.java b/src/main/java/com/easysoftware/application/collaboration/CoMaintainerServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..8c013530f67438356eeb1cdedb46ec8dd94f6321 --- /dev/null +++ b/src/main/java/com/easysoftware/application/collaboration/CoMaintainerServiceImpl.java @@ -0,0 +1,45 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.application.collaboration; + +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import com.easysoftware.application.collaboration.dto.PackageSearchCondition; +import com.easysoftware.common.utils.ResultUtil; +import com.easysoftware.domain.collaboration.gateway.PackageStatusGateway; + +import jakarta.annotation.Resource; + +@Service("CoMaintainerService") +public class CoMaintainerServiceImpl implements CoMaintainerService { + /** + * Resource for interacting with package status Gateway. + */ + @Resource + private PackageStatusGateway pkgStatusGateway; + + /** + * Searches for packages based on the specified search conditions. + * + * @param condition The search conditions to filter packages. + * @return ResponseEntity. + */ + @Override + public ResponseEntity queryPackages(PackageSearchCondition condition) { + Map res = pkgStatusGateway.queryByCondition(condition); + return ResultUtil.success(HttpStatus.OK, res); + } +} diff --git a/src/main/java/com/easysoftware/application/collaboration/dto/PackageSearchCondition.java b/src/main/java/com/easysoftware/application/collaboration/dto/PackageSearchCondition.java new file mode 100644 index 0000000000000000000000000000000000000000..1c40fe8a46f785cb27b3202f38457e2dd9f7ae64 --- /dev/null +++ b/src/main/java/com/easysoftware/application/collaboration/dto/PackageSearchCondition.java @@ -0,0 +1,66 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.application.collaboration.dto; + +import org.hibernate.validator.constraints.Range; + +import com.easysoftware.common.constant.PackageConstant; + +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class PackageSearchCondition { + /** + * Name of the package. Restricted by length and character pattern. + */ + @Size(max = PackageConstant.MAX_FIELD_LENGTH) + @Pattern(regexp = PackageConstant.VALID_STR_REG, message = PackageConstant.VALID_MESSAGE) + private String repo; + + /** + * Page number within the allowable range. + */ + @Range(min = PackageConstant.MIN_PAGE_NUM, max = PackageConstant.MAX_PAGE_NUM) + private Integer pageNum = 1; + + /** + * Page size within the allowable range. + */ + @Range(min = PackageConstant.MIN_PAGE_SIZE, max = PackageConstant.MAX_PAGE_SIZE) + private Integer pageSize = 10; + + /** + * repo kind. + */ + @Size(max = PackageConstant.MAX_FIELD_LENGTH) + @Pattern(regexp = PackageConstant.VALID_STR_REG, message = PackageConstant.VALID_MESSAGE) + private String kind; + + /** + * repo status. + */ + @Size(max = PackageConstant.MAX_FIELD_LENGTH) + @Pattern(regexp = PackageConstant.VALID_STR_REG, message = PackageConstant.VALID_MESSAGE) + private String status; + + /** + * sig name. + */ + @Size(max = PackageConstant.MAX_FIELD_LENGTH) + @Pattern(regexp = PackageConstant.VALID_STR_REG, message = PackageConstant.VALID_MESSAGE) + private String sigName; + +} diff --git a/src/main/java/com/easysoftware/application/collaboration/vo/PackageStatusVO.java b/src/main/java/com/easysoftware/application/collaboration/vo/PackageStatusVO.java new file mode 100644 index 0000000000000000000000000000000000000000..96da3b6d4cf36eea53f56bb9190ab440bd06f299 --- /dev/null +++ b/src/main/java/com/easysoftware/application/collaboration/vo/PackageStatusVO.java @@ -0,0 +1,69 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.application.collaboration.vo; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class PackageStatusVO { + /** + * Name of the package. Restricted by length and character pattern. + */ + private String repo; + + /** + * repo kind. + */ + private String kind; + + /** + * repo status. + */ + private String status; + + /** + * sig name. + */ + private String sigName; + + /** + * cve status. + */ + private String cveStatus; + + /** + * issue status. + */ + private String issueStatus; + + /** + * pr status. + */ + private String prStatus; + + /** + * contributor status. + */ + private String contributorStatus; + + /** + * org status. + */ + private String orgStatus; + + /** + * version status. + */ + private String versionStatus; +} diff --git a/src/main/java/com/easysoftware/common/account/UserPermission.java b/src/main/java/com/easysoftware/common/account/UserPermission.java index ae0330e33bf767c85742f31419ea32ff39de3122..d41afd4b73eb872a0dde02891200ca032249a39d 100644 --- a/src/main/java/com/easysoftware/common/account/UserPermission.java +++ b/src/main/java/com/easysoftware/common/account/UserPermission.java @@ -100,6 +100,18 @@ public class UserPermission { return repoSet.contains(repo); } + /** + * Check if user has maintainer permission. + * @return Permission matching results. + */ + public boolean checkUserMaintainerPermission() { + HashSet repoSet = this.getUserRepoList(); + if (Objects.isNull(repoSet) || repoSet.isEmpty()) { + return false; + } + return true; + } + /** * Get user login name by user token and manage token. * @return login name. diff --git a/src/main/java/com/easysoftware/common/annotation/CoUserRepoPermission.java b/src/main/java/com/easysoftware/common/annotation/CoUserRepoPermission.java new file mode 100644 index 0000000000000000000000000000000000000000..6ecfa36cdbf14e4cab03475579a3e67d8a7e0369 --- /dev/null +++ b/src/main/java/com/easysoftware/common/annotation/CoUserRepoPermission.java @@ -0,0 +1,33 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.common.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Inherited; +import java.lang.annotation.Documented; + +@Target({ ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Documented +public @interface CoUserRepoPermission { + + /** + * Specifies the number of allowed requests (default value: 5). + * + * @return Default to empty. + */ + String[] value() default {}; +} diff --git a/src/main/java/com/easysoftware/common/aop/CoMaintainerAspect.java b/src/main/java/com/easysoftware/common/aop/CoMaintainerAspect.java index e12d7c217f4c2e71cee1faea735c47478687ae06..8d6aa2f4fa1fffae726c00118bce084380bd4c77 100644 --- a/src/main/java/com/easysoftware/common/aop/CoMaintainerAspect.java +++ b/src/main/java/com/easysoftware/common/aop/CoMaintainerAspect.java @@ -24,8 +24,6 @@ import com.easysoftware.common.account.UserPermission; import com.easysoftware.common.entity.MessageCode; import com.easysoftware.common.utils.ResultUtil; -import jakarta.servlet.http.HttpServletRequest; - @Aspect @Component public class CoMaintainerAspect { @@ -41,32 +39,27 @@ public class CoMaintainerAspect { private UserPermission userPermission; /** - * Autowired HttpServletRequest for handling HTTP request information. - */ - @Autowired - private HttpServletRequest request; - - /** - * Advice method called before a method with CoMaintainerPermission, and authentication. - * @param joinPoint The JoinPoint representing the intercepted method. - * @throws Throwable if an error occurs during method execution, or authentication fail. + * Advice method called before a method with CoMaintainerPermission, and + * authentication. + * @param joinPoint The JoinPoint representing the intercepted method. + * @throws Throwable if an error occurs during method execution, or + * authentication fail. * @return Business processing results. */ @Around("@annotation(com.easysoftware.common.annotation.CoMaintainerPermission)") public Object around(final ProceedingJoinPoint joinPoint) throws Throwable { try { - String repo = request.getParameter("repo"); - /* Check if the user has repo permission */ - boolean permissionFlag = userPermission.checkUserRepoPermission(repo); + /* Check if the user has maintainer permission */ + boolean permissionFlag = userPermission.checkUserMaintainerPermission(); if (!permissionFlag) { LOGGER.error("Insufficient permissions"); - return ResultUtil.fail(HttpStatus.FORBIDDEN, MessageCode.EC00019); + return ResultUtil.fail(HttpStatus.FORBIDDEN, MessageCode.EC00019); } } catch (Exception e) { LOGGER.error("Authentication exception - {}", e.getMessage()); - return ResultUtil.fail(HttpStatus.UNAUTHORIZED, MessageCode.EC00020); + return ResultUtil.fail(HttpStatus.UNAUTHORIZED, MessageCode.EC00020); } /* 业务处理 */ diff --git a/src/main/java/com/easysoftware/common/aop/CoUserRepoAspect.java b/src/main/java/com/easysoftware/common/aop/CoUserRepoAspect.java new file mode 100644 index 0000000000000000000000000000000000000000..d0711f48b5a1ed8c04a217ef6da0d618bf0d7a71 --- /dev/null +++ b/src/main/java/com/easysoftware/common/aop/CoUserRepoAspect.java @@ -0,0 +1,77 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.common.aop; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +import com.easysoftware.common.account.UserPermission; +import com.easysoftware.common.entity.MessageCode; +import com.easysoftware.common.utils.ResultUtil; + +import jakarta.servlet.http.HttpServletRequest; + +@Aspect +@Component +public class CoUserRepoAspect { + /** + * Logger for CoUserRepoAspect. + */ + private static final Logger LOGGER = LoggerFactory.getLogger(CoMaintainerAspect.class); + + /** + * Autowired UserPermission for get user permission. + */ + @Autowired + private UserPermission userPermission; + + /** + * Autowired HttpServletRequest for handling HTTP request information. + */ + @Autowired + private HttpServletRequest request; + + /** + * Advice method called before a method with CoMaintainerPermission, and + * authentication. + * @param joinPoint The JoinPoint representing the intercepted method. + * @throws Throwable if an error occurs during method execution, or + * authentication fail. + * @return Business processing results. + */ + @Around("@annotation(com.easysoftware.common.annotation.CoUserRepoPermission)") + public Object around(final ProceedingJoinPoint joinPoint) throws Throwable { + try { + String repo = request.getParameter("repo"); + + /* Check if the user has repo permission */ + boolean permissionFlag = userPermission.checkUserRepoPermission(repo); + + if (!permissionFlag) { + LOGGER.error("Insufficient permissions"); + return ResultUtil.fail(HttpStatus.FORBIDDEN, MessageCode.EC00019); + } + } catch (Exception e) { + LOGGER.error("Authentication exception - {}", e.getMessage()); + return ResultUtil.fail(HttpStatus.UNAUTHORIZED, MessageCode.EC00020); + } + + /* 业务处理 */ + return joinPoint.proceed(); + } +} diff --git a/src/main/java/com/easysoftware/common/constant/HttpConstant.java b/src/main/java/com/easysoftware/common/constant/HttpConstant.java index 19a458c03b8b81f50c963057635c22725557add0..7b2e7da6dd59ccaf796b5cb2c16019ebb3e4d8c9 100644 --- a/src/main/java/com/easysoftware/common/constant/HttpConstant.java +++ b/src/main/java/com/easysoftware/common/constant/HttpConstant.java @@ -61,4 +61,9 @@ public final class HttpConstant { * https protol type key. */ public static final String HTTPS_PROTOL = "https"; + + /** + * Timeout duration in milliseconds. + */ + public static final int ES_TIME_OUT = 100000; } diff --git a/src/main/java/com/easysoftware/common/constant/PackageConstant.java b/src/main/java/com/easysoftware/common/constant/PackageConstant.java index a61688f59b64ddc0b35239a61b6f1ca817e442be..de2bc04122122268300832efb58645717207f204 100644 --- a/src/main/java/com/easysoftware/common/constant/PackageConstant.java +++ b/src/main/java/com/easysoftware/common/constant/PackageConstant.java @@ -163,4 +163,14 @@ public final class PackageConstant { * Constan software num. */ public static final long SOFTWARE_NUM = 82782; + + /** + * index name of package status. + */ + public static final String PACKAGE_STATUS_INDEX = "package_status_20240525_test"; + + /** + * package status keyword format. + */ + public static final String KEY_WORD_FORMAT = " AND %s.keyword:%s"; } diff --git a/src/main/java/com/easysoftware/common/utils/EsAsyncHttpUtil.java b/src/main/java/com/easysoftware/common/utils/EsAsyncHttpUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..2a8cdb17d956a8a0e2f947eec8bd4548111cc46a --- /dev/null +++ b/src/main/java/com/easysoftware/common/utils/EsAsyncHttpUtil.java @@ -0,0 +1,202 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.common.utils; + +import io.netty.handler.ssl.ClientAuth; +import io.netty.handler.ssl.JdkSslContext; +import io.netty.handler.ssl.SslProtocols; + +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Base64; +import java.util.Map; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.net.ssl.X509TrustManager; + +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.DefaultAsyncHttpClient; +import org.asynchttpclient.DefaultAsyncHttpClientConfig; +import org.asynchttpclient.ListenableFuture; +import org.asynchttpclient.RequestBuilder; +import org.asynchttpclient.Response; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.stereotype.Component; + +import com.easysoftware.common.constant.HttpConstant; +import com.easysoftware.common.constant.PackageConstant; + +@Component +public final class EsAsyncHttpUtil { + /** + * Value injected for the es url. + */ + @Value("${es.url}") + private String esUrl; + + /** + * Value injected for the es user. + */ + @Value("${es.user}") + private String esUser; + + /** + * Value injected for the es password. + */ + @Value("${es.pwd}") + private String pwd; + + /** + * Value injected for the es searchFormat. + */ + @Value("${es.searchFormat}") + private String searchFormat; + + /** + * AsyncHttpClient. + */ + private static volatile AsyncHttpClient asyncHttpClient = null; + + /** + * get es client. + * @return AsyncHttpClient + */ + public static synchronized AsyncHttpClient getClient() throws KeyManagementException, NoSuchAlgorithmException { + if (asyncHttpClient == null) { + asyncHttpClient = new DefaultAsyncHttpClient(new DefaultAsyncHttpClientConfig.Builder() + .setConnectTimeout(HttpConstant.ES_TIME_OUT) + .setRequestTimeout(HttpConstant.ES_TIME_OUT) + .setSslContext(new JdkSslContext(skipSsl(), true, ClientAuth.NONE)) + .build()); + } + + return asyncHttpClient; + } + + /** + * get es builder. + * @return RequestBuilder + */ + public RequestBuilder getBuilder() { + RequestBuilder builder = new RequestBuilder(); + builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); + builder.addHeader(HttpHeaders.AUTHORIZATION, + "Basic " + Base64.getEncoder().encodeToString((esUser + ":" + pwd).getBytes(StandardCharsets.UTF_8))) + .setMethod(HttpConstant.POST); + return builder; + + } + + /** + * execute es search. + * @param index es index name + * @param obj search condition + * @return ListenableFuture the result of es search. + */ + public ListenableFuture executeSearch(String index, Map obj) + throws NoSuchAlgorithmException, KeyManagementException { + AsyncHttpClient client = getClient(); + RequestBuilder builder = getBuilder(); + String query = convertQuery(obj); + + builder.setUrl(esUrl + index + "/_search"); + builder.setBody(query); + + return client.executeRequest(builder.build()); + } + + /** + * convert condition to query string. + * @param obj search condition + * @return query string. + */ + public String convertQuery(Map obj) { + String queryString = ""; + for (Map.Entry entry : obj.entrySet()) { + String field = entry.getKey(); + if (field.contains("page")) { + continue; + } + String value = entry.getValue().toString(); + queryString += String.format(PackageConstant.KEY_WORD_FORMAT, field, value); + } + + int pageSize = Integer.parseInt(obj.get("pageSize").toString()); + int from = (Integer.parseInt(obj.get("pageNum").toString()) - 1) * pageSize; + String query = String.format(searchFormat, from, pageSize, queryString); + return query; + } + + /** + * skip ssl. + * @return SSLContext + */ + public static SSLContext skipSsl() throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sc = SSLContext.getInstance(SslProtocols.TLS_v1_2); + + X509TrustManager trustManager = new X509ExtendedTrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) + throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) + throws CertificateException { + + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) + throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) + throws CertificateException { + + } + + @Override + public void checkClientTrusted( + X509Certificate[] paramArrayOfX509Certificate, + String paramString) throws CertificateException { + } + + @Override + public void checkServerTrusted( + X509Certificate[] paramArrayOfX509Certificate, + String paramString) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + SecureRandom secureRandom = SecureRandom.getInstanceStrong(); + sc.init(null, new TrustManager[] { + trustManager + }, secureRandom); + return sc; + } +} diff --git a/src/main/java/com/easysoftware/domain/collaboration/gateway/PackageStatusGateway.java b/src/main/java/com/easysoftware/domain/collaboration/gateway/PackageStatusGateway.java new file mode 100644 index 0000000000000000000000000000000000000000..18af2f0429c828f9285fefb9b3093cbbd714db64 --- /dev/null +++ b/src/main/java/com/easysoftware/domain/collaboration/gateway/PackageStatusGateway.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.domain.collaboration.gateway; + +import java.util.Map; + +import com.easysoftware.application.collaboration.dto.PackageSearchCondition; + +public interface PackageStatusGateway { + /** + * Query detailed information based on the provided search condition. + * + * @param condition The search condition for querying detailed information + * @return A map containing detailed information + */ + Map queryByCondition(PackageSearchCondition condition); +} diff --git a/src/main/java/com/easysoftware/infrastructure/applicationversion/gatewayimpl/ApplicationVersionGatewayImpl.java b/src/main/java/com/easysoftware/infrastructure/applicationversion/gatewayimpl/ApplicationVersionGatewayImpl.java index 376cc61c9ffcdd5ce1b40fbe810db3916bf17dfa..6cf95c74fc2d271bcd2312cd775b1256ead8cd6a 100644 --- a/src/main/java/com/easysoftware/infrastructure/applicationversion/gatewayimpl/ApplicationVersionGatewayImpl.java +++ b/src/main/java/com/easysoftware/infrastructure/applicationversion/gatewayimpl/ApplicationVersionGatewayImpl.java @@ -63,9 +63,7 @@ public class ApplicationVersionGatewayImpl implements ApplicationVersionGateway List appDOs = resPage.getRecords(); List appDetails = ApplicationVersionConvertor.toEntity(appDOs); - Map res = Map.ofEntries( - Map.entry("total", resPage.getTotal()), - Map.entry("list", appDetails)); + Map res = Map.ofEntries(Map.entry("total", resPage.getTotal()), Map.entry("list", appDetails)); return res; } diff --git a/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/PackageStatusGatewayImpl.java b/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/PackageStatusGatewayImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..d80c47647de082b46d6a8de8a7ac8a733363f0a4 --- /dev/null +++ b/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/PackageStatusGatewayImpl.java @@ -0,0 +1,73 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.infrastructure.collaboration.gatewayimpl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.asynchttpclient.ListenableFuture; +import org.asynchttpclient.Response; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.easysoftware.application.collaboration.dto.PackageSearchCondition; +import com.easysoftware.application.collaboration.vo.PackageStatusVO; +import com.easysoftware.common.constant.PackageConstant; +import com.easysoftware.common.utils.EsAsyncHttpUtil; +import com.easysoftware.common.utils.ObjectMapperUtil; +import com.easysoftware.domain.collaboration.gateway.PackageStatusGateway; +import com.easysoftware.infrastructure.collaboration.gatewayimpl.converter.PackageStatusConverter; +import com.fasterxml.jackson.databind.JsonNode; + +import static java.nio.charset.StandardCharsets.UTF_8; + +@Component +public class PackageStatusGatewayImpl implements PackageStatusGateway { + /** + * Autowired field for the EsAsyncHttpUtil. + */ + @Autowired + private EsAsyncHttpUtil esAsyncHttpUtil; + + /** + * Logger instance for PackageStatusGatewayImpl. + */ + private static final Logger LOGGER = LoggerFactory.getLogger(PackageStatusGatewayImpl.class); + + /** + * Query information based on the provided search condition. + * + * @param condition The search condition for querying package + * @return A map containing relevant information + */ + @Override + public Map queryByCondition(final PackageSearchCondition condition) { + List pkgs = new ArrayList<>(); + int total = 0; + try { + Map query = ObjectMapperUtil.jsonToMap(condition); + ListenableFuture future = esAsyncHttpUtil.executeSearch(PackageConstant.PACKAGE_STATUS_INDEX, + query); + String responseBody = future.get().getResponseBody(UTF_8); + JsonNode dataNode = ObjectMapperUtil.toJsonNode(responseBody); + JsonNode hits = dataNode.path("hits").path("hits"); + total = dataNode.path("hits").path("total").path("value").asInt(); + pkgs = PackageStatusConverter.toDetail(hits); + } catch (Exception e) { + LOGGER.error("search package error - {}", e.getMessage()); + } + return Map.ofEntries(Map.entry("total", total), Map.entry("list", pkgs)); + } +} diff --git a/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/converter/PackageStatusConverter.java b/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/converter/PackageStatusConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..0a21a6eeeddc8348793b9337f8df12495592086e --- /dev/null +++ b/src/main/java/com/easysoftware/infrastructure/collaboration/gatewayimpl/converter/PackageStatusConverter.java @@ -0,0 +1,61 @@ +/* Copyright (c) 2024 openEuler Community + EasySoftware is licensed under the Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. +*/ + +package com.easysoftware.infrastructure.collaboration.gatewayimpl.converter; + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.easysoftware.application.collaboration.vo.PackageStatusVO; +import com.fasterxml.jackson.databind.JsonNode; + +public final class PackageStatusConverter { + // Private constructor to prevent instantiation of the utility class + private PackageStatusConverter() { + // private constructor to hide the implicit public one + throw new AssertionError("Cannot instantiate PackageStatusConverter class"); + } + + /** + * Logger instance for PackageStatusConverter. + */ + private static final Logger LOGGER = LoggerFactory.getLogger(PackageStatusConverter.class); + + /** + * Convert JsonNode to a list of PackageStatusVO + * objects. + * + * @param hits JsonNode to convert + * @return A list of PackageStatusVO objects + */ + public static List toDetail(final JsonNode hits) { + List pkgs = new ArrayList<>(); + for (JsonNode hit : hits) { + PackageStatusVO pkg = new PackageStatusVO(); + JsonNode source = hit.path("_source"); + pkg.setIssueStatus(source.path("issue").path("status").asText()); + pkg.setPrStatus(source.path("package_update").path("status").asText()); + pkg.setCveStatus(source.path("cve").path("status").asText()); + pkg.setVersionStatus(source.path("package_version").path("status").asText()); + pkg.setOrgStatus(source.path("company").path("status").asText()); + pkg.setContributorStatus(source.path("participant").path("status").asText()); + pkg.setRepo(source.path("repo").asText()); + pkg.setKind(source.path("kind").asText()); + pkg.setSigName(source.path("sig_names").asText()); + pkg.setStatus(source.path("status").asText()); + pkgs.add(pkg); + } + return pkgs; + } +}