I have a hard time having a nice and clean way to implement this JsonParamInjectable. I searched on this forum and elsewhere but found nowhere an hint that would tell me how to implement it nice and clean.
For a jaxrs method :
public Object add(
@JsonParam("a") int a,
@JsonParam("b") int b
)
It parse a json {"a":1, "b":2} to param a and b
For this to work i implement an InjectableProvider, which create one JsonInjectable instance by method param.
@Provider
public class JsonParamProvider implements InjectableProvider<JsonParam, Type> {
private Gson gson;
public JsonParamProvider(@Context ServletConfig sc) throws Exception {
super();
this.gson = GsonFactory.newGson(sc);
}
@Override
public Injectable<Object> getInjectable(ComponentContext cc, JsonParam a, Type type) {
if (a.value() != null) {
String signature = cc.getAccesibleObject().toString();
return new JsonInjectable(signature, a, type, gson);
}
return null;
}
@Override
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
The magic is done in this JsonInjectable, and its where id did a dirty trick :
public class JsonInjectable extends AbstractHttpContextInjectable<Object> {
private final JsonParam param;
private final Type type;
private final String signature;
private final Gson gson;
private static ThreadLocal<WeakReference<HttpContext>> contextCache = new ThreadLocal<WeakReference<HttpContext>>(){
@Override
protected WeakReference<HttpContext> initialValue() {
return new WeakReference<HttpContext>(null);
}
};
private static ThreadLocal<WeakReference<JsonElement>> entityCache = new ThreadLocal<WeakReference<JsonElement>>(){
@Override
protected WeakReference<JsonElement> initialValue() {
return new WeakReference<JsonElement>(null);
}
};
public JsonInjectable(String signature, JsonParam param, Type type, Gson gson) {
this.signature = signature;
this.param = param;
this.type = type;
this.gson = gson;
}
@Override
public Object getValue(HttpContext context) {
try {
JsonElement methodJsonElement = entityCache.get().get();
HttpContext context2 = contextCache.get().get();
if (context != context2) {
contextCache.set(new WeakReference<HttpContext>(context));
String entity = context.getRequest().getEntity(String.class);
System.out.println("entity:"+entity);
JsonParser parser = new JsonParser();
methodJsonElement = parser.parse(entity);
entityCache.set(new WeakReference<JsonElement>(methodJsonElement));
}
if (methodJsonElement == null || methodJsonElement.isJsonNull()) {
return null;
}
final JsonElement valueJsonElement = ((JsonObject)methodJsonElement).get(this.param.value());
if (valueJsonElement == null || valueJsonElement.isJsonNull()) {
return null;
}
if (this.type.equals(java.lang.Integer.class)) {
Number number = valueJsonElement.getAsNumber();
return number.intValue();
}
if (this.type.equals(java.lang.String.class)) {
return valueJsonElement.getAsString();
}
Class<?> c = ((Class<?>) this.type);
if (int.class.equals(c)) {
return valueJsonElement.getAsInt();
}
//other parsing code...
//try with gson
return this.gson.fromJson(valueJsonElement, this.type);
} catch (RuntimeException e) {
throw e;
}
}
The problem is, in some case entity is empty for, i suspect, a valid http request. Resulting in java.io.EOFException: End of input at line 1 column 2. This problem arise in production, but i am unable to reproduce it in testing env.
If there is a problem, it is surely related with "context != context2". For each injectable is binded to a param and injectables are called in an order i dont control, and each injectable work on the same data : parsed json from request entity. So to avoid re-parsing entity each time, i use context != context2 to detect if its a new request.
What is the nice and clean way to detect a new request so json parsing can only occur 1 time per request.