Browse files

Adding custom validators for dynamodb models

  • Loading branch information...
1 parent c87044d commit 13305c4d57a6fb9b5e7010b45a784e48a62f322d @mboudreau committed May 9, 2012
View
2 src/main/java/com/michelboudreau/alternator/AlternatorDBController.java
@@ -27,7 +27,7 @@ public AlternatorDBController() {
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.POST, consumes = "application/x-amz-json-1.0")
@ResponseBody
- public Object alternatorDBController(HttpServletRequest request) throws IOException, ServletException {
+ public Object alternatorDBController(HttpServletRequest request) {
return handler.handle(request);
}
View
24 src/main/java/com/michelboudreau/alternator/AlternatorDBHandler.java
@@ -5,8 +5,10 @@
import com.michelboudreau.alternator.models.Item;
import com.michelboudreau.alternator.models.Table;
import com.michelboudreau.alternator.parsers.AmazonWebServiceRequestParser;
+import com.michelboudreau.alternator.validators.CreateTableRequestValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.validation.BindException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@@ -26,7 +28,7 @@ public AlternatorDBHandler() {
mapper.writeValue(new File(dbName), models);*/
}
- public Object handle(HttpServletRequest request) throws ConditionalCheckFailedException, InternalServerErrorException, ResourceInUseException, ResourceNotFoundException {
+ public Object handle(HttpServletRequest request) {
AmazonWebServiceRequestParser parser = new AmazonWebServiceRequestParser(request);
switch (parser.getType()) {
@@ -41,7 +43,7 @@ public Object handle(HttpServletRequest request) throws ConditionalCheckFailedEx
return updateTable(parser.getData(UpdateTableRequest.class, UpdateTableRequestJsonUnmarshaller.getInstance()));
case DELETE_TABLE:
return deleteTable(parser.getData(DeleteTableRequest.class, DeleteTableRequestJsonUnmarshaller.getInstance()));
-
+
// Items
case PUT:
return putItem(parser.getData(PutItemRequest.class, PutItemRequestJsonUnmarshaller.getInstance()));
@@ -68,13 +70,21 @@ public Object handle(HttpServletRequest request) throws ConditionalCheckFailedEx
return null;
}
- protected CreateTableResult createTable(CreateTableRequest request) throws InternalServerErrorException {
+ protected CreateTableResult createTable(CreateTableRequest request) {
+ CreateTableRequestValidator validator = new CreateTableRequestValidator();
+ // make sure request is filled properly
String tableName = request.getTableName();
KeySchemaElement hashKey = request.getKeySchema().getHashKeyElement();
KeySchemaElement rangeKey = request.getKeySchema().getRangeKeyElement();
- // TODO: make sure request is filled properly
+ String rangeKeyName = null;
+ String rangeKeyType = null;
+ if (rangeKey != null) {
+ rangeKeyName = rangeKey.getAttributeName();
+ rangeKeyType = rangeKey.getAttributeType();
+ }
+
if (getTable(tableName) == null) {
- Table table = new Table(hashKey.getAttributeName(), rangeKey.getAttributeName(), tableName, hashKey.getAttributeType(), rangeKey.getAttributeType());
+ Table table = new Table(hashKey.getAttributeName(), rangeKeyName, tableName, hashKey.getAttributeType(), rangeKey.getAttributeType());
this.tables.add(table);
} else {
// TODO: create error
@@ -171,11 +181,11 @@ protected GetItemResult getItem(GetItemRequest request) {
}
protected UpdateItemResult updateItem(UpdateItemRequest request) {
- return null;
+ return new UpdateItemResult();
}
protected DeleteItemResult deleteItem(DeleteItemRequest request) {
- return null;
+ return new DeleteItemResult();
}
protected BatchGetItemResult batchGetItem(BatchGetItemRequest request) {
View
6 src/main/java/com/michelboudreau/alternator/validation/Validator.java
@@ -0,0 +1,6 @@
+package com.michelboudreau.alternator.validation;
+
+public interface Validator {
+ boolean supports(Class<?> clazz);
+ Error[] validate(Object target);
+}
View
49 src/main/java/com/michelboudreau/alternator/validation/ValidatorUtils.java
@@ -0,0 +1,49 @@
+package com.michelboudreau.alternator.validation;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.util.Assert;
+
+public class ValidatorUtils {
+ private static Log logger = LogFactory.getLog(ValidatorUtils.class);
+
+ public static Error[] invokeValidator(Validator validator, Object obj) {
+ Assert.notNull(validator, "Validator must not be null");
+ if (logger.isDebugEnabled()) {
+ logger.debug("Invoking validator [" + validator + "]");
+ }
+ if (obj != null && !validator.supports(obj.getClass())) {
+ throw new IllegalArgumentException("Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
+ } else {
+ return validator.validate(obj);
+ }
+ }
+
+ public static Error rejectIfNull(String property) {
+ /*Object value = errors.getFieldValue(field);
+ if (value == null || !StringUtils.hasText(value.toString())) {
+ errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
+ }*/
+ return null;
+ }
+
+ public static Error rejectIfNullOrEmpty(String property) {
+ /*Assert.
+ rejectIfEmpty(property);
+ Object value = errors.getFieldValue(field);
+ if (value == null || !StringUtils.hasText(value.toString())) {
+ errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
+ }*/
+ return null;
+ }
+
+ public static Error rejectIfNullOrEmptyOrWhitespace(String property) {
+ /*Assert.
+ rejectIfEmpty(property);
+ Object value = errors.getFieldValue(field);
+ if (value == null || !StringUtils.hasText(value.toString())) {
+ errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
+ }*/
+ return null;
+ }
+}
View
20 src/main/java/com/michelboudreau/alternator/validators/CreateTableRequestValidator.java
@@ -0,0 +1,20 @@
+package com.michelboudreau.alternator.validators;
+
+import com.amazonaws.services.dynamodb.model.CreateTableRequest;
+import com.michelboudreau.alternator.validation.Validator;
+import com.michelboudreau.alternator.validation.ValidatorUtils;
+
+public class CreateTableRequestValidator implements Validator {
+
+ public boolean supports(Class clazz) {
+ return CreateTableRequest.class.isAssignableFrom(clazz);
+ }
+
+ public Error[] validate(Object target) {
+ CreateTableRequest instance = (CreateTableRequest) target;
+
+ ValidatorUtils.rejectIfNullOrEmptyOrWhitespace("tableName");
+ ValidatorUtils.invokeValidator(new KeySchemaValidator(), instance.getKeySchema());
+ return null;
+ }
+}
View
30 src/main/java/com/michelboudreau/alternator/validators/KeySchemaElementValidator.java
@@ -0,0 +1,30 @@
+package com.michelboudreau.alternator.validators;
+
+import com.amazonaws.services.dynamodb.model.KeySchemaElement;
+import com.michelboudreau.alternator.validation.Validator;
+import com.michelboudreau.alternator.validation.ValidatorUtils;
+
+public class KeySchemaElementValidator implements Validator {
+
+ public boolean supports(Class clazz) {
+ return KeySchemaElement.class.isAssignableFrom(clazz);
+ }
+
+ public Error[] validate(Object target) {
+ KeySchemaElement instance = (KeySchemaElement) target;
+
+ ValidatorUtils.rejectIfNullOrEmptyOrWhitespace("attributeName");
+ ValidatorUtils.rejectIfNullOrEmptyOrWhitespace("attributeType");
+
+ /*if (!errors.hasFieldErrors("attributeName")) {
+ if (instance.getAttributeName().length() > 255)
+ errors.rejectValue("attributeName", "too_long", "Length cannot exceed 255");
+ }
+
+ if (!errors.hasFieldErrors("attributeType")) {
+ if (instance.getAttributeType() != "S" && instance.getAttributeType() != "N")
+ errors.rejectValue("attributeType", "wrong_type", "Must be of type 'S' or 'N'");
+ }*/
+ return null;
+ }
+}
View
27 src/main/java/com/michelboudreau/alternator/validators/KeySchemaValidator.java
@@ -0,0 +1,27 @@
+package com.michelboudreau.alternator.validators;
+
+import com.amazonaws.services.dynamodb.model.KeySchema;
+import com.michelboudreau.alternator.validation.Validator;
+import com.michelboudreau.alternator.validation.ValidatorUtils;
+import org.springframework.validation.Errors;
+
+public class KeySchemaValidator implements Validator {
+
+ public boolean supports(Class clazz) {
+ return KeySchema.class.isAssignableFrom(clazz);
+ }
+
+ public Error[] validate(Object target) {
+ KeySchema instance = (KeySchema) target;
+ ValidatorUtils.rejectIfNullOrEmptyOrWhitespace("hashKeyElement");
+
+ if (instance.getHashKeyElement() != null) {
+ ValidatorUtils.invokeValidator(new KeySchemaElementValidator(), instance.getHashKeyElement());
+ }
+
+ if (instance.getRangeKeyElement() != null) {
+ ValidatorUtils.invokeValidator(new KeySchemaElementValidator(), instance.getRangeKeyElement());
+ }
+ return null;
+ }
+}

0 comments on commit 13305c4

Please sign in to comment.