5
5
import static org .apache .commons .lang3 .StringUtils .isNumeric ;
6
6
7
7
import com .fasterxml .jackson .annotation .JsonInclude .Include ;
8
- import com .fasterxml .jackson .core .JsonProcessingException ;
9
8
import com .fasterxml .jackson .databind .ObjectMapper ;
10
- import graphql .kickstart .autoconfigure .editor .PropertyGroupReader ;
11
- import graphql .kickstart .autoconfigure .editor .PropsLoader ;
12
9
import java .io .IOException ;
13
10
import java .io .InputStream ;
14
11
import java .nio .charset .Charset ;
15
12
import java .util .HashMap ;
16
13
import java .util .Map ;
17
- import java .util .Properties ;
18
14
import javax .annotation .PostConstruct ;
19
- import javax .servlet .http .HttpServletRequest ;
20
15
import javax .servlet .http .HttpServletResponse ;
21
16
import lombok .SneakyThrows ;
22
17
import lombok .extern .slf4j .Slf4j ;
23
18
import lombok .val ;
24
19
import org .apache .commons .lang3 .StringUtils ;
25
20
import org .apache .commons .text .StringSubstitutor ;
26
21
import org .springframework .beans .factory .annotation .Autowired ;
27
- import org .springframework .core .env .Environment ;
28
22
import org .springframework .core .io .ClassPathResource ;
29
23
import org .springframework .stereotype .Controller ;
30
24
import org .springframework .util .StreamUtils ;
31
25
import org .springframework .web .bind .annotation .GetMapping ;
32
- import org .springframework .web .bind .annotation .PathVariable ;
33
- import org .springframework .web .bind .annotation .RequestParam ;
34
26
35
27
/** @author Moncef AOUDIA */
36
28
@ Slf4j
@@ -39,23 +31,18 @@ public class AltairController {
39
31
40
32
private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/" ;
41
33
private static final String ALTAIR = "altair-static" ;
42
-
34
+ private final ObjectMapper objectMapper = new ObjectMapper ();
43
35
@ Autowired private AltairProperties altairProperties ;
44
36
@ Autowired private AltairOptions altairOptions ;
45
- private final ObjectMapper objectMapper = new ObjectMapper ();
46
-
47
- @ Autowired private Environment environment ;
37
+ @ Autowired private AltairResources altairResources ;
48
38
49
39
private String template ;
50
- private String props ;
51
- private String headers ;
52
40
53
41
@ PostConstruct
54
42
public void onceConstructed () throws IOException {
55
43
objectMapper .setSerializationInclusion (Include .NON_NULL );
44
+ altairResources .load (altairOptions );
56
45
loadTemplate ();
57
- loadProps ();
58
- loadHeaders ();
59
46
}
60
47
61
48
private void loadTemplate () throws IOException {
@@ -65,57 +52,33 @@ private void loadTemplate() throws IOException {
65
52
}
66
53
}
67
54
68
- private void loadProps () throws IOException {
69
- props = new PropsLoader (environment , "graphql.altair.props.resources." , "graphql.altair.props.variables." ).load ();
70
- }
71
-
72
- private void loadHeaders () throws JsonProcessingException {
73
- PropertyGroupReader propertyReader = new PropertyGroupReader (environment , "graphql.altair.headers." );
74
- Properties headerProperties = propertyReader .load ();
75
- this .headers = new ObjectMapper ().writeValueAsString (headerProperties );
76
- }
77
-
78
55
@ GetMapping (value = "${graphql.altair.mapping:/altair}" )
79
- public void altair (
80
- HttpServletRequest request ,
81
- HttpServletResponse response ,
82
- @ PathVariable Map <String , String > params )
83
- throws IOException {
56
+ public void altair (HttpServletResponse response ) throws IOException {
84
57
response .setContentType ("text/html; charset=UTF-8" );
85
-
86
- Map <String , String > replacements =
87
- getReplacements (
88
- constructGraphQlEndpoint (request , params ),
89
- request .getContextPath () + altairProperties .getEndpoint ().getSubscriptions ());
90
-
91
- String populatedTemplate = StringSubstitutor .replace (template , replacements );
58
+ String populatedTemplate = StringSubstitutor .replace (template , getReplacements ());
92
59
response .getOutputStream ().write (populatedTemplate .getBytes (Charset .defaultCharset ()));
93
60
}
94
61
95
62
@ SneakyThrows
96
- private Map <String , String > getReplacements (
97
- String graphqlEndpoint , String subscriptionsEndpoint ) {
63
+ private Map <String , String > getReplacements () {
98
64
Map <String , String > replacements = new HashMap <>();
99
- replacements .put ("graphqlEndpoint" , graphqlEndpoint );
100
- replacements .put ("subscriptionsEndpoint" , subscriptionsEndpoint );
101
65
replacements .put ("pageTitle" , altairProperties .getPageTitle ());
102
66
replacements .put ("pageFavicon" , getResourceUrl ("favicon.ico" , "favicon.ico" ));
103
67
replacements .put (
104
68
"altairBaseUrl" ,
105
69
getResourceUrl (
106
70
StringUtils .join (altairProperties .getBasePath (), "/vendor/altair/" ),
107
- joinJsUnpkgPath ( ALTAIR , altairProperties .getCdn ().getVersion (), "build/dist/" )));
71
+ joinJsdelivrPath ( altairProperties .getCdn ().getVersion ())));
108
72
replacements .put (
109
73
"altairLogoUrl" , getResourceUrl ("assets/img/logo_350.svg" , "assets/img/logo_350.svg" ));
110
74
replacements .put ("altairCssUrl" , getResourceUrl ("styles.css" , "styles.css" ));
111
75
112
76
val suffix = isJsSuffixAdded () ? "-es2018.js" : ".js" ;
113
77
replacements .put ("altairMainJsUrl" , getResourceUrl ("main-es2018.js" , "main" + suffix ));
114
- replacements .put ("altairPolyfillsJsUrl" , getResourceUrl ("polyfills-es2018.js" , "polyfills" + suffix ));
78
+ replacements .put (
79
+ "altairPolyfillsJsUrl" , getResourceUrl ("polyfills-es2018.js" , "polyfills" + suffix ));
115
80
replacements .put ("altairRuntimeJsUrl" , getResourceUrl ("runtime-es2018.js" , "runtime" + suffix ));
116
- replacements .put ("props" , props );
117
81
replacements .put ("options" , objectMapper .writeValueAsString (altairOptions ));
118
- replacements .put ("headers" , headers );
119
82
return replacements ;
120
83
}
121
84
@@ -134,20 +97,7 @@ private String getResourceUrl(String staticFileName, String cdnUrl) {
134
97
return staticFileName ;
135
98
}
136
99
137
- private String joinJsUnpkgPath (String library , String cdnVersion , String cdnFileName ) {
138
- return CDN_JSDELIVR_NET_NPM + library + "@" + cdnVersion + "/" + cdnFileName ;
139
- }
140
-
141
- private String constructGraphQlEndpoint (
142
- HttpServletRequest request , @ RequestParam Map <String , String > params ) {
143
- String endpoint = altairProperties .getEndpoint ().getGraphql ();
144
- for (Map .Entry <String , String > param : params .entrySet ()) {
145
- endpoint = endpoint .replaceAll ("\\ {" + param .getKey () + "}" , param .getValue ());
146
- }
147
- if (StringUtils .isNotBlank (request .getContextPath ())
148
- && !endpoint .startsWith (request .getContextPath ())) {
149
- return request .getContextPath () + endpoint ;
150
- }
151
- return endpoint ;
100
+ private String joinJsdelivrPath (String cdnVersion ) {
101
+ return CDN_JSDELIVR_NET_NPM + AltairController .ALTAIR + "@" + cdnVersion + "/build/dist/" ;
152
102
}
153
103
}
0 commit comments