1
1
package graphql .kickstart .servlet ;
2
2
3
- import com .fasterxml .jackson .core .JsonProcessingException ;
4
3
import graphql .GraphQLException ;
5
4
import graphql .kickstart .execution .input .GraphQLInvocationInput ;
6
5
import java .io .IOException ;
@@ -15,7 +14,7 @@ class HttpRequestHandlerImpl implements HttpRequestHandler {
15
14
private final GraphQLConfiguration configuration ;
16
15
private final HttpRequestInvoker requestInvoker ;
17
16
18
- public HttpRequestHandlerImpl (GraphQLConfiguration configuration ) {
17
+ HttpRequestHandlerImpl (GraphQLConfiguration configuration ) {
19
18
this (
20
19
configuration ,
21
20
new HttpRequestInvokerImpl (
@@ -24,28 +23,30 @@ public HttpRequestHandlerImpl(GraphQLConfiguration configuration) {
24
23
new QueryResponseWriterFactoryImpl ()));
25
24
}
26
25
27
- public HttpRequestHandlerImpl (
26
+ HttpRequestHandlerImpl (
28
27
GraphQLConfiguration configuration , HttpRequestInvoker requestInvoker ) {
29
28
this .configuration = configuration ;
30
29
this .requestInvoker = requestInvoker ;
31
30
}
32
31
33
32
@ Override
34
33
public void handle (HttpServletRequest request , HttpServletResponse response ) throws IOException {
34
+ if (request .getCharacterEncoding () == null ) {
35
+ request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
36
+ }
37
+
38
+ ListenerHandler listenerHandler =
39
+ ListenerHandler .start (request , response , configuration .getListeners ());
40
+
35
41
try {
36
- if (request .getCharacterEncoding () == null ) {
37
- request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
38
- }
39
- GraphQLInvocationInputParser invocationInputParser =
40
- GraphQLInvocationInputParser .create (
41
- request ,
42
- configuration .getInvocationInputFactory (),
43
- configuration .getObjectMapper (),
44
- configuration .getContextSetting ());
45
- GraphQLInvocationInput invocationInput =
46
- invocationInputParser . getGraphQLInvocationInput (request , response );
47
- requestInvoker .execute (invocationInput , request , response );
48
- } catch (GraphQLException | JsonProcessingException e ) {
42
+ GraphQLInvocationInput invocationInput = parseInvocationInput (request , response );
43
+ requestInvoker .execute (invocationInput , request , response , listenerHandler );
44
+ } catch (InvocationInputParseException e ) {
45
+ response .setStatus (STATUS_BAD_REQUEST );
46
+ log .info ("Bad request: cannot parse http request" , e );
47
+ listenerHandler .onParseError (e );
48
+ throw e ;
49
+ } catch (GraphQLException e ) {
49
50
response .setStatus (STATUS_BAD_REQUEST );
50
51
log .info ("Bad request: cannot handle http request" , e );
51
52
throw e ;
@@ -55,4 +56,20 @@ public void handle(HttpServletRequest request, HttpServletResponse response) thr
55
56
throw t ;
56
57
}
57
58
}
59
+
60
+ private GraphQLInvocationInput parseInvocationInput (
61
+ HttpServletRequest request ,
62
+ HttpServletResponse response ) {
63
+ try {
64
+ GraphQLInvocationInputParser invocationInputParser =
65
+ GraphQLInvocationInputParser .create (
66
+ request ,
67
+ configuration .getInvocationInputFactory (),
68
+ configuration .getObjectMapper (),
69
+ configuration .getContextSetting ());
70
+ return invocationInputParser .getGraphQLInvocationInput (request , response );
71
+ } catch (Exception e ) {
72
+ throw new InvocationInputParseException (e );
73
+ }
74
+ }
58
75
}
0 commit comments