/** * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @version $Revision: 1 $ */ @DecorateTypes({"text/*+xml", "application/*+xml"}) public class XmlHeaderProcessor implements DecoratorProcessor<Marshaller, XmlHeader> { public Marshaller decorate(Marshaller target, XmlHeader annotation, Class type, Annotation[] annotations, MediaType mediaType) { String h = StringContextReplacement.replace(annotation.value()); try { target.setProperty("com.sun.xml.bind.xmlHeaders", h); } catch (PropertyException e) { throw new RuntimeException(e); } return target; } }
private String getSeparator(Annotation[] annotations) { for (Annotation a : annotations) { if (a instanceof Separator) { if ("".equals(((Separator) a).value())) { break; } return ((Separator) a).value(); } } for (Annotation a : annotations) { if (a instanceof CookieParam) { return "-"; } } return ","; }
public <T> boolean hasDecorator(Class<T> targetClass, Annotation[] annotations) { if (targetClass == null || annotations == null) return false; for (Annotation annotation : annotations) { Decorators decorators = annotation.annotationType().getAnnotation(Decorators.class); if (decorators != null) { for (Decorator decorator : decorators.values()) { if (decorator != null && decorator.target().isAssignableFrom(targetClass)) return true; } } else { Decorator decorator = annotation.annotationType().getAnnotation(Decorator.class); if (decorator != null && decorator.target().isAssignableFrom(targetClass)) return true; } } return false; }
} else if ((form = FindAnnotation.findAnnotation(annotations, Form.class)) != null) { if (type == List.class) { addParameter(type, annotations, ResteasyWadlMethodParamMetaData.MethodParamType.FORM, form.prefix()); this.wantsForm = true; } else if (type == Map.class) { addParameter(type, annotations, ResteasyWadlMethodParamMetaData.MethodParamType.FORM, form.prefix()); this.wantsForm = true; } else
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); } }
private Link getLink(final LinkHeaderParam link, ClientContext context) { return context.getClientResponse().getLink(link.rel()); }
protected String getEncoding(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(ContentEncoding.class)) { return annotation.annotationType().getAnnotation(ContentEncoding.class).value(); } } return null; }
private String[] getStreamType(ResourceMethodInvoker method) { Stream stream = method.getMethod().getAnnotation(Stream.class); Stream.MODE mode = stream != null ? stream.value() : null; if (mode == null) { return new String[]{"text", "event-stream"}; } else if (Stream.MODE.GENERAL.equals(mode)) { return new String[] {"application", "x-stream-general"}; } else if (Stream.MODE.RAW.equals(mode)) { return new String[] {"application", "x-stream-raw"}; } throw new RuntimeException(Messages.MESSAGES.expectedStreamModeGeneralOrRaw(mode)); }
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); } } }
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 <T> void registerDecorators(Class<T> targetClass, HashMap<Class<?>, Annotation> meta, Annotation[] annotations) { for (Annotation annotation : annotations) { Decorators decorators = annotation.annotationType().getAnnotation(Decorators.class); 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); } } }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { Method method = ((PostMatchContainerRequestContext) requestContext).getResourceMethod().getMethod(); Stream stream = method.getAnnotation(Stream.class); Stream.MODE mode = stream != null ? stream.value() : null; Class<?> clazz = method.getReturnType(); if ((mode == Stream.MODE.GENERAL && ResteasyProviderFactory.getInstance().getAsyncStreamProvider(clazz) != null) || requestContext instanceof PostMatchContainerRequestContext && ((PostMatchContainerRequestContext) requestContext).getResourceMethod().isSse()) { SseEventOutputImpl sink = new SseEventOutputImpl(new SseEventProvider()); ResteasyContext.getContextDataMap().put(SseEventSink.class, sink); ResteasyContext.getContextData(PostResourceMethodInvokers.class).addInvokers(new PostResourceMethodInvoker() { @Override public void invoke() { sink.flushResponseToClient(); } }); } }
if (stream.includeStreaming())
addParameter(type, annotations, MethodParamType.FORM, form.prefix()); this.wantsForm = true; } else
/** * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @version $Revision: 1 $ */ @DecorateTypes({"text/*+xml", "application/*+xml"}) public class PrettyProcessor implements DecoratorProcessor<Marshaller, Formatted> { public Marshaller decorate(Marshaller target, Formatted annotation, Class type, Annotation[] annotations, MediaType mediaType) { try { target.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return target; } catch (Exception ex) { throw new RuntimeException(ex); } } }
for (Decorator decorator : decorators.values()) { injectDecorator(typeMap, decoratorAnnotation, decorator); Decorators decorators = decoratorAnnotation.getAnnotation(Decorators.class); if (decorators != null) { for (Decorator decorator : decorators.values()) { if (decorator.target().isAssignableFrom(target.getClass())) { target = doDecoration(target, type, annotations, mediaType, annotation, decorator);
protected Set<String> getEncodings(Annotation[] annotations) { Set<String> encodings = new HashSet<String>(); for (Annotation annotation : annotations) { if (annotation.annotationType().isAnnotationPresent(ContentEncoding.class)) { encodings.add(annotation.annotationType().getAnnotation(ContentEncoding.class).value().toLowerCase()); } } return encodings; } }
public static AsyncResponseConsumer makeAsyncResponseConsumer(ResourceMethodInvoker method, AsyncStreamProvider<?> asyncStreamProvider) { if(method.isSse()) { return new AsyncGeneralStreamingSseResponseConsumer(method, asyncStreamProvider); } Stream stream = method.getMethod().getAnnotation(Stream.class); if (stream != null) { if (Stream.MODE.RAW.equals(stream.value())) { return new AsyncRawStreamingResponseConsumer(method, asyncStreamProvider); } else { return new AsyncGeneralStreamingSseResponseConsumer(method, asyncStreamProvider); } } return new AsyncStreamCollectorResponseConsumer(method, asyncStreamProvider); }
/** * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @version $Revision: 1 $ */ @DecorateTypes({"text/*+xml", "application/*+xml"}) public class StylesheetProcessor implements DecoratorProcessor<Marshaller, Stylesheet> { public Marshaller decorate(Marshaller target, Stylesheet annotation, Class type, Annotation[] annotations, MediaType mediaType) { String doctype = StringContextReplacement.replace(annotation.type()); String href = StringContextReplacement.replace(annotation.href()); String h = "<?xml-stylesheet type='" + doctype + "' href='" + href + "' ?>"; try { target.setProperty("com.sun.xml.bind.xmlHeaders", h); } catch (PropertyException e) { throw new RuntimeException(e); } return target; } }