|
@@ -1,187 +0,0 @@
|
|
|
-package seecoder.devcloud.api.nexus;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-import seecoder.devcloud.api.nexus.impl.AssetApiImpl;
|
|
|
|
|
-import seecoder.devcloud.api.nexus.impl.ComponentApiImpl;
|
|
|
|
|
-import seecoder.devcloud.api.nexus.impl.TaskApiImpl;
|
|
|
|
|
-import seecoder.devcloud.api.nexus.model.ApiVersion;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.boot.web.client.RestTemplateBuilder;
|
|
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
|
|
-import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
|
|
-import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
-
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class NexusApi {
|
|
|
|
|
-
|
|
|
|
|
- private static final String DEFAULT_USER = "admin";
|
|
|
|
|
- private static final String DEFAULT_PASSWORD = "admin123";
|
|
|
|
|
- private static final String CONTENT_TYPE = "application/json";
|
|
|
|
|
-
|
|
|
|
|
- private ApiVersion apiVersion;
|
|
|
|
|
- private String username = DEFAULT_USER;
|
|
|
|
|
- private String password = DEFAULT_PASSWORD;
|
|
|
|
|
- private String nexusServerUrl;
|
|
|
|
|
- private String restApiUrl;
|
|
|
|
|
- private AssetApi assetApi;
|
|
|
|
|
- private ComponentApi componentApi;
|
|
|
|
|
- private TaskApi taskApi;
|
|
|
|
|
-
|
|
|
|
|
- private RestTemplate restTemplate; //惰性调用getRestTemplate方法初始化
|
|
|
|
|
-
|
|
|
|
|
- public NexusApi(String nexusServerUrl) {
|
|
|
|
|
- this(nexusServerUrl, ApiVersion.V1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public NexusApi(String nexusServerUrl, ApiVersion apiVersion) {
|
|
|
|
|
- this(nexusServerUrl, apiVersion, DEFAULT_USER, DEFAULT_PASSWORD);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public NexusApi(String nexusServerUrl, ApiVersion apiVersion, String username, String password) {
|
|
|
|
|
- this.nexusServerUrl = nexusServerUrl.endsWith("/") ? nexusServerUrl.replaceAll("/$", "") : nexusServerUrl;
|
|
|
|
|
- this.nexusServerUrl = nexusServerUrl.startsWith("http") ? nexusServerUrl : "http://" + nexusServerUrl;
|
|
|
|
|
- this.apiVersion = apiVersion;
|
|
|
|
|
- this.username = username;
|
|
|
|
|
- this.password = password;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public AssetApi getAssetApi(){
|
|
|
|
|
- if (assetApi == null) {
|
|
|
|
|
- synchronized (this) {
|
|
|
|
|
- if (assetApi == null) {
|
|
|
|
|
- assetApi = new AssetApiImpl(this);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return this.assetApi;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public ComponentApi getComponentApi(){
|
|
|
|
|
- if (componentApi == null) {
|
|
|
|
|
- synchronized (this) {
|
|
|
|
|
- if (componentApi == null) {
|
|
|
|
|
- componentApi = new ComponentApiImpl(this);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return this.componentApi;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public TaskApi getTaskApi(){
|
|
|
|
|
- if (taskApi == null) {
|
|
|
|
|
- synchronized (this) {
|
|
|
|
|
- if (taskApi == null) {
|
|
|
|
|
- taskApi = new TaskApiImpl(this);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return this.taskApi;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public ApiVersion getApiVersion() {
|
|
|
|
|
- return apiVersion;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public <T> ResponseEntity<T> get(String path, Class<T> responseType, Map<String, List<String>> params, Object... pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- log.debug("生成的url={}", url);
|
|
|
|
|
- return getRestTemplate().getForEntity(url, responseType);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public <T> ResponseEntity<T> get(String path, Class<T> responseType, Map<String, List<String>> params, Map<String, Object> pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- log.debug("生成的url={}", url);
|
|
|
|
|
- return getRestTemplate().getForEntity(url, responseType);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public <T> ResponseEntity<T> post(String path, Class<T> responseType, Object data, Map<String, List<String>> params, Object... pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- return getRestTemplate().postForEntity(url, data, responseType);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public <T> ResponseEntity<T> post(String path, Class<T> responseType, Object data, Map<String, List<String>> params, Map<String, Object> pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- return getRestTemplate().postForEntity(url, data, responseType);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public ResponseEntity<Void> delete(String path, Map<String, List<String>> params, Object... pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- return getRestTemplate().exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public ResponseEntity<Void> delete(String path, Map<String, List<String>> params, Map<String, Object> pathVariables) {
|
|
|
|
|
- String url = buildUri(path, params, pathVariables);
|
|
|
|
|
- return getRestTemplate().exchange(url, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private String buildUri(String path, Map<String, List<String>> params, Object... pathVariables) {
|
|
|
|
|
- path = validatePath(path);
|
|
|
|
|
- MultiValueMap<String, String> paramMap = toMultiValueMap(params);
|
|
|
|
|
- return UriComponentsBuilder.fromHttpUrl(getRestApiUrl())
|
|
|
|
|
- .path(path)
|
|
|
|
|
- .queryParams(paramMap)
|
|
|
|
|
- .build(pathVariables)
|
|
|
|
|
- .toString();
|
|
|
|
|
- }
|
|
|
|
|
- private String buildUri(String path, Map<String, List<String>> params, Map<String, Object> pathVariables) {
|
|
|
|
|
- path = validatePath(path);
|
|
|
|
|
- MultiValueMap<String, String> paramMap = toMultiValueMap(params);
|
|
|
|
|
- return UriComponentsBuilder.fromHttpUrl(getRestApiUrl())
|
|
|
|
|
- .path(path)
|
|
|
|
|
- .queryParams(paramMap)
|
|
|
|
|
- .build(pathVariables)
|
|
|
|
|
- .toString();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private String validatePath(String path) {
|
|
|
|
|
- return path.startsWith("/") ? path : "/" + path;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private MultiValueMap<String, String> toMultiValueMap(Map<String, List<String>> params) {
|
|
|
|
|
- return (params == null) ?
|
|
|
|
|
- CollectionUtils.toMultiValueMap(Collections.EMPTY_MAP) :
|
|
|
|
|
- CollectionUtils.toMultiValueMap(params);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private String getRestApiUrl() {
|
|
|
|
|
- if (this.restApiUrl == null) {
|
|
|
|
|
- synchronized (this) {
|
|
|
|
|
- if (this.restApiUrl == null) {
|
|
|
|
|
- restApiUrl = this.nexusServerUrl + ApiVersion.V1.getApiNamespace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- return this.restApiUrl;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private RestTemplate getRestTemplate() {
|
|
|
|
|
- if (this.restTemplate == null) {
|
|
|
|
|
- synchronized (this) {
|
|
|
|
|
- if (this.restTemplate == null) {
|
|
|
|
|
- this.restTemplate = new RestTemplateBuilder()
|
|
|
|
|
- .basicAuthentication(username, password)
|
|
|
|
|
- .additionalInterceptors((ClientHttpRequestInterceptor) (httpRequest, bytes, clientHttpRequestExecution) -> {
|
|
|
|
|
- HttpHeaders headers = httpRequest.getHeaders();
|
|
|
|
|
- headers.add("Accept", CONTENT_TYPE);
|
|
|
|
|
- return clientHttpRequestExecution.execute(httpRequest, bytes);
|
|
|
|
|
- })
|
|
|
|
|
- .build();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return restTemplate;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|