Skip to content

Commit

Permalink
Refactor CommonController.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed May 13, 2019
1 parent 7b480ca commit 363acf4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Page<PostCommentWithPostVO> pageBy(@PageableDefault(sort = "updateTime",

@GetMapping("latest")
@ApiOperation("Pages latest comments")
public List<PostCommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top,
public List<PostCommentWithPostVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top,
@RequestParam(name = "status", required = false) CommentStatus status) {
// Get latest comment
List<PostComment> content = postCommentService.pageLatest(top, status).getContent();
Expand Down
44 changes: 6 additions & 38 deletions src/main/java/run/halo/app/controller/core/CommonController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import run.halo.app.model.entity.User;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.ThemeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
* Error page Controller
Expand All @@ -29,8 +26,6 @@ public class CommonController implements ErrorController {

private static final String INTERNAL_ERROR_TEMPLATE = "500.ftl";

private static final String ADMIN_URL = "/admin";

private final ThemeService themeService;

public CommonController(ThemeService themeService) {
Expand All @@ -44,13 +39,9 @@ public CommonController(ThemeService themeService) {
* @return String
*/
@GetMapping(value = ERROR_PATH)
public String handleError(HttpServletRequest request, HttpSession session) {
public String handleError(HttpServletRequest request) {
final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

final String requestURI = request.getRequestURI();

final User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);

log.error("Error path: [{}], status: [{}]", getErrorPath(), statusCode);

// Get the exception
Expand All @@ -62,40 +53,17 @@ public String handleError(HttpServletRequest request, HttpSession session) {
if (StringUtils.startsWithIgnoreCase(throwable.getMessage(), "Could not resolve view with name '")) {
// TODO May cause unknown-reason problem
// if Ftl was not found then redirect to /404
if (requestURI.contains(ADMIN_URL) && null != user) {
return "redirect:/admin/404";
} else {
return "redirect:/404";
}
return "redirect:/404";
}
}
if (requestURI.contains(ADMIN_URL) && null != user) {
return "redirect:/admin/500";
} else {

if (statusCode == 500) {
return "redirect:/500";
} else {
return "redirect:/404";
}
}

/**
* Render 404 error page
*
* @return template path:
*/
@GetMapping(value = "/admin/404")
public String adminNotFround() {
return "common/error/404";
}

/**
* Render 500 error page
*
* @return template path:
*/
@GetMapping(value = "/admin/500")
public String adminInternalError() {
return "common/error/500";
}

/**
* Render 404 error page
*
Expand Down

0 comments on commit 363acf4

Please sign in to comment.