Commit 42431a7d authored by Matija Obreza's avatar Matija Obreza
Browse files

REST /mymethods and PUT'ting a new method on /methods

parent 526465e0
...@@ -20,10 +20,7 @@ public class RestController { ...@@ -20,10 +20,7 @@ public class RestController {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
@ResponseBody @ResponseBody
public ExceptionJson handleIOException(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException { public ExceptionJson handleIOException(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException {
LOG.error(ex, ex);
if (LOG.isDebugEnabled()) {
LOG.debug("Unexpected error: " + ex.getLocalizedMessage());
}
return new ExceptionJson(ex); return new ExceptionJson(ex);
} }
......
...@@ -16,6 +16,16 @@ ...@@ -16,6 +16,16 @@
package org.genesys2.server.servlet.controller.rest; package org.genesys2.server.servlet.controller.rest;
import java.util.List;
import javax.xml.bind.ValidationException;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import net.sf.oval.constraint.MinLength;
import net.sf.oval.constraint.NotBlank;
import net.sf.oval.constraint.NotNull;
import org.genesys2.server.exception.AuthorizationException; import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.genesys.Method; import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter; import org.genesys2.server.model.genesys.Parameter;
...@@ -29,6 +39,7 @@ import org.springframework.http.MediaType; ...@@ -29,6 +39,7 @@ import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -79,4 +90,55 @@ public class TraitsController extends RestController { ...@@ -79,4 +90,55 @@ public class TraitsController extends RestController {
return OAuth2Cleanup.clean(methods); return OAuth2Cleanup.clean(methods);
} }
@RequestMapping(value = "/mymethods", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object listMyMethods() {
LOG.info("Listing user's methods");
List<Method> methods = traitService.listMyMethods();
return OAuth2Cleanup.clean(methods);
}
/**
* Add a crop
*
* @return
* @throws ValidationException
*/
@RequestMapping(value = "/methods", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object createMethod(@RequestBody MethodJson methodJson) throws ValidationException {
LOG.info("Creating method");
Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(methodJson);
if (violations.size() > 0) {
// TODO We could do better messages on validation error
throw new ModelValidationException("Validation problem", violations);
}
Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.options,
methodJson.range, null);
return method;
}
public static class MethodJson {
@NotNull
@NotBlank
@MinLength(10)
public String description;
@NotBlank
public String unit;
@NotNull
@NotBlank
public String fieldName;
public int fieldType;
@NotBlank
public String options;
@NotBlank
public String range;
}
} }
\ No newline at end of file
Markdown is supported
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