Skip to content

Commit

Permalink
save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
liweinan authored and asoldano committed Jan 20, 2019
1 parent 5e9135b commit 21c207b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
Expand Up @@ -2,6 +2,7 @@

import org.jboss.resteasy.annotations.DecorateTypes;
import org.jboss.resteasy.annotations.Decorator;
import org.jboss.resteasy.annotations.Decorators;
import org.jboss.resteasy.core.MediaTypeMap;
import org.jboss.resteasy.spi.DecoratorProcessor;

Expand Down Expand Up @@ -46,45 +47,64 @@ public <T> T decorate(Class<T> targetClass, T target, Class type, Annotation[] a
if (meta.size() == 0) return target;

MediaTypeMap<Class<?>> typeMap = new MediaTypeMap<Class<?>>();

for (Class<?> decoratorAnnotation : meta.keySet())
{
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
String[] mediaTypes = {"*/*"};
DecorateTypes produces = decorator.processor().getAnnotation(DecorateTypes.class);
if (produces != null)
{
mediaTypes = produces.value();
}
for (String pType : mediaTypes)
{
typeMap.add(MediaType.valueOf(pType), decoratorAnnotation);
Decorators decorators = decoratorAnnotation.getAnnotation(Decorators.class);
if (decorators != null) {
for (Decorator decorator : decorators.values()) {
injectDecorator(typeMap, decoratorAnnotation, decorator);
}
} else {
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
injectDecorator(typeMap, decoratorAnnotation, decorator);
}
}

List<Class<?>> list = typeMap.getPossible(mediaType);
for (Class<?> decoratorAnnotation : list)
{
Annotation annotation = meta.get(decoratorAnnotation);
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
DecoratorProcessor processor = null;
try
{
processor = decorator.processor().newInstance();
}
catch (InstantiationException e)
{
throw new RuntimeException(e.getCause());
Decorators decorators = decoratorAnnotation.getAnnotation(Decorators.class);
if (decorators != null) {
for (Decorator decorator : decorators.values()) {
if (target.getClass().isAssignableFrom(decorator.target())) {
target = doDecoration(target, type, annotations, mediaType, annotation, decorator);
}
}
} else {
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
target = doDecoration(target, type, annotations, mediaType, annotation, decorator);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
target = (T) processor.decorate(target, annotation, type, annotations, mediaType);
}

return target;
}

private <T> T doDecoration(T target, Class type, Annotation[] annotations, MediaType mediaType, Annotation annotation, Decorator decorator) {
DecoratorProcessor processor = null;
try {
processor = decorator.processor().newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e.getCause());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
target = (T) processor.decorate(target, annotation, type, annotations, mediaType);
return target;
}

private void injectDecorator(MediaTypeMap<Class<?>> typeMap, Class<?> decoratorAnnotation, Decorator decorator) {
String[] mediaTypes = {"*/*"};
DecorateTypes produces = decorator.processor().getAnnotation(DecorateTypes.class);
if (produces != null) {
mediaTypes = produces.value();
}
for (String pType : mediaTypes) {
typeMap.add(MediaType.valueOf(pType), decoratorAnnotation);
}
}

public <T> boolean hasDecorator(Class<T> targetClass, Annotation[] annotations) {
if (targetClass == null || annotations == null)
return false;
Expand All @@ -97,14 +117,22 @@ public <T> boolean hasDecorator(Class<T> targetClass, Annotation[] annotations)
}

private <T> void registerDecorators(Class<T> targetClass, HashMap<Class<?>, Annotation> meta, Annotation[] annotations) {
for (Annotation annotation : annotations)
{
for (Annotation annotation : annotations) {
Decorators decorators = annotation.annotationType().getAnnotation(Decorators.class);
Decorator decorator = annotation.annotationType().getAnnotation(Decorator.class);
if (decorator != null && targetClass.isAssignableFrom(decorator.target()))
{
meta.put(annotation.annotationType(), annotation);
if (decorators != null) {
for (Decorator decorator : decorators.values()) {
putMeta(targetClass, meta, annotation, decorator);
}
} else {
Decorator decorator = annotation.annotationType().getAnnotation(Decorator.class);
putMeta(targetClass, meta, annotation, decorator);
}
}
}

private <T> void putMeta(Class<T> targetClass, HashMap<Class<?>, Annotation> meta, Annotation annotation, Decorator decorator) {
if (decorator != null && targetClass.isAssignableFrom(decorator.target())) {
meta.put(annotation.annotationType(), annotation);
}
}
}
Expand Up @@ -2,7 +2,7 @@

import org.jboss.resteasy.core.ResourceMethodRegistry;
import org.jboss.resteasy.core.ResteasyContext;
import org.jboss.resteasy.links.AddJsonLinks;
import org.jboss.resteasy.links.AddLinks;
import org.jboss.resteasy.plugins.providers.jackson.DecoratedEntityContainer;
import org.jboss.resteasy.spi.DecoratorProcessor;
import org.jboss.resteasy.spi.Registry;
Expand All @@ -11,10 +11,10 @@
import javax.ws.rs.core.UriInfo;
import java.lang.annotation.Annotation;

public class JsonLinkDecorator implements DecoratorProcessor<DecoratedEntityContainer, AddJsonLinks> {
public class JsonLinkDecorator implements DecoratorProcessor<DecoratedEntityContainer, AddLinks> {

@Override
public DecoratedEntityContainer decorate(DecoratedEntityContainer target, AddJsonLinks annotation, Class type, Annotation[] annotations, MediaType mediaType) {
public DecoratedEntityContainer decorate(DecoratedEntityContainer target, AddLinks annotation, Class type, Annotation[] annotations, MediaType mediaType) {
UriInfo uriInfo = ResteasyContext.getContextData(UriInfo.class);
ResourceMethodRegistry registry = (ResourceMethodRegistry) ResteasyContext.getContextData(Registry.class);

Expand Down

0 comments on commit 21c207b

Please sign in to comment.