Commit d40455a3 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

712: Implement error handling

Task-Url: https://minfba.de.dariah.eu/mantisbt/view.php?id=712
parent c545bad2
......@@ -3,11 +3,11 @@ package eu.dariah.de.dariahsp.sample.controller;
import java.io.IOException;
import java.util.List;
import javax.activity.InvalidActivityException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.logging.Property;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -17,9 +17,11 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -64,6 +66,13 @@ public class HomeController {
return "common/logout";
}
@RequestMapping(value = {"/throw/{code}", "/throw"}, method = RequestMethod.GET)
public void throwError(@PathVariable(required=false) Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
//response.sendError(code==null?HttpStatus.NOT_FOUND.value():code);
throw new Exception();
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String getLogin(@RequestParam(value = "error", required = false) String error, @RequestParam(value = "url", defaultValue = "/") String url, HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
if (saml) {
......
package eu.dariah.de.dariahsp.sample.exceptions;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/error")
public class SampleExceptionController {
@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public String getHome(HttpServletResponse response) throws IOException {
return "error";
}
}
package eu.dariah.de.dariahsp.sample.exceptions;
import javax.activity.InvalidActivityException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.http.HttpStatus;
@ControllerAdvice
public class SampleExceptionHandler {
public static final String DEFAULT_ERROR_VIEW = "error";
@ResponseStatus(HttpStatus.CONFLICT) // 409
@ExceptionHandler(InvalidActivityException.class)
public void handleConflict() {
// Nothing to do
System.out.println("ex");
}
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
// If the exception is annotated with @ResponseStatus rethrow it and let
// the framework handle it - like the OrderNotFoundException example
// at the start of this post.
// AnnotationUtils is a Spring Framework utility class.
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
throw e;
// Otherwise setup and send the user to a default error-view.
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
}
......@@ -37,5 +37,6 @@
</bean>
<context:component-scan base-package="eu.dariah.de.dariahsp.sample.controller" />
<context:component-scan base-package="eu.dariah.de.dariahsp.sample.exceptions" />
<context:component-scan base-package="eu.dariah.de.dariahsp.saml.web.controller" />
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>dariahsp-sample</display-name>
<context-param>
......@@ -13,6 +12,9 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<location>/errors</location>
</error-page>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment