diff --git a/build.gradle b/build.gradle index 04584221f6..44427230b0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,6 @@ +import ch.digitalfondue.jfiveparse.NodeMatcher +import ch.digitalfondue.jfiveparse.Parser +import ch.digitalfondue.jfiveparse.Selector import com.opentable.db.postgres.embedded.EmbeddedPostgres import com.opentable.db.postgres.embedded.PgBinaryResolver import org.apache.commons.io.FileUtils @@ -6,6 +9,7 @@ import org.apache.commons.lang3.SystemUtils import org.apache.tools.ant.filters.ReplaceTokens import org.springframework.jdbc.core.JdbcTemplate +import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Paths import java.time.Year @@ -21,6 +25,11 @@ buildscript { classpath 'com.opentable.components:otj-pg-embedded:0.13.1' classpath 'org.postgresql:postgresql:42.2.5' classpath "org.springframework:spring-jdbc:$springVersion" + + //this is for processing the index.html at compile time + classpath "com.github.alfio-event:alf.io-public-frontend:$alfioPublicFrontendVersion" + classpath "ch.digitalfondue.jfiveparse:jfiveparse:0.5.3" + // } repositories { @@ -28,6 +37,10 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } + mavenCentral() + maven { + url "https://jitpack.io" + } } } @@ -152,7 +165,7 @@ dependencies { compile 'org.imgscalr:imgscalr-lib:4.2' compile 'org.aspectj:aspectjweaver:1.9.4' - compile 'com.github.alfio-event:alf.io-public-frontend:6872d70529' + compile "com.github.alfio-event:alf.io-public-frontend:$alfioPublicFrontendVersion" testCompile 'com.opentable.components:otj-pg-embedded:0.13.1' @@ -229,6 +242,28 @@ processResources { properties['alfio.version'] = project.version properties['alfio.build-ts'] = ZonedDateTime.now(ZoneId.of("UTC")).format(DateTimeFormatter.ISO_ZONED_DATE_TIME) gradleProperties.withWriter { properties.store(it, null) } + + + // + // alf.io public frontend: read index.html rewrite path for css/js + + final resource = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/resources/webjars/alfio-public-frontend/$alfioPublicFrontendVersion/alfio-public-frontend/index.html") + final indexDoc = new Parser().parse(new InputStreamReader(resource, StandardCharsets.UTF_8)) + + final basePath = "webjars/alfio-public-frontend/$alfioPublicFrontendVersion/alfio-public-frontend/" + NodeMatcher scriptNodes = Selector.select().element("script").toMatcher() + + indexDoc.getAllNodesMatching(scriptNodes).stream().forEach({ + it.setAttribute("src", basePath + it.getAttribute("src")) + }) + + NodeMatcher cssNodes = Selector.select().element("link").attrValEq("rel", "stylesheet").toMatcher(); + indexDoc.getAllNodesMatching(cssNodes).stream().forEach({ + it.setAttribute("href", basePath + it.getAttribute("href")) + }) + + final alfioPublicIndex = new File((File) it.destinationDir, "alfio-public-frontend-index.html") + alfioPublicIndex.write(indexDoc.getOuterHTML(), "UTF-8", false) } } diff --git a/gradle.properties b/gradle.properties index e7fedac594..1468b9d1bc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,7 @@ log4jVersion=2.11.2 jacksonVersion=2.9.9 junitVersion=5.1.0 -systemProp.jdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" \ No newline at end of file +systemProp.jdk.tls.client.protocols="TLSv1,TLSv1.1,TLSv1.2" + +# https://jitpack.io/#alfio-event/alf.io-public-frontend -> go to commit tab, set the version +alfioPublicFrontendVersion=6872d70529 \ No newline at end of file diff --git a/src/main/java/alfio/controller/IndexController.java b/src/main/java/alfio/controller/IndexController.java index 60dd0483c5..9b7ec9c0ae 100644 --- a/src/main/java/alfio/controller/IndexController.java +++ b/src/main/java/alfio/controller/IndexController.java @@ -16,9 +16,8 @@ */ package alfio.controller; -import ch.digitalfondue.jfiveparse.*; import lombok.AllArgsConstructor; -import org.springframework.context.ApplicationContext; +import org.springframework.core.io.ClassPathResource; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StreamUtils; @@ -27,15 +26,11 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; @Controller @AllArgsConstructor public class IndexController { - private final ApplicationContext applicationContext; - @RequestMapping(value = "/", method = RequestMethod.HEAD) public ResponseEntity replyToProxy() { return ResponseEntity.ok("Up and running!"); @@ -76,30 +71,10 @@ public ResponseEntity replyToK8s() { "/event/{eventShortName}/ticket/{ticketId}/view" }, method = RequestMethod.GET) public void replyToIndex(HttpServletResponse response) throws IOException { - - // FIXME, do this at compile time... - // fetch version of alfio-public-frontend, do the transformation, etc... - final String basePath = "webjars/alfio-public-frontend/6872d70529/alfio-public-frontend/"; - var resource = applicationContext.getResource(basePath + "index.html"); - response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); - - Parser p = new Parser(); - try(var is = resource.getInputStream(); var isr = new InputStreamReader(is, StandardCharsets.UTF_8); var os = response.getOutputStream()) { - Document d = p.parse(isr); - NodeMatcher scriptNodes = Selector.select().element("script").toMatcher(); - - d.getAllNodesMatching(scriptNodes).stream().map(Element.class::cast).forEach(elem -> { - elem.setAttribute("src", basePath + elem.getAttribute("src")); - }); - - NodeMatcher cssNodes = Selector.select().element("link").attrValEq("rel", "stylesheet").toMatcher(); - d.getAllNodesMatching(cssNodes).stream().map(Element.class::cast).forEach(elem -> { - elem.setAttribute("href", basePath + elem.getAttribute("href")); - }); - - StreamUtils.copy(d.getOuterHTML(), StandardCharsets.UTF_8, os); + try (var is = new ClassPathResource("alfio-public-frontend-index.html").getInputStream(); var os = response.getOutputStream()) { + StreamUtils.copy(is, os); } } }