1212
1313import javax .servlet .AsyncContext ;
1414import javax .servlet .Servlet ;
15+ import javax .servlet .ServletConfig ;
1516import javax .servlet .ServletException ;
1617import javax .servlet .http .HttpServlet ;
1718import javax .servlet .http .HttpServletRequest ;
2425import java .io .Writer ;
2526import java .util .ArrayList ;
2627import java .util .Arrays ;
27- import java .util .Collections ;
2828import java .util .HashMap ;
2929import java .util .List ;
3030import java .util .Map ;
@@ -50,31 +50,66 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
5050 private static final GraphQLRequest INTROSPECTION_REQUEST = new GraphQLRequest (IntrospectionQuery .INTROSPECTION_QUERY , new HashMap <>(), null );
5151 private static final String [] MULTIPART_KEYS = new String []{"operations" , "graphql" , "query" };
5252
53+ private GraphQLConfiguration configuration ;
54+
55+ /**
56+ * @deprecated override {@link #getConfiguration()} instead
57+ */
58+ @ Deprecated
5359 protected abstract GraphQLQueryInvoker getQueryInvoker ();
5460
61+ /**
62+ * @deprecated override {@link #getConfiguration()} instead
63+ */
64+ @ Deprecated
5565 protected abstract GraphQLInvocationInputFactory getInvocationInputFactory ();
5666
67+ /**
68+ * @deprecated override {@link #getConfiguration()} instead
69+ */
70+ @ Deprecated
5771 protected abstract GraphQLObjectMapper getGraphQLObjectMapper ();
5872
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+ }
6087
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 ;
6393
64- private final boolean asyncServletMode ;
94+ private HttpRequestHandler getHandler ;
95+ private HttpRequestHandler postHandler ;
6596
6697 public AbstractGraphQLHttpServlet () {
67- this (null , false );
98+ this (null );
6899 }
69100
70- public AbstractGraphQLHttpServlet (List <GraphQLServletListener > listeners , boolean asyncServletMode ) {
101+ public AbstractGraphQLHttpServlet (List <GraphQLServletListener > listeners ) {
71102 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 ();
73108
74109 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 ();
78113
79114 String path = request .getPathInfo ();
80115 if (path == null ) {
@@ -106,22 +141,22 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
106141 };
107142
108143 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 ();
112147
113148 try {
114149 if (APPLICATION_GRAPHQL .equals (request .getContentType ())) {
115150 String query = CharStreams .toString (request .getReader ());
116151 query (queryInvoker , graphQLObjectMapper , invocationInputFactory .create (new GraphQLRequest (query , null , null )), response );
117152 } else if (request .getContentType () != null && request .getContentType ().startsWith ("multipart/form-data" ) && !request .getParts ().isEmpty ()) {
118153 final Map <String , List <Part >> fileItems = request .getParts ()
119- .stream ()
120- .collect (Collectors .groupingBy (Part ::getName ));
154+ .stream ()
155+ .collect (Collectors .groupingBy (Part ::getName ));
121156
122157 for (String key : MULTIPART_KEYS ) {
123158 // Check to see if there is a part under the key we seek
124- if (!fileItems .containsKey (key )) {
159+ if (!fileItems .containsKey (key )) {
125160 continue ;
126161 }
127162
@@ -134,28 +169,28 @@ public AbstractGraphQLHttpServlet(List<GraphQLServletListener> listeners, boolea
134169 InputStream inputStream = asMarkableInputStream (queryItem .get ().getInputStream ());
135170
136171 final Optional <Map <String , List <String >>> variablesMap =
137- getFileItem (fileItems , "map" ).map (graphQLObjectMapper ::deserializeMultipartMap );
172+ getFileItem (fileItems , "map" ).map (graphQLObjectMapper ::deserializeMultipartMap );
138173
139174 if (isBatchedQuery (inputStream )) {
140175 List <GraphQLRequest > graphQLRequests =
141- graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
176+ graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
142177 variablesMap .ifPresent (map -> graphQLRequests .forEach (r -> mapMultipartVariables (r , map , fileItems )));
143178 GraphQLBatchedInvocationInput invocationInput =
144- invocationInputFactory .create (graphQLRequests , request , response );
179+ invocationInputFactory .create (graphQLRequests , request , response );
145180 invocationInput .getContext ().setParts (fileItems );
146181 queryBatched (queryInvoker , graphQLObjectMapper , invocationInput , response );
147182 return ;
148183 } else {
149184 GraphQLRequest graphQLRequest ;
150- if ("query" .equals (key )) {
185+ if ("query" .equals (key )) {
151186 graphQLRequest = buildRequestFromQuery (inputStream , graphQLObjectMapper , fileItems );
152187 } else {
153188 graphQLRequest = graphQLObjectMapper .readGraphQLRequest (inputStream );
154189 }
155190
156191 variablesMap .ifPresent (m -> mapMultipartVariables (graphQLRequest , m , fileItems ));
157192 GraphQLSingleInvocationInput invocationInput =
158- invocationInputFactory .create (graphQLRequest , request , response );
193+ invocationInputFactory .create (graphQLRequest , request , response );
159194 invocationInput .getContext ().setParts (fileItems );
160195 query (queryInvoker , graphQLObjectMapper , invocationInput , response );
161196 return ;
@@ -190,8 +225,7 @@ private static InputStream asMarkableInputStream(InputStream inputStream) {
190225
191226 private GraphQLRequest buildRequestFromQuery (InputStream inputStream ,
192227 GraphQLObjectMapper graphQLObjectMapper ,
193- Map <String , List <Part >> fileItems ) throws IOException
194- {
228+ Map <String , List <Part >> fileItems ) throws IOException {
195229 GraphQLRequest graphQLRequest ;
196230 String query = new String (ByteStreams .toByteArray (inputStream ));
197231
@@ -213,49 +247,48 @@ private GraphQLRequest buildRequestFromQuery(InputStream inputStream,
213247
214248 private void mapMultipartVariables (GraphQLRequest request ,
215249 Map <String , List <String >> variablesMap ,
216- Map <String , List <Part >> fileItems )
217- {
250+ Map <String , List <Part >> fileItems ) {
218251 Map <String , Object > variables = request .getVariables ();
219252
220253 variablesMap .forEach ((partName , objectPaths ) -> {
221254 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" ));
225258
226259 objectPaths .forEach (objectPath -> VariableMapper .mapVariable (objectPath , variables , part ));
227260 });
228261 }
229262
230263 public void addListener (GraphQLServletListener servletListener ) {
231- listeners .add (servletListener );
264+ configuration .add (servletListener );
232265 }
233266
234267 public void removeListener (GraphQLServletListener servletListener ) {
235- listeners .remove (servletListener );
268+ configuration .remove (servletListener );
236269 }
237270
238271 @ Override
239272 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 );
241274 }
242275
243276 @ Override
244277 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 );
246279 }
247280
248281 @ Override
249282 public String executeQuery (String query ) {
250283 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 ))));
252285 } catch (Exception e ) {
253286 return e .getMessage ();
254287 }
255288 }
256289
257290 private void doRequestAsync (HttpServletRequest request , HttpServletResponse response , HttpRequestHandler handler ) {
258- if (asyncServletMode ) {
291+ if (configuration . isAsyncServletModeEnabled () ) {
259292 AsyncContext asyncContext = request .startAsync ();
260293 HttpServletRequest asyncRequest = (HttpServletRequest ) asyncContext .getRequest ();
261294 HttpServletResponse asyncResponse = (HttpServletResponse ) asyncContext .getResponse ();
@@ -324,11 +357,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper
324357 }
325358
326359 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 ()
332361 .map (listener -> {
333362 try {
334363 return action .apply (listener );
0 commit comments