|
| 1 | +package com.oembedler.moon.altair.boot; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.apache.commons.lang3.text.StrSubstitutor; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.core.env.Environment; |
| 10 | +import org.springframework.core.io.ClassPathResource; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.stereotype.Controller; |
| 13 | +import org.springframework.util.StreamUtils; |
| 14 | +import org.springframework.web.bind.annotation.PathVariable; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | + |
| 18 | +import javax.annotation.PostConstruct; |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.nio.charset.Charset; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Moncef AOUDIA |
| 30 | + */ |
| 31 | +@Controller |
| 32 | +public class AltairController { |
| 33 | + |
| 34 | + private static final String CDN_UNPKG = "//unpkg.com/"; |
| 35 | + private static final String ALTAIR = "altair-static"; |
| 36 | + |
| 37 | + @Value("${altair.endpoint.graphql:/graphql}") |
| 38 | + private String graphqlEndpoint; |
| 39 | + |
| 40 | + @Value("${altair.endpoint.subscriptions:/subscriptions}") |
| 41 | + private String subscriptionsEndpoint; |
| 42 | + |
| 43 | + @Value("${altair.static.basePath:/}") |
| 44 | + private String staticBasePath; |
| 45 | + |
| 46 | + @Value("${altair.pageTitle:Altair}") |
| 47 | + private String pageTitle; |
| 48 | + |
| 49 | + @Value("${altair.cdn.enabled:false}") |
| 50 | + private Boolean altairCdnEnabled; |
| 51 | + |
| 52 | + @Value("${altair.cdn.version:2.1.1}") |
| 53 | + private String altairCdnVersion; |
| 54 | + |
| 55 | + @Autowired |
| 56 | + private Environment environment; |
| 57 | + |
| 58 | + private String template; |
| 59 | + private String props; |
| 60 | + |
| 61 | + @PostConstruct |
| 62 | + public void onceConstructed() throws IOException { |
| 63 | + loadTemplate(); |
| 64 | + loadProps(); |
| 65 | + } |
| 66 | + |
| 67 | + private void loadTemplate() throws IOException { |
| 68 | + try (InputStream inputStream = new ClassPathResource("altair.html").getInputStream()) { |
| 69 | + template = StreamUtils.copyToString(inputStream, Charset.defaultCharset()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void loadProps() throws IOException { |
| 74 | + props = new PropsLoader(environment).load(); |
| 75 | + } |
| 76 | + |
| 77 | + private void addIfAbsent(Properties headerProperties, String header) { |
| 78 | + if (!headerProperties.containsKey(header)) { |
| 79 | + headerProperties.setProperty(header, MediaType.APPLICATION_JSON_VALUE); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @RequestMapping(value = "${altair.mapping:/altair}") |
| 84 | + public void altair(HttpServletRequest request, HttpServletResponse response, @PathVariable Map<String, String> params) throws IOException { |
| 85 | + response.setContentType("text/html; charset=UTF-8"); |
| 86 | + |
| 87 | + Map<String, String> replacements = getReplacements( |
| 88 | + constructGraphQlEndpoint(request, params), |
| 89 | + request.getContextPath() + subscriptionsEndpoint |
| 90 | + ); |
| 91 | + |
| 92 | + String populatedTemplate = StrSubstitutor.replace(template, replacements); |
| 93 | + response.getOutputStream().write(populatedTemplate.getBytes(Charset.defaultCharset())); |
| 94 | + } |
| 95 | + |
| 96 | + private Map<String, String> getReplacements(String graphqlEndpoint, String subscriptionsEndpoint) { |
| 97 | + Map<String, String> replacements = new HashMap<>(); |
| 98 | + replacements.put("graphqlEndpoint", graphqlEndpoint); |
| 99 | + replacements.put("subscriptionsEndpoint", subscriptionsEndpoint); |
| 100 | + replacements.put("pageTitle", pageTitle); |
| 101 | + replacements.put("pageFavicon", getResourceUrl("favicon.ico", "favicon.ico")); |
| 102 | + replacements.put("altairBaseUrl", getResourceUrl("/vendor/altair/", |
| 103 | + joinJsUnpkgPath(ALTAIR, altairCdnVersion, "build/dist/"))); |
| 104 | + replacements.put("altairLogoUrl", getResourceUrl("assets/img/logo_350.svg", "assets/img/logo_350.svg")); |
| 105 | + replacements.put("altairCssUrl", getResourceUrl("styles.css", "styles.css")); |
| 106 | + replacements.put("altairMainJsUrl", getResourceUrl("main.js", "main.js")); |
| 107 | + replacements.put("altairPolyfillsJsUrl", getResourceUrl("polyfills.js", "polyfills.js")); |
| 108 | + replacements.put("altairRuntimeJsUrl", getResourceUrl("runtime.js", "runtime.js")); |
| 109 | + replacements.put("props", props); |
| 110 | + return replacements; |
| 111 | + } |
| 112 | + |
| 113 | + private String getResourceUrl(String staticFileName, String cdnUrl) { |
| 114 | + if (altairCdnEnabled && StringUtils.isNotBlank(cdnUrl)) { |
| 115 | + return cdnUrl; |
| 116 | + } |
| 117 | + return staticFileName; |
| 118 | + } |
| 119 | + |
| 120 | + private String joinJsUnpkgPath(String library, String cdnVersion, String cdnFileName) { |
| 121 | + return CDN_UNPKG + library + "@" + cdnVersion + "/" + cdnFileName; |
| 122 | + } |
| 123 | + |
| 124 | + private String constructGraphQlEndpoint(HttpServletRequest request, @RequestParam Map<String, String> params) { |
| 125 | + String endpoint = graphqlEndpoint; |
| 126 | + for (Map.Entry<String, String> param : params.entrySet()) { |
| 127 | + endpoint = endpoint.replaceAll("\\{" + param.getKey() + "}", param.getValue()); |
| 128 | + } |
| 129 | + if (StringUtils.isNotBlank(request.getContextPath()) && !endpoint.startsWith(request.getContextPath())) { |
| 130 | + return request.getContextPath() + endpoint; |
| 131 | + } |
| 132 | + return endpoint; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments