Skip to content

Instantly share code, notes, and snippets.

@vuk-nikolic
Created February 1, 2012 10:00
Show Gist options
  • Select an option

  • Save vuk-nikolic/1716302 to your computer and use it in GitHub Desktop.

Select an option

Save vuk-nikolic/1716302 to your computer and use it in GitHub Desktop.
JsonActionInterceptor
package com.klopaj.interceptor;
import com.klopaj.domain.Poi;
import com.klopaj.interceptor.meta.JsonAction;
import com.klopaj.interceptor.meta.JsonIn;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import jodd.madvoc.ActionRequest;
import jodd.madvoc.interceptor.ActionInterceptor;
import jodd.servlet.ServletUtil;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
public class JsonActionInterceptor extends ActionInterceptor {
@Override
public Object intercept(ActionRequest actionRequest) throws Exception {
HttpServletRequest request = actionRequest.getHttpServletRequest();
Object action = actionRequest.getAction();
Method method = actionRequest.getActionConfig().getActionClassMethod();
if (method.isAnnotationPresent(JsonAction.class)) {
// if this is a json action, get all @JsonIn fields, and try to parse them from input
String jsonStringValue = ServletUtil.readRequestBody(request);
Object fieldValues = new JSONDeserializer<Object>().deserialize(jsonStringValue);
Field[] fields = action.getClass().getDeclaredFields();
for (Field field : fields) {
if (field != null) {
if (field.isAnnotationPresent(JsonIn.class)) {
// This field is marked as @JsonIn meaning it should be in the content of the response
Class fieldType = field.getType();
if (fieldType == String.class || fieldType.isPrimitive()) {
// it's simple string, just try to get that value from json
Object fieldValue = getFieldValue(field.getName(), fieldValues);
field.setAccessible(true);
field.set(action, fieldValue);
} else if (fieldType.isArray()) {
// Do some array magic
} else if (fieldType.isEnum()) {
// Convert value to enum
} else if (List.class.isAssignableFrom(fieldType)) {
// Handle lists and similar
System.out.println("It's a collection");
List fieldValue = (List) getFieldValue(field.getName(), fieldValues);
field.setAccessible(true);
field.set(action, fieldValue);
for (Object object : fieldValue) {
if (object != null) {
System.out.println("Collection item " + object);
}
}
} else {
// Most likely this is a POJO.
Object fieldValue = getFieldValue(field.getName(), fieldValues);
String objectSerialized = new JSONSerializer().exclude("*.class").serialize(fieldValue);
Object fieldValueObject = new JSONDeserializer().deserialize(objectSerialized, fieldType);
field.setAccessible(true);
field.set(action, fieldValueObject);
}
}
}
}
}
return actionRequest.invoke();
}
private Object getFieldValue(String fieldName, Object fieldValues) {
if (fieldValues instanceof List) {
List listFieldValue = (List) fieldValues;
} else if (fieldValues instanceof Map) {
Map mapFieldValue = (Map) fieldValues;
return mapFieldValue.get(fieldName);
} else {
// some primitive type?
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment