Skip to content

Commit

Permalink
process alfio-public-frontend index.html at compile time #657
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jun 14, 2019
1 parent e31f09d commit 96f54eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
37 changes: 36 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -21,13 +25,22 @@ 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 {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
maven {
url "https://jitpack.io"
}
}
}

Expand Down Expand Up @@ -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'

Expand Down Expand Up @@ -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)
}
}

Expand Down
5 changes: 4 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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
31 changes: 3 additions & 28 deletions src/main/java/alfio/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> replyToProxy() {
return ResponseEntity.ok("Up and running!");
Expand Down Expand Up @@ -76,30 +71,10 @@ public ResponseEntity<String> 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);
}
}
}

0 comments on commit 96f54eb

Please sign in to comment.