Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #246: Header must be fetched from jakarta.ee #293

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.starter</groupId>
<artifactId>jakarta-starter-ui</artifactId>
<version>2.0.5</version>
<version>2.1.0-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
Expand Down Expand Up @@ -98,6 +98,11 @@
<artifactId>maven-resolver-connector-basic</artifactId>
<version>1.9.16</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
Expand Down Expand Up @@ -135,6 +140,41 @@
</resources>

<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>my-id-0</id>
<phase>process-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://jakarta.ee/templates/header/</url>
<outputFileName>_template_header_.html</outputFileName>
<outputDirectory>${project.build.directory}/jakarta-starter-ui</outputDirectory>
<overwrite>true</overwrite>
<skipCache>true</skipCache>
</configuration>
</execution>
<execution>
<id>my-id-1</id>
<phase>process-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://jakarta.ee/templates/footer/</url>
<outputFileName>_template_footer_.html</outputFileName>
<outputDirectory>${project.build.directory}/jakarta-starter-ui</outputDirectory>
<overwrite>true</overwrite>
<skipCache>true</skipCache>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
53 changes: 53 additions & 0 deletions ui/src/main/java/org/eclipse/starter/ui/FetchHtmlUtility.java
Copy link
Contributor

@m-reza-rahman m-reza-rahman Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be done every time? It looks like this is static. Why can't be do this in the Maven build too? Also, why can’t we just directly include the file if it can be valid XHTML?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my first time seeing this PR. My first impression is that using the JSF Facelets <ui:include> feature is an easier way to accomplish what the FetchHtmlUtility is doing.

At a very high level, using Java code to generate markup is an anti-pattern. A more current (but certainly not the only) way to do it is to keep in XHTML as much as possible and use Expression Language elements in the XHTML to pull in the parts that need to be dynamic.

This is a StackOver Flow question that talks about this. https://stackoverflow.com/questions/8947425/how-to-conditionally-include-a-file-in-my-template-using-jsf-and-facelets

Copy link
Author

@yashTEF yashTEF Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some existing XHTML code present in the topbar.xhtml which was needed along with the html code fetched from the url, some parsing along with placing the code inside ui:composition tag made me think of the above approach as it was a bit straightforward but as Reza mentioned I did not take into the consideration the performance issue of doing the above repeatedly.

