diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java index b306515a..8231c3cd 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java @@ -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"); } } diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java b/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java new file mode 100644 index 00000000..201c4eb3 --- /dev/null +++ b/wise-webapp/src/main/java/com/wisemapping/rest/BaseController.java @@ -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(); + } +} diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java index 5b1219ca..d68556ef 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java @@ -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"); } + } diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java b/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java index cbbea9bf..2c4a1819 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/TransformerController.java @@ -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";