Skip to content

Commit

Permalink
feat($Starter): provide exposing HTTP resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Miller (锺俊) committed Dec 25, 2020
1 parent 629e066 commit 4f2ebdc
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions auth-center/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ maf:
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
included-package-for-http-api-scan: ${project.property.base-package}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jmsoftware.maf.common.domain.springbootstarter;

import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
* Description: HttpApiResourcesResponse, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/25/2020 4:58 PM
**/
@Data
public class HttpApiResourcesResponse {
private List<HttpApiResource> list = Lists.newLinkedList();

@Data
public static class HttpApiResource {
private RequestMethod method;
private String urlPattern;
}
}
1 change: 1 addition & 0 deletions exercise-mis/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ maf:
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
included-package-for-http-api-scan: ${project.property.base-package}
1 change: 1 addition & 0 deletions muscle-mis/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ maf:
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
included-package-for-http-api-scan: ${project.property.base-package}
2 changes: 1 addition & 1 deletion service-registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ maf:
ignored-url:
pattern:
- "/actuator/**"
super-user: "admin"
included-package-for-http-api-scan: ${project.property.base-package}
1 change: 1 addition & 0 deletions spring-boot-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ maf:
- "/*/v2/api-docs/**"
- "/webjars/**"
- "/doc.html"
included-package-for-http-api-scan: ${project.property.base-package}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jmsoftware.maf.springbootstarter.aspect.ExceptionControllerAdvice;
import com.jmsoftware.maf.springbootstarter.aspect.WebRequestLogAspect;
import com.jmsoftware.maf.springbootstarter.controller.GlobalErrorController;
import com.jmsoftware.maf.springbootstarter.controller.HttpApiResourceRemoteApiController;
import com.jmsoftware.maf.springbootstarter.controller.RedirectController;
import com.jmsoftware.maf.springbootstarter.filter.AccessLogFilter;
import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper;
Expand Down Expand Up @@ -107,4 +108,11 @@ public HttpApiScanHelper httpApiScanHelper(RequestMappingHandlerMapping requestM
log.warn("Initial bean: {}", HttpApiScanHelper.class.getName());
return new HttpApiScanHelper(requestMappingHandlerMapping);
}

@Bean
public HttpApiResourceRemoteApiController httpApiResourceRemoteController(MafConfiguration mafConfiguration,
HttpApiScanHelper httpApiScanHelper) {
log.warn("Initial bean: {}", HttpApiResourceRemoteApiController.class.getName());
return new HttpApiResourceRemoteApiController(mafConfiguration, httpApiScanHelper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class MafConfiguration {
@NotBlank
private String superUser = "admin";
/**
* Ignore URLs
* Ignore URLs, used by web access log filter and web security.
*/
@Valid
private IgnoredUrl ignoredUrl;
Expand All @@ -50,7 +50,11 @@ public class MafConfiguration {
*/
@NotNull
private Boolean webRequestLogDisabled = false;

/**
* Included package for http api scan, could be base package
*/
@NotBlank
private String includedPackageForHttpApiScan;
/**
* Flatten ignored urls string [ ].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.jmsoftware.maf.springbootstarter.controller;

import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.springbootstarter.HttpApiResourcesResponse;
import com.jmsoftware.maf.springbootstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Optional;

/**
* Description: HttpApiResourceRemoteApiController, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/25/2020 4:33 PM
**/
@RestController
@RequiredArgsConstructor
@Api(tags = {"HTTP API Resource Remote API"})
public class HttpApiResourceRemoteApiController {
private final MafConfiguration mafConfiguration;
private final HttpApiScanHelper httpApiScanHelper;

@GetMapping("/http-api-resources")
@ApiOperation(value = "Get HTTP API resources", notes = "Get HTTP API resources (Remote)")
public ResponseBodyBean<HttpApiResourcesResponse> getHttpApiResource() {
val handlerMethodMap = httpApiScanHelper.scan(mafConfiguration.getIncludedPackageForHttpApiScan());
HttpApiResourcesResponse response = new HttpApiResourcesResponse();
handlerMethodMap.forEach((requestMappingInfo, handlerMethod) -> {
val requestMethod = Optional.ofNullable(
new ArrayList<>(requestMappingInfo.getMethodsCondition().getMethods()).get(0))
.orElseThrow(() -> new RuntimeException("Request method mustn't be null!"));
val httpApiResource = new HttpApiResourcesResponse.HttpApiResource();
httpApiResource.setMethod(requestMethod);
httpApiResource.setUrlPattern(
new LinkedList<>(requestMappingInfo.getPatternsCondition().getPatterns()).get(0));
response.getList().add(httpApiResource);
});
return ResponseBodyBean.ofSuccess(response);
}
}

0 comments on commit 4f2ebdc

Please sign in to comment.