exception handling for rest services
This commit is contained in:
parent
c306e338db
commit
897e47f926
@ -3,10 +3,14 @@
|
|||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.owasp.webgoat.service;
|
package org.owasp.webgoat.service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -14,5 +18,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
*/
|
*/
|
||||||
@RequestMapping("/service")
|
@RequestMapping("/service")
|
||||||
public abstract class BaseService {
|
public abstract class BaseService {
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
@ResponseStatus(value = HttpStatus.I_AM_A_TEAPOT)
|
||||||
|
public @ResponseBody
|
||||||
|
ExceptionInfo handleException(HttpServletRequest request, Exception ex) {
|
||||||
|
|
||||||
|
ExceptionInfo response = new ExceptionInfo();
|
||||||
|
response.setUrl(request.getRequestURL().toString());
|
||||||
|
response.setMessage(ex.getMessage());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
32
java/org/owasp/webgoat/service/ExceptionInfo.java
Normal file
32
java/org/owasp/webgoat/service/ExceptionInfo.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package org.owasp.webgoat.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author rlawson
|
||||||
|
*/
|
||||||
|
public class ExceptionInfo {
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
@ -35,7 +35,7 @@ public class HintService extends BaseService {
|
|||||||
WebSession ws;
|
WebSession ws;
|
||||||
Object o = session.getAttribute(WebSession.SESSION);
|
Object o = session.getAttribute(WebSession.SESSION);
|
||||||
if (o == null || !(o instanceof WebSession)) {
|
if (o == null || !(o instanceof WebSession)) {
|
||||||
return null;
|
throw new IllegalArgumentException("No valid session object found, has session timed out?");
|
||||||
}
|
}
|
||||||
ws = (WebSession) o;
|
ws = (WebSession) o;
|
||||||
AbstractLesson l = ws.getCurrentLesson();
|
AbstractLesson l = ws.getCurrentLesson();
|
||||||
|
@ -35,12 +35,12 @@ public class LessonMenuService extends BaseService {
|
|||||||
@RequestMapping(value = "/lessonmenu.mvc", produces = "application/json")
|
@RequestMapping(value = "/lessonmenu.mvc", produces = "application/json")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
List<LessonMenuItem> showLeftNav(HttpSession session) {
|
List<LessonMenuItem> showLeftNav(HttpSession session) {
|
||||||
//TODO - need Links, rank, title
|
if(true) throw new IllegalArgumentException("No valid session object found, has session timed out?");
|
||||||
List<LessonMenuItem> menu = new ArrayList<LessonMenuItem>();
|
List<LessonMenuItem> menu = new ArrayList<LessonMenuItem>();
|
||||||
WebSession ws;
|
WebSession ws;
|
||||||
Object o = session.getAttribute(WebSession.SESSION);
|
Object o = session.getAttribute(WebSession.SESSION);
|
||||||
if (o == null || !(o instanceof WebSession)) {
|
if (o == null || !(o instanceof WebSession)) {
|
||||||
return null;
|
throw new IllegalArgumentException("No valid session object found, has session timed out?");
|
||||||
}
|
}
|
||||||
ws = (WebSession) o;
|
ws = (WebSession) o;
|
||||||
AbstractLesson l = ws.getCurrentLesson();
|
AbstractLesson l = ws.getCurrentLesson();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user