1
1
package com .vison .webmvc .framework ;
2
2
3
3
import com .google .gson .Gson ;
4
+ import com .vison .webmvc .config .App ;
4
5
import com .vison .webmvc .config .Log ;
5
6
import com .vison .webmvc .framework .exception .NullRouteException ;
7
+ import java .io .BufferedReader ;
6
8
import java .io .IOException ;
9
+ import java .lang .annotation .Annotation ;
10
+ import java .lang .reflect .InvocationTargetException ;
7
11
import java .lang .reflect .Method ;
8
12
import java .lang .reflect .Parameter ;
9
13
import java .util .Set ;
14
+ import java .util .logging .Level ;
15
+ import java .util .logging .Logger ;
10
16
import javax .servlet .ServletException ;
11
17
import javax .servlet .annotation .WebServlet ;
12
18
import javax .servlet .http .HttpServlet ;
26
32
@ WebServlet (name = "DispatchServlet" , urlPatterns = {"/" })
27
33
public class DispatchServlet extends HttpServlet {
28
34
35
+ private Reflections f ;
36
+
29
37
@ Override
30
38
public void init () throws ServletException {
31
39
ViewEngine .load (this .getServletContext ());
40
+ f = App .f ;
41
+
32
42
}
33
43
34
44
@ Override
35
45
protected void doGet (HttpServletRequest req , HttpServletResponse resp )
36
46
throws ServletException , IOException {
47
+ try {
48
+ process (req , resp , RequestMethod .GET );
49
+ } catch (NullRouteException ex ) {
50
+ Logger .getLogger (DispatchServlet .class .getName ()).log (Level .SEVERE , null , ex );
51
+ }
52
+ }
53
+
54
+ @ Override
55
+ protected void doPost (HttpServletRequest req , HttpServletResponse resp )
56
+ throws ServletException , IOException {
57
+ try {
58
+ process (req , resp , RequestMethod .POST );
59
+ } catch (NullRouteException ex ) {
60
+ Logger .getLogger (DispatchServlet .class .getName ()).log (Level .SEVERE , null , ex );
61
+ }
62
+
63
+ }
64
+
65
+ private void process (HttpServletRequest req , HttpServletResponse resp , RequestMethod requestMethod )
66
+ throws NullRouteException , IOException {
37
67
resp .setContentType ("text/html" );
38
68
resp .setCharacterEncoding ("UTF-8" );
39
69
String path = req .getRequestURI ().substring (req .getContextPath ().length ());
40
- Object res ;
70
+ Object res = null ;
41
71
try {
42
- res = dispatch (path , req , resp );
72
+ Method invokeMethod = this .getMaps (path , requestMethod );
73
+ switch (requestMethod ) {
74
+ case GET :
75
+ res = getDispatch (req , resp , invokeMethod );
76
+ break ;
77
+ case POST :
78
+ res = postDispatch (req , resp , invokeMethod );
79
+ break ;
80
+ }
43
81
} catch (NullRouteException ex ) {
44
82
res = "404 Not Found" ;
83
+ } catch (Exception e ) {
84
+ Log .error ("捕获错误" , e );
45
85
}
46
86
String responseBody = this .handInvokeRes (res );
47
- Log .info ("返回信息" , responseBody );
48
87
resp .getWriter ().write (responseBody );
49
88
resp .getWriter ().flush ();
50
89
}
51
90
52
- private Object dispatch (String path , HttpServletRequest request , HttpServletResponse response )
53
- throws NullRouteException {
54
- Method method = this .getMaps (path );
91
+ private Object getDispatch (HttpServletRequest request , HttpServletResponse response , Method invokeMethod ) {
92
+
55
93
Object res = null ;
56
- Parameter [] parameters = method .getParameters ();
94
+ Parameter [] parameters = invokeMethod .getParameters ();
57
95
Object [] arguments = new Object [parameters .length ];
58
96
for (int i = 0 ; i < parameters .length ; i ++) {
59
97
Parameter parameter = parameters [i ];
@@ -80,12 +118,48 @@ private Object dispatch(String path, HttpServletRequest request, HttpServletResp
80
118
}
81
119
Object obj ;
82
120
try {
83
- obj = method .getDeclaringClass ().getDeclaredConstructor ().newInstance ();
84
- res = method .invoke (obj , arguments );
121
+ obj = invokeMethod .getDeclaringClass ().getDeclaredConstructor ().newInstance ();
122
+ res = invokeMethod .invoke (obj , arguments );
123
+ } catch (Exception e ) {
124
+ InvocationTargetException targetEx = (InvocationTargetException ) e ;
125
+ Throwable trowEx = targetEx .getTargetException ();
126
+ Log .error ("方法invoke失败" , trowEx );
127
+ }
128
+ return res ;
129
+ }
130
+
131
+ private Object postDispatch (HttpServletRequest request , HttpServletResponse response , Method invokeMethod )
132
+ throws IOException {
133
+ Object res = null ;
134
+ Parameter [] parameters = invokeMethod .getParameters ();
135
+ Object [] arguments = new Object [parameters .length ];
136
+ for (int i = 0 ; i < parameters .length ; i ++) {
137
+ Parameter parameter = parameters [i ];
138
+ Log .info ("参数类型" , parameter .getType ());
139
+ Class <?> parameterClass = parameter .getType ();
140
+ if (parameterClass == HttpServletRequest .class ) {
141
+ arguments [i ] = request ;
142
+ } else if (parameterClass == HttpServletResponse .class ) {
143
+ arguments [i ] = response ;
144
+ } else if (parameterClass == HttpSession .class ) {
145
+ arguments [i ] = request .getSession ();
146
+ } else {
147
+ // 读取JSON并解析为JavaBean:
148
+ request .setCharacterEncoding ("UTF-8" );
149
+ BufferedReader reader = request .getReader ();
150
+ Gson gson = new Gson ();
151
+ arguments [i ] = gson .fromJson (reader , parameterClass );
152
+ }
153
+ }
154
+ Object obj ;
155
+ try {
156
+ obj = invokeMethod .getDeclaringClass ().getDeclaredConstructor ().newInstance ();
157
+ res = invokeMethod .invoke (obj , arguments );
85
158
} catch (Exception e ) {
86
159
Log .error ("方法invoke失败" , e );
87
160
}
88
161
return res ;
162
+
89
163
}
90
164
91
165
private String getOrDefault (HttpServletRequest request , String name , String defaultValue ) {
@@ -108,22 +182,27 @@ private String handInvokeRes(Object obj) {
108
182
return jsonRes ;
109
183
}
110
184
111
- private Method getMaps (String path ) throws NullRouteException {
112
-
113
- String packageName = "com.vison.webmvc.controller" ;
114
- ConfigurationBuilder config = new ConfigurationBuilder ();
115
- config .filterInputsBy (new FilterBuilder ().includePackage (packageName ));
116
- config .addUrls (ClasspathHelper .forPackage (packageName ));
117
- config .setScanners (new MethodAnnotationsScanner ());
118
- Reflections f = new Reflections (config );
119
- Set <Method > resources = f .getMethodsAnnotatedWith (GetMapping .class );
185
+ private Method getMaps (String path , RequestMethod requestMethod ) throws NullRouteException {
120
186
121
- for (Method method : resources ) {
122
- GetMapping annotation = method .getAnnotation (GetMapping .class );
123
- if (path .equals (annotation .path ())) {
124
- return method ;
187
+ if (requestMethod == RequestMethod .GET ) {
188
+ Set <Method > resources = f .getMethodsAnnotatedWith (GetMapping .class );
189
+ for (Method method : resources ) {
190
+ GetMapping annotation = method .getAnnotation (GetMapping .class );
191
+ if (annotation .path ().equals (path )) {
192
+ return method ;
193
+ }
125
194
}
126
195
}
196
+ if (requestMethod == RequestMethod .POST ) {
197
+ Set <Method > resources = f .getMethodsAnnotatedWith (PostMapping .class );
198
+ for (Method method : resources ) {
199
+ PostMapping annotation = method .getAnnotation (PostMapping .class );
200
+ if (annotation .path ().equals (path )) {
201
+ return method ;
202
+ }
203
+ }
204
+ }
205
+
127
206
throw new NullRouteException ();
128
207
}
129
208
}
0 commit comments