1
1
package graphql .kickstart .autoconfigure .editor .graphiql ;
2
2
3
+ import static java .util .Objects .nonNull ;
4
+
3
5
import com .fasterxml .jackson .core .JsonProcessingException ;
4
6
import com .fasterxml .jackson .databind .ObjectMapper ;
5
- import graphql .kickstart .autoconfigure .editor .PropertyGroupReader ;
6
- import graphql .kickstart .autoconfigure .editor .PropsLoader ;
7
+ import graphql .kickstart .autoconfigure .editor .graphiql . GraphiQLProperties . Props . GraphiQLVariables ;
8
+ import graphql .kickstart .autoconfigure .editor .graphiql . GraphiQLProperties . Resources ;
7
9
import java .io .IOException ;
8
10
import java .io .InputStream ;
9
11
import java .nio .charset .Charset ;
12
+ import java .nio .charset .StandardCharsets ;
13
+ import java .nio .file .Files ;
14
+ import java .util .Collections ;
10
15
import java .util .HashMap ;
11
16
import java .util .Map ;
12
- import java .util .Properties ;
17
+ import java .util .Optional ;
18
+ import lombok .RequiredArgsConstructor ;
13
19
import lombok .extern .slf4j .Slf4j ;
14
20
import org .apache .commons .lang3 .StringUtils ;
15
21
import org .apache .commons .text .StringSubstitutor ;
16
- import org .springframework .beans .factory .annotation .Autowired ;
17
- import org .springframework .core .env .Environment ;
18
22
import org .springframework .core .io .ClassPathResource ;
19
23
import org .springframework .security .web .csrf .CsrfToken ;
20
24
import org .springframework .util .StreamUtils ;
23
27
24
28
/** @author Andrew Potter */
25
29
@ Slf4j
30
+ @ RequiredArgsConstructor
26
31
public abstract class GraphiQLController {
27
32
28
33
private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/" ;
29
34
private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/" ;
30
35
private static final String GRAPHIQL = "graphiql" ;
31
36
private static final String FAVICON_GRAPHQL_ORG = "//graphql.org/img/favicon.png" ;
32
37
33
- @ Autowired private Environment environment ;
34
-
35
- @ Autowired private GraphiQLProperties graphiQLProperties ;
38
+ private final GraphiQLProperties graphiQLProperties ;
36
39
37
40
private String template ;
38
41
private String props ;
39
- private Properties headerProperties ;
40
42
41
43
public void onceConstructed () throws IOException {
42
44
loadTemplate ();
43
45
loadProps ();
44
- loadHeaders ();
45
46
}
46
47
47
48
private void loadTemplate () throws IOException {
@@ -52,35 +53,45 @@ private void loadTemplate() throws IOException {
52
53
}
53
54
54
55
private void loadProps () throws IOException {
55
- props =
56
- new PropsLoader (environment , "graphiql.props.resources." , "graphiql.props.variables." )
57
- .load ();
58
- }
59
-
60
- private void loadHeaders () {
61
- PropertyGroupReader propertyReader = new PropertyGroupReader (environment , "graphiql.headers." );
62
- headerProperties = propertyReader .load ();
56
+ Resources resources = graphiQLProperties .getProps ().getResources ();
57
+ GraphiQLVariables combinedVariables = graphiQLProperties .getProps ().getVariables ();
58
+ if (nonNull (resources .getVariables ())) {
59
+ combinedVariables = combinedVariables .withVariables (getContent (resources .getVariables ()));
60
+ }
61
+ if (nonNull (resources .getDefaultQuery ())) {
62
+ combinedVariables
63
+ = combinedVariables .withDefaultQuery (getContent (resources .getDefaultQuery ()));
64
+ }
65
+ if (nonNull (resources .getQuery ())) {
66
+ combinedVariables = combinedVariables .withQuery (getContent (resources .getQuery ()));
67
+ }
68
+ this .props = new ObjectMapper ().writeValueAsString (combinedVariables );
63
69
}
64
70
65
71
public byte [] graphiql (
66
72
String contextPath , @ PathVariable Map <String , String > params , Object csrf ) {
73
+ Map <String , String > finalHeaders = Optional .ofNullable (graphiQLProperties .getHeaders ())
74
+ .orElseGet (Collections ::emptyMap );
67
75
if (csrf != null ) {
68
76
CsrfToken csrfToken = (CsrfToken ) csrf ;
69
- headerProperties .setProperty (csrfToken .getHeaderName (), csrfToken .getToken ());
77
+ finalHeaders = new HashMap <>(finalHeaders );
78
+ finalHeaders .put (csrfToken .getHeaderName (), csrfToken .getToken ());
70
79
}
71
80
72
81
Map <String , String > replacements =
73
82
getReplacements (
74
83
constructGraphQlEndpoint (contextPath , params ),
75
84
contextPath + graphiQLProperties .getEndpoint ().getSubscriptions (),
76
- contextPath + graphiQLProperties .getBasePath ());
85
+ contextPath + graphiQLProperties .getBasePath (),
86
+ finalHeaders );
77
87
78
88
String populatedTemplate = StringSubstitutor .replace (template , replacements );
79
89
return populatedTemplate .getBytes (Charset .defaultCharset ());
80
90
}
81
91
82
92
private Map <String , String > getReplacements (
83
- String graphqlEndpoint , String subscriptionsEndpoint , String staticBasePath ) {
93
+ String graphqlEndpoint , String subscriptionsEndpoint , String staticBasePath ,
94
+ Map <String , String > headers ) {
84
95
Map <String , String > replacements = new HashMap <>();
85
96
replacements .put ("graphqlEndpoint" , graphqlEndpoint );
86
97
replacements .put ("subscriptionsEndpoint" , subscriptionsEndpoint );
@@ -137,7 +148,7 @@ private Map<String, String> getReplacements(
137
148
joinJsDelivrPath ("graphiql-subscriptions-fetcher" , "0.0.2" , "browser/client.js" )));
138
149
replacements .put ("props" , props );
139
150
try {
140
- replacements .put ("headers" , new ObjectMapper ().writeValueAsString (headerProperties ));
151
+ replacements .put ("headers" , new ObjectMapper ().writeValueAsString (headers ));
141
152
} catch (JsonProcessingException e ) {
142
153
log .error ("Cannot serialize headers" , e );
143
154
}
@@ -191,4 +202,8 @@ private String constructGraphQlEndpoint(
191
202
}
192
203
return endpoint ;
193
204
}
205
+
206
+ private String getContent (final ClassPathResource resource ) throws IOException {
207
+ return new String (Files .readAllBytes (resource .getFile ().toPath ()), StandardCharsets .UTF_8 );
208
+ }
194
209
}
0 commit comments