12
12
13
13
import javax .servlet .AsyncContext ;
14
14
import javax .servlet .Servlet ;
15
+ import javax .servlet .ServletConfig ;
15
16
import javax .servlet .ServletException ;
16
17
import javax .servlet .http .HttpServlet ;
17
18
import javax .servlet .http .HttpServletRequest ;
24
25
import java .io .Writer ;
25
26
import java .util .ArrayList ;
26
27
import java .util .Arrays ;
27
- import java .util .Collections ;
28
28
import java .util .HashMap ;
29
29
import java .util .List ;
30
30
import java .util .Map ;
@@ -50,31 +50,66 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
50
50
private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest (IntrospectionQuery .INTROSPECTION_QUERY , new HashMap <>(), null );
51
51
private static final String [] MULTIPART_KEYS = new String []{"operations" , "graphql" , "query" };
52
52
53
+ private GraphQLConfiguration configuration ;
54
+
55
+ /**
56
+ * @deprecated override {@link #getConfiguration()} instead
57
+ */
58
+ @ Deprecated
53
59
protected abstract GraphQLQueryInvoker getQueryInvoker ();
54
60
61
+ /**
62
+ * @deprecated override {@link #getConfiguration()} instead
63
+ */
64
+ @ Deprecated
55
65
protected abstract GraphQLInvocationInputFactory getInvocationInputFactory ();
56
66
67
+ /**
68
+ * @deprecated override {@link #getConfiguration()} instead
69
+ */
70
+ @ Deprecated
57
71
protected abstract GraphQLObjectMapper getGraphQLObjectMapper ();
58
72
59
- private final List <GraphQLServletListener > listeners ;
73
+ /**
74
+ * @deprecated override {@link #getConfiguration()} instead
75
+ */
76
+ @ Deprecated
77
+ protected abstract boolean isAsyncServletMode ();
78
+
79
+ protected GraphQLConfiguration getConfiguration () {
80
+ return GraphQLConfiguration .with (getInvocationInputFactory ())
81
+ .with (getQueryInvoker ())
82
+ .with (getGraphQLObjectMapper ())
83
+ .with (isAsyncServletMode ())
84
+ .with (listeners )
85
+ .build ();
86
+ }
60
87
61
- private final HttpRequestHandler getHandler ;
62
- private final HttpRequestHandler postHandler ;
88
+ /**
89
+ * @deprecated use {@link #getConfiguration()} instead
90
+ */
91
+ @ Deprecated
92
+ private final List <GraphQLServletListener > listeners ;
63
93
64
- private final boolean asyncServletMode ;
94
+ private HttpRequestHandler getHandler ;
95
+ private HttpRequestHandler postHandler ;
65
96
66
97
public AbstractGraphQLHttpServlet () {
67
- this (null , false );
98
+ this (null );
68
99
}
69
100
70
- public AbstractGraphQLHttpServlet (List <GraphQLServletListener > listeners , boolean asyncServletMode ) {
101
+ public AbstractGraphQLHttpServlet (List <GraphQLServletListener > listeners ) {
71
102
this .listeners = listeners != null ? new ArrayList <>(listeners ) : new ArrayList <>();
72
- this .asyncServletMode = asyncServletMode ;
103
+ }
104
+
105
+ @ Override
106
+ public void init (ServletConfig servletConfig ) {
107
+ this .configuration = getConfiguration ();
73
108
74
109
this .getHandler = (request , response ) -> {
75
- GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory ();
76
- GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper ();
77
- GraphQLQueryInvoker queryInvoker = getQueryInvoker ();
110
+ GraphQLInvocationInputFactory invocationInputFactory = configuration . getInvocationInputFactory ();
111
+ GraphQLObjectMapper graphQLObjectMapper = configuration . getObjectMapper ();
112
+ GraphQLQueryInvoker queryInvoker = configuration . getQueryInvoker ();
78
113
79
114
String path = request .getPathInfo ();
80
115
if (path == null ) {
@@ -106,22 +141,22 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
106
141
};
107
142
108
143
this .postHandler = (request , response ) -> {
109
- GraphQLInvocationInputFactory invocationInputFactory = getInvocationInputFactory ();
110
- GraphQLObjectMapper graphQLObjectMapper = getGraphQLObjectMapper ();
111
- GraphQLQueryInvoker queryInvoker = getQueryInvoker ();
144
+ GraphQLInvocationInputFactory invocationInputFactory = configuration . getInvocationInputFactory ();
145
+ GraphQLObjectMapper graphQLObjectMapper = configuration . getObjectMapper ();
146
+ GraphQLQueryInvoker queryInvoker = configuration . getQueryInvoker ();
112
147
113
148
try {
114
149
if (APPLICATION_GRAPHQL .equals (request .getContentType ())) {
115
150
String query = CharStreams .toString (request .getReader ());
116
151
query (queryInvoker , graphQLObjectMapper , invocationInputFactory .create (new GraphQLRequest (query , null , null )), response );
117
152
} else if (request .getContentType () != null && request .getContentType ().startsWith ("multipart/form-data" ) && !request .getParts ().isEmpty ()) {
118
153
final Map <String , List <Part >> fileItems = request .getParts ()
119
- .stream ()
120
- .collect (Collectors .groupingBy (Part ::getName ));
154
+ .stream ()
155
+ .collect (Collectors .groupingBy (Part ::getName ));
121
156
122
157
for (String key : MULTIPART_KEYS ) {
123
158
// Check to see if there is a part under the key we seek
124
- if (!fileItems .containsKey (key )) {
159
+ if (!fileItems .containsKey (key )) {
125
160
continue ;
126
161
}
127
162
@@ -134,28 +169,28 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
134
169
InputStream inputStream = asMarkableInputStream (queryItem .get ().getInputStream ());
135
170
136
171
final Optional <Map <String , List <String >>> variablesMap =
137
- getFileItem (fileItems , "map" ).map (graphQLObjectMapper ::deserializeMultipartMap );
172
+ getFileItem (fileItems , "map" ).map (graphQLObjectMapper ::deserializeMultipartMap );
138
173
139
174
if (isBatchedQuery (inputStream )) {
140
175
List <GraphQLRequest > graphQLRequests =
141
- graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
176
+ graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
142
177
variablesMap .ifPresent (map -> graphQLRequests .forEach (r -> mapMultipartVariables (r , map , fileItems )));
143
178
GraphQLBatchedInvocationInput invocationInput =
144
- invocationInputFactory .create (graphQLRequests , request , response );
179
+ invocationInputFactory .create (graphQLRequests , request , response );
145
180
invocationInput .getContext ().setParts (fileItems );
146
181
queryBatched (queryInvoker , graphQLObjectMapper , invocationInput , response );
147
182
return ;
148
183
} else {
149
184
GraphQLRequest graphQLRequest ;
150
- if ("query" .equals (key )) {
185
+ if ("query" .equals (key )) {
151
186
graphQLRequest = buildRequestFromQuery (inputStream , graphQLObjectMapper , fileItems );
152
187
} else {
153
188
graphQLRequest = graphQLObjectMapper .readGraphQLRequest (inputStream );
154
189
}
155
190
156
191
variablesMap .ifPresent (m -> mapMultipartVariables (graphQLRequest , m , fileItems ));
157
192
GraphQLSingleInvocationInput invocationInput =
158
- invocationInputFactory .create (graphQLRequest , request , response );
193
+ invocationInputFactory .create (graphQLRequest , request , response );
159
194
invocationInput .getContext ().setParts (fileItems );
160
195
query (queryInvoker , graphQLObjectMapper , invocationInput , response );
161
196
return ;
@@ -190,8 +225,7 @@ private static InputStream asMarkableInputStream(InputStream inputStream) {
190
225
191
226
private GraphQLRequest buildRequestFromQuery (InputStream inputStream ,
192
227
GraphQLObjectMapper graphQLObjectMapper ,
193
- Map <String , List <Part >> fileItems ) throws IOException
194
- {
228
+ Map <String , List <Part >> fileItems ) throws IOException {
195
229
GraphQLRequest graphQLRequest ;
196
230
String query = new String (ByteStreams .toByteArray (inputStream ));
197
231
@@ -213,49 +247,48 @@ private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
213
247
214
248
private void mapMultipartVariables (GraphQLRequest request ,
215
249
Map <String , List <String >> variablesMap ,
216
- Map <String , List <Part >> fileItems )
217
- {
250
+ Map <String , List <Part >> fileItems ) {
218
251
Map <String , Object > variables = request .getVariables ();
219
252
220
253
variablesMap .forEach ((partName , objectPaths ) -> {
221
254
Part part = getFileItem (fileItems , partName )
222
- .orElseThrow (() -> new RuntimeException ("unable to find part name " +
223
- partName +
224
- " as referenced in the variables map" ));
255
+ .orElseThrow (() -> new RuntimeException ("unable to find part name " +
256
+ partName +
257
+ " as referenced in the variables map" ));
225
258
226
259
objectPaths .forEach (objectPath -> VariableMapper .mapVariable (objectPath , variables , part ));
227
260
});
228
261
}
229
262
230
263
public void addListener (GraphQLServletListener servletListener ) {
231
- listeners .add (servletListener );
264
+ configuration .add (servletListener );
232
265
}
233
266
234
267
public void removeListener (GraphQLServletListener servletListener ) {
235
- listeners .remove (servletListener );
268
+ configuration .remove (servletListener );
236
269
}
237
270
238
271
@ Override
239
272
public String [] getQueries () {
240
- return getInvocationInputFactory ().getSchemaProvider ().getSchema ().getQueryType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
273
+ return configuration . getInvocationInputFactory ().getSchemaProvider ().getSchema ().getQueryType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
241
274
}
242
275
243
276
@ Override
244
277
public String [] getMutations () {
245
- return getInvocationInputFactory ().getSchemaProvider ().getSchema ().getMutationType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
278
+ return configuration . getInvocationInputFactory ().getSchemaProvider ().getSchema ().getMutationType ().getFieldDefinitions ().stream ().map (GraphQLFieldDefinition ::getName ).toArray (String []::new );
246
279
}
247
280
248
281
@ Override
249
282
public String executeQuery (String query ) {
250
283
try {
251
- return getGraphQLObjectMapper ().serializeResultAsJson (getQueryInvoker ().query (getInvocationInputFactory ().create (new GraphQLRequest (query , new HashMap <>(), null ))));
284
+ return configuration . getObjectMapper ().serializeResultAsJson (configuration . getQueryInvoker ().query (configuration . getInvocationInputFactory ().create (new GraphQLRequest (query , new HashMap <>(), null ))));
252
285
} catch (Exception e ) {
253
286
return e .getMessage ();
254
287
}
255
288
}
256
289
257
290
private void doRequestAsync (HttpServletRequest request , HttpServletResponse response , HttpRequestHandler handler ) {
258
- if (asyncServletMode ) {
291
+ if (configuration . isAsyncServletModeEnabled () ) {
259
292
AsyncContext asyncContext = request .startAsync ();
260
293
HttpServletRequest asyncRequest = (HttpServletRequest ) asyncContext .getRequest ();
261
294
HttpServletResponse asyncResponse = (HttpServletResponse ) asyncContext .getResponse ();
@@ -324,11 +357,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper
324
357
}
325
358
326
359
private <R > List <R > runListeners (Function <? super GraphQLServletListener , R > action ) {
327
- if (listeners == null ) {
328
- return Collections .emptyList ();
329
- }
330
-
331
- return listeners .stream ()
360
+ return configuration .getListeners ().stream ()
332
361
.map (listener -> {
333
362
try {
334
363
return action .apply (listener );
0 commit comments