Skip to content

Commit

Permalink
Upgrade spring boot version (#1289)
Browse files Browse the repository at this point in the history
* Update gradle wrapper version

* Update spring boot version to 2.5.0-M2

* Fix wrong const of temp_dir

* Refactor error controller

* Fix startup error due to theme not found

* Refine error controller handler

* Refine multipart resolver config

* Fix ThemeRepositoryImplTest error

* Fix freemarker not found error

* chore: change jetty to undertow.

* Remove useless throws

Co-authored-by: Ryan Wang <[email protected]>
  • Loading branch information
JohnNiang and ruibaby authored Mar 5, 2021
1 parent 696a9ad commit c22348e
Show file tree
Hide file tree
Showing 23 changed files with 311 additions and 351 deletions.
11 changes: 4 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "org.springframework.boot" version "2.4.2"
id "org.springframework.boot" version "2.5.0-M2"
id "io.spring.dependency-management" version "1.0.11.RELEASE"
id "checkstyle"
id "java"
Expand All @@ -8,11 +8,7 @@ plugins {
group = "run.halo.app"
version = "1.4.5"
description = "Halo, An excellent open source blog publishing application."

java {
archivesBaseName = "halo"
sourceCompatibility = JavaVersion.VERSION_11
}
sourceCompatibility = JavaVersion.VERSION_11

checkstyle {
toolVersion = "8.39"
Expand All @@ -26,6 +22,7 @@ repositories {
// url "https://maven.aliyun.com/nexus/content/groups/public"
// }
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
jcenter()
}

Expand Down Expand Up @@ -111,7 +108,7 @@ dependencies {
implementation "org.springframework.boot:spring-boot-starter-actuator"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-jetty"
implementation "org.springframework.boot:spring-boot-starter-undertow"
implementation "org.springframework.boot:spring-boot-starter-freemarker"
implementation 'org.springframework.boot:spring-boot-starter-validation'

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 7 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
pluginManagement {
repositories {
maven { url 'https://repo.spring.io/milestone' }
gradlePluginPortal()
}
}

rootProject.name = 'halo'
62 changes: 14 additions & 48 deletions src/main/java/run/halo/app/config/HaloMvcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
import static run.halo.app.utils.HaloUtils.ensureSuffix;

import com.fasterxml.jackson.databind.ObjectMapper;
import freemarker.core.TemplateClassResolver;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -25,13 +21,15 @@
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jackson.JsonComponentModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileUrlResource;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
Expand All @@ -41,6 +39,7 @@
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
Expand All @@ -49,7 +48,6 @@
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.core.PageJacksonSerializer;
Expand Down Expand Up @@ -85,7 +83,7 @@ public HaloMvcConfiguration(PageableHandlerMethodArgumentResolver pageableResolv
this.haloProperties = haloProperties;
}

@Bean
// @Bean
public Map<String, TemplateModel> freemarkerLayoutDirectives() {
Map<String, TemplateModel> freemarkerLayoutDirectives = new HashMap<>();
freemarkerLayoutDirectives.put("extends", new ThemeExtendsDirective());
Expand All @@ -95,53 +93,16 @@ public Map<String, TemplateModel> freemarkerLayoutDirectives() {
return freemarkerLayoutDirectives;
}

/**
* Configuring freemarker template file path.
*
* @return new FreeMarkerConfigurer
*/
@Bean
FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties)
throws IOException, TemplateException {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer
.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/",
"classpath:/templates/");
configurer.setDefaultEncoding("UTF-8");

Properties properties = new Properties();
properties.setProperty("auto_import",
"/common/macro/common_macro.ftl as common,/common/macro/global_macro.ftl as global");

configurer.setFreemarkerSettings(properties);

// Predefine configuration
freemarker.template.Configuration configuration = configurer.createConfiguration();

configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);

if (haloProperties.isProductionEnv()) {
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}

configuration.setSharedVariables(new HashMap<>() {{
put("layout", freemarkerLayoutDirectives());
}
});

// Set predefined freemarker configuration
configurer.setConfiguration(configuration);

return configurer;
}

/**
* Configuring multipartResolver for large file upload..
*
* @return new multipartResolver
*/
@Bean(name = "multipartResolver")
MultipartResolver multipartResolver(MultipartProperties multipartProperties) {
@ConditionalOnProperty(prefix = "spring.servlet.multipart", name = "enabled",
havingValue = "true", matchIfMissing = true)
MultipartResolver multipartResolver(MultipartProperties multipartProperties)
throws IOException {
MultipartConfigElement multipartConfigElement = multipartProperties.createMultipartConfig();
CommonsMultipartResolver resolver = new CommonsMultipartResolver() {
@Override
Expand All @@ -156,10 +117,15 @@ public boolean isMultipart(@NonNull HttpServletRequest request) {
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxUploadSize(multipartConfigElement.getMaxRequestSize());
resolver.setMaxUploadSizePerFile(multipartConfigElement.getMaxFileSize());
var location = multipartProperties.getLocation();
if (StringUtils.hasText(location)) {
FileUrlResource resource = new FileUrlResource(location);
resolver.setUploadTempDir(resource);
}

//lazy multipart parsing, throwing parse exceptions once the application attempts to
// obtain multipart files
resolver.setResolveLazily(true);
resolver.setResolveLazily(multipartProperties.isResolveLazily());

return resolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import static run.halo.app.utils.HaloUtils.ensureSuffix;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import run.halo.app.model.enums.Mode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
Expand All @@ -12,7 +13,8 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.cache.AbstractStringCacheStore;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import run.halo.app.cache.lock.CacheLock;
import run.halo.app.controller.content.model.CategoryModel;
import run.halo.app.controller.content.model.JournalModel;
Expand Down Expand Up @@ -66,8 +68,6 @@ public class ContentContentController {

private final SheetService sheetService;

private final AbstractStringCacheStore cacheStore;

private final AuthenticationService authenticationService;

private final CategoryService categoryService;
Expand All @@ -82,7 +82,6 @@ public ContentContentController(PostModel postModel,
OptionService optionService,
PostService postService,
SheetService sheetService,
AbstractStringCacheStore cacheStore,
AuthenticationService authenticationService,
CategoryService categoryService) {
this.postModel = postModel;
Expand All @@ -95,7 +94,6 @@ public ContentContentController(PostModel postModel,
this.optionService = optionService;
this.postService = postService;
this.sheetService = sheetService;
this.cacheStore = cacheStore;
this.authenticationService = authenticationService;
this.categoryService = categoryService;
}
Expand Down Expand Up @@ -126,12 +124,14 @@ public String content(@PathVariable("prefix") String prefix,
Sheet sheet = sheetService.getBySlug(prefix);
return sheetModel.content(sheet, token, model);
}
throw new NotFoundException("Not Found");

throw buildPathNotFoundException();
}

@GetMapping("{prefix}/page/{page:\\d+}")
public String content(@PathVariable("prefix") String prefix,
@PathVariable(value = "page") Integer page,
HttpServletRequest request,
Model model) {
if (optionService.getArchivesPrefix().equals(prefix)) {
return postModel.archives(page, model);
Expand All @@ -145,7 +145,7 @@ public String content(@PathVariable("prefix") String prefix,
return photoModel.list(page, model);
}

throw new NotFoundException("Not Found");
throw buildPathNotFoundException();
}

@GetMapping("{prefix}/{slug}")
Expand Down Expand Up @@ -186,7 +186,7 @@ public String content(@PathVariable("prefix") String prefix,
return sheetModel.content(sheet, token, model);
}

throw new NotFoundException("Not Found");
throw buildPathNotFoundException();
}

@GetMapping("{prefix}/{slug}/page/{page:\\d+}")
Expand All @@ -202,7 +202,7 @@ public String content(@PathVariable("prefix") String prefix,
return tagModel.listPost(model, slug, page);
}

throw new NotFoundException("Not Found");
throw buildPathNotFoundException();
}

@GetMapping("{year:\\d+}/{month:\\d+}/{slug}")
Expand All @@ -217,7 +217,7 @@ public String content(@PathVariable("year") Integer year,
return postModel.content(post, token, model);
}

throw new NotFoundException("Not Found");
throw buildPathNotFoundException();
}

@GetMapping("{year:\\d+}/{month:\\d+}/{day:\\d+}/{slug}")
Expand All @@ -233,7 +233,7 @@ public String content(@PathVariable("year") Integer year,
return postModel.content(post, token, model);
}

throw new NotFoundException("Not Found");
throw buildPathNotFoundException();
}

@PostMapping(value = "content/{type}/{slug:.*}/authentication")
Expand All @@ -254,6 +254,17 @@ public String password(@PathVariable("type") String type,
return "redirect:" + redirectUrl;
}

private NotFoundException buildPathNotFoundException() {
var requestAttributes = RequestContextHolder.currentRequestAttributes();

var requestUri = "";
if (requestAttributes instanceof ServletRequestAttributes) {
requestUri =
((ServletRequestAttributes) requestAttributes).getRequest().getRequestURI();
}
return new NotFoundException("无法定位到该路径:" + requestUri);
}

private String doAuthenticationPost(
String slug, String password) throws UnsupportedEncodingException {
Post post = postService.getBy(PostStatus.INTIMATE, slug);
Expand Down
Loading

0 comments on commit c22348e

Please sign in to comment.