personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit 486ee6538e5470b35f5c8e3addf81175cc753323
parent 34f70ea2e78752b25f827cdc614a1b96025b5649
Author: WimDupont <WimDupont@users.noreply.gitlab.com>
Date:   Fri,  3 Sep 2021 22:34:08 +0200

removed http redirect and thymeleaf xml config + small cleanup

Diffstat:
Mpom.xml | 10+++++-----
Msrc/main/java/com/wimdupont/personalweb/PersonalWebApplication.java | 8+-------
Dsrc/main/java/com/wimdupont/personalweb/config/ServerConfig.java | 41-----------------------------------------
Dsrc/main/java/com/wimdupont/personalweb/config/ThymeleafConfig.java | 44--------------------------------------------
Msrc/main/java/com/wimdupont/personalweb/controller/BlogController.java | 2+-
Msrc/main/java/com/wimdupont/personalweb/controller/BookController.java | 14+++++++-------
Msrc/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java | 18+++++++++++++-----
Msrc/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java | 4+++-
8 files changed, 30 insertions(+), 111 deletions(-)

diff --git a/pom.xml b/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> - <version>2.5.3</version> + <version>2.5.4</version> </parent> <groupId>com.wimdupont</groupId> <artifactId>personalweb</artifactId> @@ -13,11 +13,11 @@ <name>PersonalWeb</name> <description>Wim Dupont's personal website</description> - <properties> <java.version>11</java.version> - <flyway.maven.plugin.version>7.12.1</flyway.maven.plugin.version> - <asciidoctorj.version>2.5.1</asciidoctorj.version> + <flyway.maven.plugin.version>7.14.1</flyway.maven.plugin.version> + <asciidoctorj.version>2.5.2</asciidoctorj.version> + <rome.version>1.0</rome.version> <rome.rometools.version>1.16.0</rome.rometools.version> </properties> @@ -78,7 +78,7 @@ <dependency> <groupId>rome</groupId> <artifactId>rome</artifactId> - <version>1.0</version> + <version>${rome.version}</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> diff --git a/src/main/java/com/wimdupont/personalweb/PersonalWebApplication.java b/src/main/java/com/wimdupont/personalweb/PersonalWebApplication.java @@ -7,8 +7,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; -import java.io.IOException; - @SpringBootApplication @EnableScheduling @ComponentScan @@ -16,10 +14,6 @@ public class PersonalWebApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(PersonalWebApplication.class, args); - try { - context.getBean(BlogArticleGenerator.class).generate(); - } catch (IOException e) { - e.printStackTrace(); - } + context.getBean(BlogArticleGenerator.class).generate(); } } diff --git a/src/main/java/com/wimdupont/personalweb/config/ServerConfig.java b/src/main/java/com/wimdupont/personalweb/config/ServerConfig.java @@ -1,41 +0,0 @@ -package com.wimdupont.personalweb.config; - -import org.apache.catalina.Context; -import org.apache.catalina.connector.Connector; -import org.apache.tomcat.util.descriptor.web.SecurityCollection; -import org.apache.tomcat.util.descriptor.web.SecurityConstraint; -import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; -import org.springframework.boot.web.servlet.server.ServletWebServerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -@SuppressWarnings("unused") -public class ServerConfig { - - @Bean - public ServletWebServerFactory servletContainer() { - TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { - @Override - protected void postProcessContext(Context context) { - SecurityConstraint securityConstraint = new SecurityConstraint(); - securityConstraint.setUserConstraint("CONFIDENTIAL"); - SecurityCollection collection = new SecurityCollection(); - collection.addPattern("/*"); - securityConstraint.addCollection(collection); - context.addConstraint(securityConstraint); - } - }; - tomcat.addAdditionalTomcatConnectors(getHttpConnector()); - return tomcat; - } - - private Connector getHttpConnector() { - Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); - connector.setScheme("http"); - connector.setPort(8081); - connector.setSecure(false); - connector.setRedirectPort(8443); - return connector; - } -} diff --git a/src/main/java/com/wimdupont/personalweb/config/ThymeleafConfig.java b/src/main/java/com/wimdupont/personalweb/config/ThymeleafConfig.java @@ -1,44 +0,0 @@ -package com.wimdupont.personalweb.config; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; -import org.thymeleaf.templatemode.TemplateMode; - -@Configuration -@SuppressWarnings("unused") -public class ThymeleafConfig { - - @Bean(name = "htmlTemplateResolver") - SpringResourceTemplateResolver htmlTemplateResolver(ApplicationContext appCtx) { - SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); - - templateResolver.setApplicationContext(appCtx); - templateResolver.setPrefix("classpath:/templates/"); - templateResolver.setSuffix(".html"); - templateResolver.setTemplateMode(TemplateMode.HTML); - templateResolver.setCharacterEncoding("UTF-8"); - templateResolver.setOrder(0); - templateResolver.setCacheable(false); - templateResolver.setCheckExistence(true); - - return templateResolver; - } - - @Bean(name = "xmlTemplateResolver") - SpringResourceTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) { - SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); - - templateResolver.setApplicationContext(appCtx); - templateResolver.setPrefix("classpath:/templates/"); - templateResolver.setSuffix(".xml"); - templateResolver.setTemplateMode("XML"); - templateResolver.setCharacterEncoding("UTF-8"); - templateResolver.setOrder(1); - templateResolver.setCacheable(false); - templateResolver.setCheckExistence(true); - - return templateResolver; - } -} diff --git a/src/main/java/com/wimdupont/personalweb/controller/BlogController.java b/src/main/java/com/wimdupont/personalweb/controller/BlogController.java @@ -45,7 +45,7 @@ public class BlogController { article = Files.readString(Paths.get(String.format("%s/html/%s%s", Constants.ARTICLES_DIRECTORY, name, ".html"))); } catch (IOException e) { article = String.format("Article with name \"%s\" not found.", name); - log.warn(e.getMessage(), e); + log.warn(article); } model.addAttribute("article", article); model.addAttribute("title", name); diff --git a/src/main/java/com/wimdupont/personalweb/controller/BookController.java b/src/main/java/com/wimdupont/personalweb/controller/BookController.java @@ -24,13 +24,13 @@ public class BookController { @GetMapping @SuppressWarnings("unused") public String getBooks(Model model) { - List<BookDto> bookDtoList = bookService.findAll().stream().map(bookToBookDtoConverter::convert).collect(Collectors.toList()); - bookDtoList = bookDtoList.stream().sorted(Comparator.comparing(BookDto::getCategory) - .thenComparing(BookDto::getAuthor) - .thenComparing(BookDto::getSeries, Comparator.nullsFirst(Comparator.naturalOrder())) - .thenComparing(BookDto::getSeriesNumber, Comparator.nullsFirst(Comparator.naturalOrder())) - .thenComparing(BookDto::getTitle) - ).collect(Collectors.toList()); + List<BookDto> bookDtoList = bookService.findAll().stream().map(bookToBookDtoConverter::convert) + .sorted(Comparator.comparing(BookDto::getCategory) + .thenComparing(BookDto::getAuthor) + .thenComparing(BookDto::getSeries, Comparator.nullsFirst(Comparator.naturalOrder())) + .thenComparing(BookDto::getSeriesNumber, Comparator.nullsFirst(Comparator.naturalOrder())) + .thenComparing(BookDto::getTitle) + ).collect(Collectors.toList()); model.addAttribute("books", bookDtoList); return "books"; diff --git a/src/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java b/src/main/java/com/wimdupont/personalweb/service/BlogArticleGenerator.java @@ -2,6 +2,7 @@ package com.wimdupont.personalweb.service; import com.wimdupont.personalweb.util.Constants; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -12,6 +13,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; +@Slf4j @Service @Transactional @RequiredArgsConstructor @@ -20,15 +22,21 @@ public class BlogArticleGenerator { private final AdocConverter adocConverter; @Scheduled(cron = "0 0 0 * * *") - public void generate() throws IOException { + public void generate() { + log.info("Blog article generator started."); File[] files = new File(Constants.ARTICLES_DIRECTORY).listFiles(); if (files != null) { for (File article : files) { if (article.isFile() && article.getName().endsWith(".adoc") && !new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replace(".adoc", ".html"))).exists()) { - String htmlArticle = adocConverter.convert(article); - new File(String.format("%s/html/", Constants.ARTICLES_DIRECTORY)).mkdir(); - new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))).createNewFile(); - Files.write(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))), htmlArticle.getBytes(StandardCharsets.UTF_8)); + try { + String htmlArticle = adocConverter.convert(article); + new File(String.format("%s/html/", Constants.ARTICLES_DIRECTORY)).mkdir(); + new File(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))).createNewFile(); + Files.write(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName().replaceFirst(".adoc", ".html"))), htmlArticle.getBytes(StandardCharsets.UTF_8)); + log.info("Article [{}] has been generated.", article.getName()); + } catch (IOException e) { + log.error(e.getMessage(), e); + } } } } diff --git a/src/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java b/src/main/java/com/wimdupont/personalweb/service/RssFeedGenerator.java @@ -6,6 +6,7 @@ import com.rometools.rome.feed.rss.Guid; import com.rometools.rome.feed.rss.Item; import com.wimdupont.personalweb.util.Constants; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @@ -19,6 +20,7 @@ import java.util.Comparator; import java.util.Date; import java.util.List; +@Slf4j @Service @Transactional @RequiredArgsConstructor @@ -50,7 +52,7 @@ public class RssFeedGenerator { try { articleString = Files.readString(Paths.get(String.format("%s/html/%s", Constants.ARTICLES_DIRECTORY, article.getName()))); } catch (IOException e) { - e.printStackTrace(); + log.error(e.getMessage(), e); } String title = article.getName().replace(".html", "");