Commit d116329f authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Added error pages

parent 2ad586a1
Loading
Loading
Loading
Loading
Loading
+85 −0
Original line number Diff line number Diff line
package it.inaf.ia2.vospace.ui.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Scanner;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class ErrorController extends AbstractErrorController {

    @Value("${support.contact.label}")
    private String supportContactLabel;
    @Value("${support.contact.email}")
    private String supportContactEmail;

    @Autowired
    public ErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public void errorHtml(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, Object> errors = super.getErrorAttributes(request, true);

        HttpStatus status = getStatus(request);

        String responseText;
        if (status == HttpStatus.NOT_FOUND) {
            responseText = getFileContent("404.html");
        } else {
            responseText = getFileContent("error.html")
                    .replace("#ERROR_TITLE#", (String) errors.get("error"))
                    .replace("#ERROR_MESSAGE#", (String) errors.get("message"))
                    .replace("#ADDITIONAL_MESSAGE#", getAdditionalMessage(status));
        }

        response.setContentType("text/html;charset=UTF-8");
        response.getOutputStream().print(responseText);
    }

    private String getAdditionalMessage(HttpStatus status) {
        if (status.is5xxServerError()) {
            // unexpected error -> let users report the issue
            return "<br/>If you need support please contact"
                    + " <a href=\"mailto:" + supportContactEmail + "\">" + supportContactLabel + "</a>";
        }
        return "";
    }

    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);
        if (status == HttpStatus.NO_CONTENT) {
            return new ResponseEntity<>(status);
        }
        Map<String, Object> body = getErrorAttributes(request, false);
        return new ResponseEntity<>(body, status);
    }

    private String getFileContent(String templateFileName) throws IOException {
        try (InputStream in = ErrorController.class.getClassLoader()
                .getResourceAsStream("public/error/" + templateFileName)) {
            Scanner s = new Scanner(in).useDelimiter("\\A");
            return s.hasNext() ? s.next() : "";
        }
    }

    @Override
    public String getErrorPath() {
        return null;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -11,3 +11,6 @@ cors.allowed.origin=http://localhost:8080
logging.level.it.inaf=TRACE

trusted.eppn.scope=inaf.it

support.contact.label=IA2 team
support.contact.email=ia2@inaf.it
+14 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
    <head>
        <title>Page Not Found</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous" />
    </head>
    <body>
        <div class="container mt-4">
            <h1 class="mb-3 text-primary">Page Not Found</h1>
        </div>
    </body>
</html>
+16 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
    <head>
        <title>#ERROR_TITLE#</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous" />
    </head>
    <body>
        <div class="container mt-4">
            <h1 class="mb-3 text-danger">#ERROR_TITLE#</h1>
            <p><strong>#ERROR_MESSAGE#</strong></p>
            <p>#ADDITIONAL_MESSAGE#</p>
        </div>
    </body>
</html>