Skip to content

Commit

Permalink
spring-projects#232: modified the AnnotationRelProvider so it can loo…
Browse files Browse the repository at this point in the history
…k on the controller for the @relation annotation in addition to the entity
  • Loading branch information
Jeff Stano committed Aug 26, 2014
1 parent e84e9a3 commit 0d8484c
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@
*/
package org.springframework.hateoas.core;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.hateoas.RelProvider;

/**
* @author Oliver Gierke
* @author Alexander Baetz
*/
@Order(100)
public class AnnotationRelProvider implements RelProvider {
public class AnnotationRelProvider implements RelProvider, ApplicationContextAware {

This comment has been minimized.

Copy link
@jstano

jstano Aug 26, 2014

Owner

Need to have the applicationContext injected

private ApplicationContext context;

private final Map<Class<?>, Relation> annotationCache = new HashMap<Class<?>, Relation>();

Expand Down Expand Up @@ -80,12 +87,46 @@ private Relation lookupAnnotation(Class<?> type) {
return relation;
}

relation = AnnotationUtils.getAnnotation(type, Relation.class);

This comment has been minimized.

Copy link
@jstano

jstano Aug 26, 2014

Owner

Attempt to locate a controller that has an ExposesResourceFor annotation. If one is found, then attempt to get the @relation annotation from it. If we didn't find a controller or it didn't have an @relation annotation, then attempt to get it from the entity.

Class controllerClass = getControllerTypeWithAnnotation(ExposesResourceFor.class, type);

if (controllerClass != null) {

relation = AnnotationUtils.getAnnotation(controllerClass, Relation.class);
}

if (relation == null) {

relation = AnnotationUtils.getAnnotation(type, Relation.class);
}

if (relation != null) {
annotationCache.put(type, relation);
}

return relation;
}

This comment has been minimized.

Copy link
@jstano

jstano Aug 26, 2014

Owner

Method to locate a bean that has a specific Annotation and value on it.

private Class<?> getControllerTypeWithAnnotation(Class<? extends Annotation> annotationType, Class annotationValue) {

if (annotationValue == Object.class) {
return null;
}

for (String beanName : context.getBeanDefinitionNames()) {

Annotation annotation = context.findAnnotationOnBean(beanName, annotationType);
if (annotation != null && AnnotationUtils.getValue(annotation) == annotationValue) {

return context.getType(beanName);
}
}

return getControllerTypeWithAnnotation(annotationType, annotationValue.getSuperclass());
}

This comment has been minimized.

Copy link
@jstano

jstano Aug 26, 2014

Owner

Required by ApplicationContextAware

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.context = applicationContext;
}
}

0 comments on commit 0d8484c

Please sign in to comment.