From 0fab57f6f28625d45e4fc456f9d5818b0c25cde6 Mon Sep 17 00:00:00 2001 From: Dmitriy Kopylenko Date: Mon, 27 Jan 2020 12:05:32 -0500 Subject: [PATCH] Working solution --- backend/build.gradle | 7 +++- .../ui/controller/RootUiViewController.java | 33 +++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/backend/build.gradle b/backend/build.gradle index 3c3cacec0..334c907ea 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -90,6 +90,9 @@ bootWar { ) } from(tasks.findByPath(':ui:npm_run_buildProd').outputs) { + //Copying into this particular classpath location due too + //deployment to external Tomcat would not work with /public location + //This way, it works with both embedded and extarnal Tomcat into 'WEB-INF/classes/resources' } archiveName = "${baseName}-${version}.war" @@ -106,6 +109,9 @@ bootJar { ) } from(tasks.findByPath(':ui:npm_run_buildProd').outputs) { + //Copying into this particular classpath location due too + //deployment to external Tomcat would not work with /public location + //This way, it works with both embedded and external Tomcat into 'WEB-INF/classes/resources' } archiveName = "${baseName}-${version}.jar" @@ -164,7 +170,6 @@ dependencies { //So it works on Java 9 without explicitly requiring to load that module (needed by Hibernate) runtimeOnly 'javax.xml.bind:jaxb-api:2.3.0' - // TODO: these will likely only be runtimeOnly or test scope, unless we want to ship the libraries with the final product compile "com.h2database:h2" runtimeOnly "org.postgresql:postgresql" runtimeOnly 'org.mariadb.jdbc:mariadb-java-client:2.2.0' diff --git a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/RootUiViewController.java b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/RootUiViewController.java index dabb03e55..fe9e4b246 100644 --- a/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/RootUiViewController.java +++ b/backend/src/main/java/edu/internet2/tier/shibboleth/admin/ui/controller/RootUiViewController.java @@ -10,28 +10,35 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.net.URISyntaxException; import java.nio.charset.Charset; +import java.util.stream.Collectors; @Controller public class RootUiViewController { - @Value("classpath:/resources/index.html") - Resource indexHtmlResource; - @RequestMapping("/") - //@ResponseBody - public void index(HttpServletRequest request, HttpServletResponse response) throws IOException { - //TODO: Use Jsoup lib to inject context path into DOM before writing to response buffer + public String index() { + return "redirect:/index.html"; + } - final String ctxPath = request.getContextPath(); + @RequestMapping(value = {"**/index.html", "/dashboard/**", "/metadata/**"}) + public void indexHtml(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException { + //This method is necessary in order for Angular framework to honor dynamic ServletContext + //under which shib ui application is deployed, both during initial index.html load and subsequest page refreshes + String content = new BufferedReader(new InputStreamReader(request.getServletContext() + .getResourceAsStream("/WEB-INF/classes/resources/index.html"))) + .lines() + .collect(Collectors.joining("\n")); - //This does not work! In order form Angular to kick in we need to redirect to index.html - String indexHtmlAsString = StreamUtils.copyToString(indexHtmlResource.getInputStream(), Charset.defaultCharset()); - //byte[] indexHtmlBytes = ByteStreams.toByteArray(indexHtmlResource.getInputStream()); - response.setHeader("Content-Type", "text/html"); - response.getWriter().print(indexHtmlAsString); - //os.write(ctxPath.getBytes(), 0, ctxPath.length()); + content = content.replaceFirst("", ""); + response.setContentType("text/html"); + try (OutputStream writer = response.getOutputStream()) { + writer.write(content.getBytes()); + } } }