Skip to content

Commit

Permalink
feat($Starter): add HttpApiScanHelper.java
Browse files Browse the repository at this point in the history
Scan HTTP API for microservice
  • Loading branch information
Johnny Miller (锺俊) committed Dec 25, 2020
1 parent 97aadce commit 629e066
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.jmsoftware.maf.authcenter.permission.controller;

import com.jmsoftware.maf.authcenter.permission.service.PermissionService;
import com.jmsoftware.maf.authcenter.universal.configuration.ProjectProperty;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -29,6 +31,8 @@
public class PermissionController {
private final PermissionService permissionService;
private final DiscoveryClient discoveryClient;
private final HttpApiScanHelper httpApiScanHelper;
private final ProjectProperty projectProperty;

@GetMapping("/permissions/services-info")
@ApiOperation(value = "Get services info", notes = "Get services info")
Expand All @@ -41,6 +45,8 @@ public ResponseBodyBean<?> servicesInfo() {
log.info("Instances: {}", instances);
resultMap.put(service, instances);
});
val httpApiMap = httpApiScanHelper.scan(projectProperty.getBasePackage());
resultMap.put("httpApiMap", httpApiMap.toString());
return ResponseBodyBean.ofSuccess(resultMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public ResponseBodyBean<?> handleException(HttpServletRequest request, HttpServl
return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()),
null);
}
log.error("Internal system exception occurred! Exception message: {} ", exception.getMessage(), exception);
log.error("Internal server exception occurred! Exception message: {} ", exception.getMessage(), exception);
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR,
"Exception message: " + removeLineSeparator(exception.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jmsoftware.maf.springbootstarter.controller.GlobalErrorController;
import com.jmsoftware.maf.springbootstarter.controller.RedirectController;
import com.jmsoftware.maf.springbootstarter.filter.AccessLogFilter;
import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper;
import com.jmsoftware.maf.springbootstarter.helper.IpHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
Expand All @@ -20,6 +21,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;
import java.util.List;
Expand Down Expand Up @@ -99,4 +101,10 @@ public GlobalErrorController globalErrorController(ErrorAttributes errorAttribut
log.warn("Initial bean: {}", GlobalErrorController.class.getName());
return new GlobalErrorController(errorAttributes, serverProperties, errorViewResolvers);
}

@Bean
public HttpApiScanHelper httpApiScanHelper(RequestMappingHandlerMapping requestMappingHandlerMapping) {
log.warn("Initial bean: {}", HttpApiScanHelper.class.getName());
return new HttpApiScanHelper(requestMappingHandlerMapping);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.jmsoftware.maf.springbootstarter.helper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Description: HttpApiScanHelper, scanning HTTP API for microservice.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/25/2020 2:43 PM
**/
@Slf4j
@Validated
@RequiredArgsConstructor
public class HttpApiScanHelper {
private final RequestMappingHandlerMapping requestMappingHandlerMapping;
private static final String EXCLUDED_PACKAGE = "com.jmsoftware.maf.springbootstarter";

/**
* Scan map.
*
* @param includedPackage the included package
* @return the map
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/25/2020 4:02 PM
*/
public Map<RequestMappingInfo, HandlerMethod> scan(@NotBlank String includedPackage) {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
log.debug("Scanned request mapping info: {}", handlerMethods);
val filteredHandlerMethods = new LinkedHashMap<RequestMappingInfo, HandlerMethod>();
handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
if (handlerMethod.toString().contains(includedPackage) &&
!handlerMethod.toString().contains(EXCLUDED_PACKAGE)) {
try {
RequestMethod requestMethod = new ArrayList<>(
requestMappingInfo.getMethodsCondition().getMethods()).get(0);
log.debug("Request: [{}] {}, handler method: {}", requestMethod,
requestMappingInfo.getPatternsCondition(), handlerMethod);
filteredHandlerMethods.put(requestMappingInfo, handlerMethod);
} catch (Exception e) {
log.warn(
"Exception occurred when getting request method. Exception message: {}. Request mapping " +
"info: {}",
e.getMessage(), requestMappingInfo);
}
}
});
return filteredHandlerMethods;
}
}

0 comments on commit 629e066

Please sign in to comment.