Skip to content

Instantly share code, notes, and snippets.

@jhiemer
Created August 29, 2012 05:19
Show Gist options
  • Save jhiemer/3507073 to your computer and use it in GitHub Desktop.
Save jhiemer/3507073 to your computer and use it in GitHub Desktop.
Handler<Object, ResponseEntity<?>> entityHandler = new Handler<Object, ResponseEntity<?>>() {
@Override public ResponseEntity<?> handle(Object linkedEntity) {
if(attrMeta.isCollectionLike()) {
Collection c = new ArrayList();
Collection current = attrMeta.asCollection(entity);
if(request.getMethod() == HttpMethod.POST && null != current) {
c.addAll(current);
}
c.add(linkedEntity);
attrMeta.set(c, entity);
} else if(attrMeta.isSetLike()) {
Set s = new HashSet();
Set current = attrMeta.asSet(entity);
if(request.getMethod() == HttpMethod.POST && null != current) {
s.addAll(current);
}
s.add(linkedEntity);
attrMeta.set(s, entity);
} else if(attrMeta.isMapLike()) {
Map m = new HashMap();
Map current = attrMeta.asMap(entity);
if(request.getMethod() == HttpMethod.POST && null != current) {
m.putAll(current);
}
String key = rel.get();
if(null == key) {
throw new IllegalArgumentException("Map key cannot be null (usually the 'rel' value of a JSON object).");
}
m.put(rel.get(), linkedEntity);
attrMeta.set(m, entity);
} else {
// Don't support POST when it's a single value
if(request.getMethod() == HttpMethod.POST) {
try {
return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null);
} catch(IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
attrMeta.set(linkedEntity, entity);
}
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment