Skip to content

Commit 6729222

Browse files
authored
Merge pull request #27 from why198852/master
Fix Bug report for Error message oversimplification On netty
2 parents 5b10530 + 0ba3c5d commit 6729222

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

netty/src/main/java/io/edurt/gcm/netty/dispatcher/RequestDispatcher.java

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void processor(ChannelHandlerContext ctx, FullHttpRequest httpRequest, Fu
7171
public void triggerAction(FullHttpRequest httpRequest, FullHttpResponse httpResponse)
7272
throws Exception
7373
{
74+
String content = null;
7475
HttpCharsetContentHandler httpCharsetContentHandler = injector.getInstance(HttpCharsetContentHandler.class);
7576
URI uri = URI.create(httpRequest.uri());
7677
String requestUrl = uri.getPath();
@@ -79,68 +80,68 @@ public void triggerAction(FullHttpRequest httpRequest, FullHttpResponse httpResp
7980
if (ObjectUtils.isEmpty(router)) {
8081
httpResponse.setStatus(HttpResponseStatus.NOT_FOUND);
8182
LOGGER.error("The requested path <{}> was not found or not supported it!", requestUrl);
82-
return;
83-
}
84-
String methodName = router.getMethod().getName();
85-
String controller = PropertiesUtils.getStringValue(configuration,
86-
NettyConfiguration.CONTROLLER_PACKAGE,
87-
NettyConfigurationDefault.CONTROLLER_PACKAGE);
88-
String ctrlClass = router.getClazz().getName();
89-
try {
90-
Class.forName(ctrlClass);
91-
}
92-
catch (ClassNotFoundException e) {
93-
LOGGER.error("Unable to instantiate controller information. Please check whether the package name is correct and the system specified path is {}",
94-
controller);
95-
httpResponse.setStatus(HttpResponseStatus.NOT_FOUND);
96-
return;
97-
}
98-
LOGGER.debug("Parsing method parameters, used to inject the corresponding entity!");
99-
Class<?> clazz = Class.forName(ctrlClass);
100-
Object ctrlObject = injector.getInstance(clazz);
101-
LOGGER.debug("Current execute controller {}", ctrlObject);
102-
ParameterDispatcher parameterDispatcher = injector.getInstance(ParameterDispatcher.class);
103-
ConcurrentHashMap<String, ArrayList> classAndParam = parameterDispatcher.getRequestObjectAndParam(ctrlObject, httpRequest, httpResponse, methodName, router);
104-
ArrayList<Object> classList = classAndParam.get(ParameterDispatcher.CLASS);
105-
Class[] classes = classList.toArray(new Class[classList.size()]);
106-
// When accessing a view, it supports view parameter parsing
107-
Object[] objects = classAndParam.get(ParameterDispatcher.PARAM).toArray();
108-
Method method = ctrlObject.getClass().getMethod(methodName, classes);
109-
String content = null;
110-
// Fix the problem of using @RestController annotation to return data results
111-
if (method.isAnnotationPresent(ResponseBody.class) || clazz.isAnnotationPresent(RestController.class)) {
83+
content = "Oops,the requested path was not found.";
84+
} else {
85+
String methodName = router.getMethod().getName();
86+
String controller = PropertiesUtils.getStringValue(configuration,
87+
NettyConfiguration.CONTROLLER_PACKAGE,
88+
NettyConfigurationDefault.CONTROLLER_PACKAGE);
89+
String ctrlClass = router.getClazz().getName();
11290
try {
113-
content = GSON.toJson(method.invoke(ctrlObject, classAndParam.get(ParameterDispatcher.PARAM).toArray()));
114-
httpResponse.setStatus(HttpResponseStatus.OK);
91+
Class.forName(ctrlClass);
11592
}
116-
catch (InvocationTargetException ex) {
117-
if (ex.getCause().getClass() == NettyException.class) {
118-
NettyException exception = (NettyException) ex.getCause();
119-
content = GSON.toJson(exception);
93+
catch (ClassNotFoundException e) {
94+
LOGGER.error("Unable to instantiate controller information. Please check whether the package name is correct and the system specified path is {}",
95+
controller);
96+
httpResponse.setStatus(HttpResponseStatus.NOT_FOUND);
97+
return;
98+
}
99+
LOGGER.debug("Parsing method parameters, used to inject the corresponding entity!");
100+
Class<?> clazz = Class.forName(ctrlClass);
101+
Object ctrlObject = injector.getInstance(clazz);
102+
LOGGER.debug("Current execute controller {}", ctrlObject);
103+
ParameterDispatcher parameterDispatcher = injector.getInstance(ParameterDispatcher.class);
104+
ConcurrentHashMap<String, ArrayList> classAndParam = parameterDispatcher.getRequestObjectAndParam(ctrlObject, httpRequest, httpResponse, methodName, router);
105+
ArrayList<Object> classList = classAndParam.get(ParameterDispatcher.CLASS);
106+
Class[] classes = classList.toArray(new Class[classList.size()]);
107+
// When accessing a view, it supports view parameter parsing
108+
Object[] objects = classAndParam.get(ParameterDispatcher.PARAM).toArray();
109+
Method method = ctrlObject.getClass().getMethod(methodName, classes);
110+
// Fix the problem of using @RestController annotation to return data results
111+
if (method.isAnnotationPresent(ResponseBody.class) || clazz.isAnnotationPresent(RestController.class)) {
112+
try {
113+
content = GSON.toJson(method.invoke(ctrlObject, classAndParam.get(ParameterDispatcher.PARAM).toArray()));
114+
httpResponse.setStatus(HttpResponseStatus.OK);
120115
}
121-
else {
122-
ex.printStackTrace();
123-
content = GSON.toJson(new NettyException(500, ex.getCause().getMessage()));
116+
catch (InvocationTargetException ex) {
117+
if (ex.getCause().getClass() == NettyException.class) {
118+
NettyException exception = (NettyException) ex.getCause();
119+
content = GSON.toJson(exception);
120+
}
121+
else {
122+
ex.printStackTrace();
123+
content = GSON.toJson(new NettyException(500, ex.getCause().getMessage()));
124+
}
125+
httpResponse.setStatus(HttpResponseStatus.BAD_GATEWAY);
124126
}
125-
httpResponse.setStatus(HttpResponseStatus.BAD_GATEWAY);
127+
httpResponse.headers().set(CONTENT_TYPE, httpCharsetContentHandler.getContentAndCharset(Charseter.UTF8, ContentType.APPLICATION_JSON));
128+
}
129+
else if (clazz.isAnnotationPresent(Controller.class)) {
130+
String viewName = String.valueOf(method.invoke(ctrlObject, objects));
131+
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), getTemplatePath());
132+
Template template = freemarkerConfiguration.getTemplate(viewName + PropertiesUtils.getStringValue(configuration,
133+
NettyConfiguration.VIEW_TEMPLATE_SUFFIX,
134+
NettyConfigurationDefault.VIEW_TEMPLATE_SUFFIX));
135+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
136+
// In view, we only need to extract the first one
137+
template.process(objects[0], new OutputStreamWriter(outputStream));
138+
content = outputStream.toString(CharsetUtil.UTF_8.name());
139+
httpResponse.headers().set(CONTENT_TYPE, httpCharsetContentHandler.getContentAndCharset(Charseter.UTF8, ContentType.TEXT_HTML));
140+
}
141+
else {
142+
// TODO: We don't do any processing here for the time being, and we will support it later
143+
LOGGER.warn("We don't do any processing here for the time being, and we will support it later");
126144
}
127-
httpResponse.headers().set(CONTENT_TYPE, httpCharsetContentHandler.getContentAndCharset(Charseter.UTF8, ContentType.APPLICATION_JSON));
128-
}
129-
else if (clazz.isAnnotationPresent(Controller.class)) {
130-
String viewName = String.valueOf(method.invoke(ctrlObject, objects));
131-
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), getTemplatePath());
132-
Template template = freemarkerConfiguration.getTemplate(viewName + PropertiesUtils.getStringValue(configuration,
133-
NettyConfiguration.VIEW_TEMPLATE_SUFFIX,
134-
NettyConfigurationDefault.VIEW_TEMPLATE_SUFFIX));
135-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
136-
// In view, we only need to extract the first one
137-
template.process(objects[0], new OutputStreamWriter(outputStream));
138-
content = outputStream.toString(CharsetUtil.UTF_8.name());
139-
httpResponse.headers().set(CONTENT_TYPE, httpCharsetContentHandler.getContentAndCharset(Charseter.UTF8, ContentType.TEXT_HTML));
140-
}
141-
else {
142-
// TODO: We don't do any processing here for the time being, and we will support it later
143-
LOGGER.warn("We don't do any processing here for the time being, and we will support it later");
144145
}
145146
httpResponse.content().writeBytes(Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
146147
}

0 commit comments

Comments
 (0)