I'll try to find some out of the box feature present in maven plugins which does the above so that all of this can be done during the build itself.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.eclipse.starter.ui;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@ApplicationScoped
public class FetchHtmlUtility {

private static final String FOOTER_TEMPLATE = "_template_footer_.html";
private static final String HEADER_TEMPLATE = "_template_header_.html";

Logger logger = LoggerFactory.getLogger(FetchHtmlUtility.class.getName());

public String getHeaderHtml() throws IOException {
String absoluteDiskPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(HEADER_TEMPLATE);
String content = readFile(absoluteDiskPath, Charset.defaultCharset());
Document document = Jsoup.parse(content);
Elements headerFragment = document.select("body");
StringBuilder headerHtml = new StringBuilder();
headerHtml.append(headerFragment.html());
logger.debug("element: {}", headerHtml);
return headerHtml.toString();
}

public String getFooterHtml() throws IOException {
String absoluteDiskPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(FOOTER_TEMPLATE);
String content = readFile(absoluteDiskPath, Charset.defaultCharset());
Document document = Jsoup.parse(content);
Elements footerFragment = document.select("body");
StringBuilder footerHtml = new StringBuilder();
footerHtml.append(footerFragment.html());
logger.debug("element: {}", footerFragment.html());
return footerHtml.toString();
}

private static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
100 changes: 3 additions & 97 deletions ui/src/main/webapp/WEB-INF/footer.xhtml
Original file line number Diff line number Diff line change
@@ -1,100 +1,6 @@
<ui:composition
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns="http://www.w3.org/1999/xhtml">
<footer id="solstice-footer">
<div class="container">
<div class="row">
<a class="scrollup" href="#">Back to the top</a>
<div class="col-sm-24 margin-top-20">
<div class="row">
<div class="col-md-16" id="copyright">
<p id="copyright-text">Copyright © Eclipse Foundation, Inc. All Rights Reserved.</p>
</div>
<div class="col-md-8 social-media">
<ul class="list-inline">
<li>
<a class="social-media-link fa-stack fa-lg" href="https://twitter.com/JakartaEE">
<i class="fa fa-circle-thin fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x"></i>
<span class="sr-only">Twitter account</span>
</a>
</li>
<li>
<a class="social-media-link fa-stack fa-lg"
href="https://www.facebook.com/JakartaEE">
<i class="fa fa-circle-thin fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x"></i>
<span class="sr-only">Facebook account</span>
</a>
</li>
<li>
<a class="social-media-link fa-stack fa-lg"
href="https://www.youtube.com/channel/UC4M7h5b6elu9VlzjLfzuXyg">
<i class="fa fa-circle-thin fa-stack-2x"></i>
<i class="fa fa-youtube fa-stack-1x"></i>
<span class="sr-only">Youtube account</span>
</a>
</li>
<li>
<a class="social-media-link fa-stack fa-lg"
href="https://www.linkedin.com/groups/13597511/" title="">
<i class="fa fa-circle-thin fa-stack-2x"></i>
<i class="fa fa-linkedin fa-stack-1x"></i>
<span class="sr-only">Linkedin account</span>
</a>
</li>
</ul>
</div>
</div>
</div>
<section class="col-sm-6 hidden-print" id="footer-useful-links">
<h2 class="section-title">Useful Links</h2>
<ul class="nav">
<li><a href="https://bugs.eclipse.org/bugs/">Report a Bug</a></li>
<li><a href="//help.eclipse.org/">Documentation</a></li>
<li><a href="https://www.eclipse.org/contribute/">How to Contribute</a></li>
<li><a href="https://www.eclipse.org/mail/">Mailing Lists</a></li>
<li><a href="https://www.eclipse.org/forums/">Forums</a></li>
<li><a href="//marketplace.eclipse.org">Marketplace</a></li>
</ul>
</section>
<section class="col-sm-6 hidden-print" id="footer-other">
<h2 class="section-title">Other</h2>
<ul class="nav">
<li><a href="https://www.eclipse.org/ide/">IDE and Tools</a></li>
<li><a href="https://www.eclipse.org/projects">Projects</a></li>
<li><a href="https://www.eclipse.org/org/workinggroups/">Working Groups</a></li>
<li><a href="https://www.eclipse.org/org/research/">Research@Eclipse</a></li>
<li><a href="https://www.eclipse.org/security/">Report a Vulnerability</a></li>
<li><a href="https://status.eclipse.org">Service Status</a></li>
</ul>
</section>
<section class="col-sm-6 hidden-print" id="footer-eclipse-foundation">
<h2 class="section-title">Eclipse Foundation</h2>
<ul class="nav">
<li><a href="https://www.eclipse.org/org/">About Us</a></li>
<li><a href="https://www.eclipse.org/org/foundation/contact.php">Contact Us</a></li>
<li><a href="https://www.eclipse.org/donate">Donate</a></li>
<li><a href="https://www.eclipse.org/membership">Members</a></li>
<li><a href="https://www.eclipse.org/org/documents/">Governance</a></li>
<li><a href="https://www.eclipse.org/org/documents/Community_Code_of_Conduct.php">Code
of Conduct</a></li>
<li><a href="https://www.eclipse.org/artwork/">Logo and Artwork</a></li>
<li><a href="https://www.eclipse.org/org/foundation/directors.php">Board of
Directors</a></li>
</ul>
</section>
<section class="col-sm-6 hidden-print" id="footer-legal">
<h2 class="section-title">Legal</h2>
<ul class="nav">
<li><a href="https://www.eclipse.org/legal/privacy.php">Privacy Policy</a></li>
<li><a href="https://www.eclipse.org/legal/termsofuse.php">Terms of Use</a></li>
<li><a href="https://www.eclipse.org/legal/copyright.php">Copyright Agent</a></li>
<li><a href="https://www.eclipse.org/legal/epl-2.0/">Eclipse Public License</a></li>
<li><a href="https://www.eclipse.org/legal/">Legal Resources </a></li>
</ul>
</section>
</div>
</div>
</footer>
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:outputText value="#{fetchHtmlUtility.footerHtml}" escape="false"/>
<script src="https://jakarta.ee/js/solstice.js"></script>
</ui:composition>
3 changes: 1 addition & 2 deletions ui/src/main/webapp/WEB-INF/template.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
margin-right: 10px;
}
</h:outputStylesheet>

<title>Eclipse Starter for Jakarta EE</title>

</h:head>
Expand Down Expand Up @@ -102,4 +101,4 @@

</h:body>

</html>
</html>
Loading