Skip to content

Commit d47dc3c

Browse files
committed
ApiBoot Logging Admin日志接收端点提供
1 parent ea9e2ed commit d47dc3c

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.endpoint;
19+
20+
import org.springframework.stereotype.Component;
21+
22+
import java.lang.annotation.*;
23+
24+
/**
25+
* ApiBoot Logging Endpoint Annotation
26+
* Only classes with this annotation will be scanned
27+
*
28+
* @author:恒宇少年 - 于起宇
29+
* <p>
30+
* DateTime:2019-07-19 09:55
31+
* Blog:http://blog.yuqiyu.com
32+
* WebSite:http://www.jianshu.com/u/092df3f77bca
33+
* Gitee:https://gitee.com/hengboy
34+
* GitHub:https://github.com/hengboy
35+
*/
36+
@Target({ElementType.TYPE})
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Documented
39+
@Component
40+
public @interface Endpoint {
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.endpoint;
19+
20+
import org.minbox.framework.api.boot.plugin.logging.ApiBootLogClientNotice;
21+
import org.minbox.framework.api.boot.plugin.logging.ReportResponse;
22+
import org.minbox.framework.api.boot.plugin.logging.admin.event.ReportLogEvent;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.http.HttpStatus;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.ResponseEntity;
30+
import org.springframework.web.bind.annotation.PostMapping;
31+
import org.springframework.web.bind.annotation.RequestBody;
32+
import org.springframework.web.bind.annotation.ResponseBody;
33+
34+
/**
35+
* ApiBoot Logging Endpoint Controller
36+
* Receive Log report
37+
* Provide log analysis
38+
*
39+
* @author:恒宇少年 - 于起宇
40+
* <p>
41+
* DateTime:2019-07-19 09:50
42+
* Blog:http://blog.yuqiyu.com
43+
* WebSite:http://www.jianshu.com/u/092df3f77bca
44+
* Gitee:https://gitee.com/hengboy
45+
* GitHub:https://github.com/hengboy
46+
*/
47+
@Endpoint
48+
public class LoggingEndpoint {
49+
/**
50+
* logger instance
51+
*/
52+
static Logger logger = LoggerFactory.getLogger(LoggingEndpoint.class);
53+
54+
/**
55+
* Application Context
56+
*/
57+
@Autowired
58+
private ApplicationContext applicationContext;
59+
60+
/**
61+
* Receive ApiBoot Logging Client Report Request Log
62+
*
63+
* @param notice ApiBoot Logging Client Notice Log Instance
64+
* @return report status
65+
*/
66+
@PostMapping(value = "/logging/report", consumes = MediaType.APPLICATION_PROBLEM_JSON_UTF8_VALUE)
67+
@ResponseBody
68+
public ResponseEntity<ReportResponse> report(@RequestBody ApiBootLogClientNotice notice) {
69+
// is report success
70+
boolean reportSuccess = true;
71+
try {
72+
// publish ReportLogEvent
73+
// Persistence logs are handed over to listeners
74+
applicationContext.publishEvent(new ReportLogEvent(this, notice));
75+
} catch (Exception e) {
76+
reportSuccess = false;
77+
logger.error(e.getMessage(), e);
78+
}
79+
ReportResponse response = new ReportResponse();
80+
response.setStatus(reportSuccess ? ReportResponse.SUCCESS : ReportResponse.ERROR);
81+
return new ResponseEntity(response, HttpStatus.OK);
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.logging.admin.endpoint;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.core.Ordered;
23+
import org.springframework.core.annotation.AnnotationUtils;
24+
import org.springframework.web.bind.annotation.RequestMapping;
25+
import org.springframework.web.bind.annotation.RequestMethod;
26+
import org.springframework.web.method.HandlerMethod;
27+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
28+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
29+
30+
import javax.servlet.http.HttpServletRequest;
31+
import java.lang.reflect.Method;
32+
import java.util.Arrays;
33+
import java.util.LinkedHashMap;
34+
import java.util.Map;
35+
36+
/**
37+
* ApiBoot Logging RequestMapping Handler
38+
*
39+
* @author:恒宇少年 - 于起宇
40+
* <p>
41+
* DateTime:2019-07-19 09:53
42+
* Blog:http://blog.yuqiyu.com
43+
* WebSite:http://www.jianshu.com/u/092df3f77bca
44+
* Gitee:https://gitee.com/hengboy
45+
* GitHub:https://github.com/hengboy
46+
*/
47+
public class LoggingRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
48+
/**
49+
* logger instance
50+
*/
51+
static Logger logger = LoggerFactory.getLogger(LoggingRequestMappingHandlerMapping.class);
52+
/**
53+
* logging handler method map
54+
*/
55+
private final Map<String, HandlerMethod> loggingHandlerMethods = new LinkedHashMap<>();
56+
/**
57+
* Endpoint Object
58+
*/
59+
private final Object handler;
60+
61+
public LoggingRequestMappingHandlerMapping(Object handler) {
62+
this.handler = handler;
63+
}
64+
65+
/**
66+
* Only Endpoint Class with @Endpoint annotation is processed
67+
*
68+
* @param beanType endpoint bean type class
69+
* @return is handler
70+
*/
71+
@Override
72+
protected boolean isHandler(Class<?> beanType) {
73+
return AnnotationUtils.findAnnotation(beanType, Endpoint.class) != null;
74+
}
75+
76+
/**
77+
* Init ApiBoot Logging Endpoint Object Methods
78+
*/
79+
@Override
80+
protected void initHandlerMethods() {
81+
logger.debug("ApiBoot Logging Endpoint Load Init.");
82+
Class<?> clazz = handler.getClass();
83+
setOrder(Ordered.HIGHEST_PRECEDENCE + 1000);
84+
if (isHandler(clazz)) {
85+
Method[] methods = clazz.getMethods();
86+
Arrays.stream(methods).forEach(method -> {
87+
if (AnnotationUtils.findAnnotation(method, RequestMapping.class) != null) {
88+
RequestMappingInfo mapping = getMappingForMethod(method, clazz);
89+
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
90+
mapping.getPatternsCondition().getPatterns().stream().forEach(path -> {
91+
logger.debug("Mapped ApiBoot Logging Endpoint URL [{}]", path);
92+
loggingHandlerMethods.put(path, handlerMethod);
93+
});
94+
}
95+
});
96+
}
97+
logger.debug("ApiBoot Logging Endpoint Load Successfully.");
98+
}
99+
100+
/**
101+
* The lookup handler method, maps the SEOMapper method to the request URL.
102+
* <p>If no mapping is found, or if the URL is disabled, it will simply drop through
103+
* to the standard 404 handling.</p>
104+
*
105+
* @param lookupPath lookup path
106+
* @param request http servlet request
107+
* @return The HandlerMethod if one was found.
108+
* @throws Exception exception
109+
*/
110+
@Override
111+
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
112+
logger.debug("looking up handler for path: " + lookupPath);
113+
HandlerMethod handlerMethod = loggingHandlerMethods.get(lookupPath);
114+
115+
if (RequestMethod.POST.toString().equals(request.getMethod()) && handlerMethod != null) {
116+
return handlerMethod;
117+
}
118+
return null;
119+
}
120+
121+
}

0 commit comments

Comments
 (0)