Configure default exception handlers for REST services.

This commit is contained in:
Paulo Gustavo Veiga 2012-02-21 20:04:17 -03:00
parent 7e59b19fc0
commit f96b0d49c2
4 changed files with 43 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestUser;
import com.wisemapping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@ -13,7 +14,7 @@ import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
@Controller
public class AdminController {
public class AdminController extends BaseController {
private static final String RESPONSE_VIEW = "responseView";
@Autowired
private UserService userService;
@ -46,7 +47,7 @@ public class AdminController {
// User already exists ?
final String email = user.getEmail();
if(userService.getUserBy(email)!=null){
if (userService.getUserBy(email) != null) {
throw new IllegalArgumentException("User already exists with this email.");
}
@ -55,26 +56,28 @@ public class AdminController {
}
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
public ModelAndView changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException {
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException {
if (password == null) {
throw new IllegalArgumentException("Password can not be null");
}
final User user = userService.getUserBy(id);
if (user == null) {
throw new IllegalArgumentException("User '" + id + "' could not be found");
}
user.setPassword(password);
userService.changePassword(user);
return new ModelAndView(RESPONSE_VIEW, "message", "User '" + user.getId() + "' password has been updated.");
}
@RequestMapping(method = RequestMethod.DELETE, value = "admin/users/{id}", produces = {"application/json", "text/html", "application/xml"})
public ModelAndView getUserByEmail(@PathVariable long id) throws IOException, WiseMappingException {
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void getUserByEmail(@PathVariable long id) throws IOException, WiseMappingException {
final User user = userService.getUserBy(id);
if (user == null) {
throw new IllegalArgumentException("User '" + id + "' could not be found");
}
userService.deleteUser(user);
return new ModelAndView(RESPONSE_VIEW, "message", "User deleted successfully");
}
}

View File

@ -0,0 +1,27 @@
package com.wisemapping.rest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
public class BaseController {
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public String handleClientErrors(Exception ex) {
ex.printStackTrace();
return ex.getMessage();
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public String handleServerErrors(Exception ex) {
ex.printStackTrace();
// LOGGER.error(ex.getMessage(), ex);
return ex.getMessage();
}
}

View File

@ -10,6 +10,7 @@ import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.security.Utils;
import com.wisemapping.service.MindmapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@ -21,7 +22,7 @@ import java.util.Date;
import java.util.List;
@Controller
public class MindmapController {
public class MindmapController extends BaseController{
@Autowired
private MindmapService mindmapService;
@ -48,7 +49,8 @@ public class MindmapController {
}
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
public ModelAndView updateMap(@RequestBody RestMindmap restMindmap, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void updateMap(@RequestBody RestMindmap restMindmap, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
final MindMap mindMap = mindmapService.getMindmapById(id);
final User user = Utils.getUser();
@ -67,7 +69,6 @@ public class MindmapController {
final String xml = restMindmap.getXml();
mindMap.setXmlStr(xml);
mindmapService.updateMindmap(mindMap, minor);
return new ModelAndView("responseView", "message", "Map has been updated successfully");
}
}

View File

@ -16,7 +16,7 @@ import java.util.HashMap;
import java.util.Map;
@Controller
public class TransformerController {
public class TransformerController extends BaseController {
private static final String PARAM_SVG_XML = "svgXml";
private static final String PARAM_WISE_MAP_XML = "mapXml";