Added event handling machinery to intercept render events. It's now possible to write and event handler to influence the resource objects that are being sent to the client. There is an abstract helper class and there is support for annotated POJOs.

This commit is contained in:
Jon Brisbin
2012-08-07 10:02:16 -05:00
parent f3dde0c144
commit 88fc8ba7cd
14 changed files with 316 additions and 60 deletions

View File

@@ -65,6 +65,8 @@ import org.springframework.data.rest.repository.context.AfterSaveEvent;
import org.springframework.data.rest.repository.context.BeforeDeleteEvent;
import org.springframework.data.rest.repository.context.BeforeLinkDeleteEvent;
import org.springframework.data.rest.repository.context.BeforeLinkSaveEvent;
import org.springframework.data.rest.repository.context.BeforeRenderResourceEvent;
import org.springframework.data.rest.repository.context.BeforeRenderResourcesEvent;
import org.springframework.data.rest.repository.context.BeforeSaveEvent;
import org.springframework.data.rest.repository.context.RepositoryEvent;
import org.springframework.data.rest.repository.invoke.CrudMethod;
@@ -326,6 +328,8 @@ public class RepositoryRestController
}
}
publishEvent(new BeforeRenderResourcesEvent(request, null, resources));
return negotiateResponse(request, HttpStatus.OK, new HttpHeaders(), resources);
}
@@ -438,6 +442,8 @@ public class RepositoryRestController
buildUri(baseUri, repository, "search")));
}
publishEvent(new BeforeRenderResourcesEvent(request, repoMeta, resources));
return negotiateResponse(request, HttpStatus.OK, new HttpHeaders(), resources);
}
@@ -490,6 +496,8 @@ public class RepositoryRestController
}
}
publishEvent(new BeforeRenderResourcesEvent(request, repoMeta, resources));
return negotiateResponse(request, HttpStatus.OK, new HttpHeaders(), resources);
}
@@ -664,6 +672,8 @@ public class RepositoryRestController
}
}
publishEvent(new BeforeRenderResourcesEvent(request, repoMeta, resources));
return negotiateResponse(request, HttpStatus.OK, new HttpHeaders(), resources);
}
@@ -726,6 +736,8 @@ public class RepositoryRestController
body = resource;
}
publishEvent(new BeforeRenderResourceEvent(request, repoMeta, body));
return negotiateResponse(request, HttpStatus.CREATED, headers, body);
}
@@ -787,6 +799,8 @@ public class RepositoryRestController
URI selfUri = buildUri(baseUri, repository, id);
res.addLink(new SimpleLink(SELF, selfUri));
publishEvent(new BeforeRenderResourceEvent(request, repoMeta, res));
return negotiateResponse(request, HttpStatus.OK, headers, res);
}
@@ -871,6 +885,8 @@ public class RepositoryRestController
baseUri);
res.addLink(new SimpleLink(SELF, selfUri));
publishEvent(new BeforeRenderResourceEvent(request, repoMeta, body));
body = res;
}
@@ -1025,6 +1041,8 @@ public class RepositoryRestController
res.addLink(new SimpleLink(rel, path));
}
publishEvent(new BeforeRenderResourceEvent(request, propRepoMeta, res));
return negotiateResponse(request, HttpStatus.OK, new HttpHeaders(), res);
}
@@ -1274,6 +1292,8 @@ public class RepositoryRestController
URI selfUri = buildUri(baseUri, linkedRepoMeta.name(), linkedId);
res.addLink(new SimpleLink(SELF, selfUri));
publishEvent(new BeforeRenderResourcesEvent(request, repoMeta, res));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Location", selfUri.toString());
@@ -1629,37 +1649,6 @@ public class RepositoryRestController
return resource;
}
/*
@SuppressWarnings({"unchecked"})
private Map<String, Object> extractPropertiesLinkAware(String repoRel,
Object entity,
EntityMetadata<AttributeMetadata> entityMetadata,
URI baseUri) {
final Map<String, Object> entityDto = new HashMap<String, Object>();
for(Map.Entry<String, AttributeMetadata> attrMeta : entityMetadata.embeddedAttributes().entrySet()) {
String name = attrMeta.getKey();
Object val = attrMeta.getValue().get(entity);
if(null != val) {
entityDto.put(name, val);
}
}
for(String attrName : entityMetadata.linkedAttributes().keySet()) {
URI uri = buildUri(baseUri, attrName);
Link l = new SimpleLink(repoRel + "." + entity.getClass().getSimpleName() + "." + attrName, uri);
List<Link> links = (List<Link>)entityDto.get(LINKS);
if(null == links) {
links = new ArrayList<Link>();
entityDto.put(LINKS, links);
}
links.add(l);
}
return entityDto;
}
*/
private boolean shouldReturnLinks(String acceptHeader) {
if(null != acceptHeader) {
List<MediaType> accept = MediaType.parseMediaTypes(acceptHeader);

View File

@@ -44,4 +44,8 @@ class EventsSpec extends BaseSpec {
}
def "captures resource rendering events"() {
}
}