Java源码示例:org.everrest.core.Filter
示例1
@SuppressWarnings({"unchecked"})
protected void processBindings(Injector injector, EverrestApplication everrest) {
for (Binding<?> binding : injector.getBindings().values()) {
Key<?> bindingKey = binding.getKey();
Type type = bindingKey.getTypeLiteral().getType();
if (type instanceof Class) {
Class clazz = (Class)type;
if (clazz.getAnnotation(Provider.class) != null) {
ProviderDescriptor providerDescriptor = new ProviderDescriptorImpl(clazz);
everrest.addFactory(new GuiceObjectFactory<>(providerDescriptor, binding.getProvider()));
} else if (clazz.getAnnotation(Filter.class) != null) {
FilterDescriptor filterDescriptor = new FilterDescriptorImpl(clazz);
everrest.addFactory(new GuiceObjectFactory<>(filterDescriptor, binding.getProvider()));
} else if (clazz.getAnnotation(Path.class) != null) {
ResourceDescriptor resourceDescriptor;
if (bindingKey.getAnnotation() != null && bindingKey.getAnnotationType().isAssignableFrom(BindingPath.class)) {
String path = ((BindingPath)bindingKey.getAnnotation()).value();
resourceDescriptor = new AbstractResourceDescriptor(path, clazz);
} else {
resourceDescriptor = new AbstractResourceDescriptor(clazz);
}
everrest.addFactory(new GuiceObjectFactory<>(resourceDescriptor, binding.getProvider()));
}
}
}
}
示例2
@SuppressWarnings({"unchecked"})
public void addSingleton(Object instance) {
Class clazz = instance.getClass();
if (clazz.getAnnotation(Provider.class) != null) {
if (instance instanceof ContextResolver) {
providers.addContextResolver((ContextResolver)instance);
}
if (instance instanceof ExceptionMapper) {
providers.addExceptionMapper((ExceptionMapper)instance);
}
if (instance instanceof MessageBodyReader) {
providers.addMessageBodyReader((MessageBodyReader)instance);
}
if (instance instanceof MessageBodyWriter) {
providers.addMessageBodyWriter((MessageBodyWriter)instance);
}
} else if (clazz.getAnnotation(Filter.class) != null) {
if (instance instanceof MethodInvokerFilter) {
providers.addMethodInvokerFilter((MethodInvokerFilter)instance);
}
if (instance instanceof RequestFilter) {
providers.addRequestFilter((RequestFilter)instance);
}
if (instance instanceof ResponseFilter) {
providers.addResponseFilter((ResponseFilter)instance);
}
} else if (clazz.getAnnotation(Path.class) != null) {
resources.addResource(instance, null);
}
}
示例3
@SuppressWarnings({"unchecked"})
public void addPerRequest(Class clazz) {
if (clazz.getAnnotation(Provider.class) != null) {
if (ContextResolver.class.isAssignableFrom(clazz)) {
providers.addContextResolver(clazz);
}
if (ExceptionMapper.class.isAssignableFrom(clazz)) {
providers.addExceptionMapper(clazz);
}
if (MessageBodyReader.class.isAssignableFrom(clazz)) {
providers.addMessageBodyReader(clazz);
}
if (MessageBodyWriter.class.isAssignableFrom(clazz)) {
providers.addMessageBodyWriter(clazz);
}
} else if (clazz.getAnnotation(Filter.class) != null) {
if (MethodInvokerFilter.class.isAssignableFrom(clazz)) {
providers.addMethodInvokerFilter(clazz);
}
if (RequestFilter.class.isAssignableFrom(clazz)) {
providers.addRequestFilter(clazz);
}
if (ResponseFilter.class.isAssignableFrom(clazz)) {
providers.addResponseFilter(clazz);
}
} else if (clazz.getAnnotation(Path.class) != null) {
resources.addResource(clazz, null);
}
}
示例4
@SuppressWarnings({"unchecked"})
public void addFactory(ObjectFactory factory) {
Class clazz = factory.getObjectModel().getObjectClass();
if (clazz.getAnnotation(Provider.class) != null) {
if (ContextResolver.class.isAssignableFrom(clazz)) {
providers.addContextResolver(factory);
}
if (ExceptionMapper.class.isAssignableFrom(clazz)) {
providers.addExceptionMapper(factory);
}
if (MessageBodyReader.class.isAssignableFrom(clazz)) {
providers.addMessageBodyReader(factory);
}
if (MessageBodyWriter.class.isAssignableFrom(clazz)) {
providers.addMessageBodyWriter(factory);
}
} else if (clazz.getAnnotation(Filter.class) != null) {
if (MethodInvokerFilter.class.isAssignableFrom(clazz)) {
providers.addMethodInvokerFilter(factory);
}
if (RequestFilter.class.isAssignableFrom(clazz)) {
providers.addRequestFilter(factory);
}
if (ResponseFilter.class.isAssignableFrom(clazz)) {
providers.addResponseFilter(factory);
}
} else if (clazz.getAnnotation(Path.class) != null) {
resources.addResource(factory);
}
}
示例5
private void initRestResource(ITestNGMethod... testMethods) {
for (ITestNGMethod testMethod : testMethods) {
Object instance = testMethod.getInstance();
if (hasEverrestJettyListenerTestHierarchy(instance.getClass())) {
EverrestApplication everrest = new EverrestApplication();
Field[] fields = instance.getClass().getDeclaredFields();
for (Field field : fields) {
try {
if (isRestResource(field.getType())) {
ObjectFactory<? extends ObjectModel> factory = createFactory(instance, field);
if (factory != null) {
everrest.addFactory(factory);
}
} else if (javax.servlet.Filter.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
Object fieldInstance = field.get(instance);
if (fieldInstance != null) {
httpServer.addFilter(((javax.servlet.Filter)fieldInstance), "/*");
} else {
httpServer.addFilter((Class<? extends javax.servlet.Filter>)field.getType(), "/*");
}
}
} catch (IllegalAccessException e) {
LOG.error(e.getLocalizedMessage(), e);
}
}
if (everrest.getFactories().size() > 0) {
httpServer.publish(everrest);
}
}
}
}
示例6
private boolean isRestResource(Class<?> resourceClass) {
return resourceClass.isAnnotationPresent(Path.class) ||
resourceClass.isAnnotationPresent(Provider.class) ||
resourceClass.isAnnotationPresent(Filter.class) ||
resourceClass.isAssignableFrom(ExceptionMapper.class) ||
resourceClass.isAssignableFrom(ContextResolver.class) ||
resourceClass.isAssignableFrom(MessageBodyReader.class) ||
resourceClass.isAssignableFrom(MessageBodyWriter.class) ||
resourceClass.isAssignableFrom(MethodInvokerFilter.class) ||
resourceClass.isAssignableFrom(RequestFilter.class) ||
resourceClass.isAssignableFrom(ResponseFilter.class);
}
示例7
public ObjectFactory<? extends ObjectModel> createFactory(Object testObject, Field field) {
Class clazz = (Class)field.getType();
if (clazz.getAnnotation(Provider.class) != null) {
ProviderDescriptor providerDescriptor = new ProviderDescriptorImpl(clazz);
return new TestResourceFactory<>(providerDescriptor, testObject, field);
} else if (clazz.getAnnotation(Filter.class) != null) {
FilterDescriptor filterDescriptor = new FilterDescriptorImpl(clazz);
return new TestResourceFactory<>(filterDescriptor, testObject, field);
} else if (clazz.getAnnotation(Path.class) != null) {
AbstractResourceDescriptor resourceDescriptor = new AbstractResourceDescriptor(clazz);
return new TestResourceFactory<>(resourceDescriptor, testObject, field);
}
return null;
}