下面列出了javax.ws.rs.container.ResourceInfo#getResourceMethod ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) {
AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
// RolesAllowed on the method takes precedence over PermitAll
RolesAllowed ra = am.getAnnotation(RolesAllowed.class);
if (ra != null) {
configuration.register(AuthCheckFilter.INSTANCE);
return;
}
// PermitAll takes precedence over RolesAllowed on the class
// This avoids putting AuthCheckFilter in the request flow for all path's which
// are defined under PermitAll annotation. That is requests for "/", "/login", "/mainLogin" and "/spnegoLogin"
// path's doesn't go through AuthCheckFilter.
if (am.isAnnotationPresent(PermitAll.class)) {
// Do nothing.
return;
}
// RolesAllowed on the class takes precedence over PermitAll
ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class);
if (ra != null) {
configuration.register(AuthCheckFilter.INSTANCE);
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
// 获取资源方法
Method resourceMethod = resourceInfo.getResourceMethod();
if (resourceMethod != null) {
// 获取FormatJson注解
ResponseFormat formatJson = resourceMethod.getAnnotation(ResponseFormat.class);
if (formatJson == null || formatJson.type().equals(FormatType.JSON)) {
context.register(DefaultWebFilter.class);
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
Class<?> resourceClass = resourceInfo.getResourceClass();
Method resourceMethod = resourceInfo.getResourceMethod();
context.register(MCRCacheFilter.class);
if (isStaticContent(resourceClass, resourceMethod)) {
// class or method is annotated with MCRStaticContent
// -> do only register session lock filter
context.register(MCRSessionLockFilter.class);
return;
}
String packageName = resourceClass.getPackage().getName();
if (getPackages().contains(packageName)) {
registerTransactionFilter(context);
registerSessionHookFilter(context);
registerAccessFilter(context, resourceClass, resourceMethod);
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
// 获取资源方法
Method resourceMethod = resourceInfo.getResourceMethod();
if (resourceMethod != null) {
// 获取FormatJson注解
ResponseFormat formatJson = resourceMethod.getAnnotation(ResponseFormat.class);
if(formatJson == null || formatJson.type().equals(FormatType.JSON)){
context.register(DefaultWebFilter.class);
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
AnnotatedMethod annotatedMethod = new AnnotatedMethod(resourceInfo.getResourceMethod());
Annotation[][] parameterAnnotations = annotatedMethod.getParameterAnnotations();
Class<?>[] parameterTypes = annotatedMethod.getParameterTypes ();
Type[] parameterGenericTypes = annotatedMethod.getGenericParameterTypes();
verifyAuthAnnotations(parameterAnnotations);
for (int i=0;i<parameterAnnotations.length;i++) {
for (Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof Auth) {
Type parameterType = parameterTypes[i];
if (parameterType == Optional.class) {
parameterType = ((ParameterizedType)parameterGenericTypes[i]).getActualTypeArguments()[0];
context.register(new WebApplicationExceptionCatchingFilter(getFilterFor(parameterType)));
} else {
context.register(getFilterFor(parameterType));
}
}
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
final Annotation[][] parameterAnnotations = am.getParameterAnnotations();
if (am.isAnnotationPresent(RolesAllowed.class) || am.isAnnotationPresent(DenyAll.class) ||
am.isAnnotationPresent(PermitAll.class)) {
context.register(authFilter);
} else {
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation instanceof Auth) {
context.register(authFilter);
return;
}
}
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
CacheControl cacheControl = null;
Method resourceMethod = resourceInfo.getResourceMethod();
if (resourceMethod != null) {
cacheControl = resourceMethod.getAnnotation(CacheControl.class);
} else {
Class<?> resourceClass = resourceInfo.getResourceClass();
if (resourceClass != null) {
cacheControl = resourceClass.getAnnotation(CacheControl.class);
}
}
if (cacheControl == null) {
featureContext.register(NO_CACHE_FILTER);
} else {
featureContext.register(new CacheResponseFilter(cacheControl.value()));
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext configurable) {
configurable.register(new PreMatchDynamicContainerRequestFilter());
configurable.register(RESPONSE_FILTER);
Map<Class<?>, Integer> contracts = new HashMap<>();
contracts.put(ContainerResponseFilter.class, 2);
configurable.register(new PostMatchDynamicContainerRequestResponseFilter(),
contracts);
Method m = resourceInfo.getResourceMethod();
if ("echoBookElement".equals(m.getName())) {
Class<?> paramType = m.getParameterTypes()[0];
if (paramType == Book.class) {
configurable.register(new PostMatchDynamicEchoBookFilter(2));
}
}
}
private static void verifyNotPrestoResource(ResourceInfo resourceInfo)
{
Method resourceMethod = resourceInfo.getResourceMethod();
if (resourceMethod != null && resourceMethod.getDeclaringClass().getPackageName().startsWith("io.prestosql.")) {
throw new IllegalArgumentException("Presto resource is not annotated with @" + ResourceSecurity.class.getSimpleName() + ": " + resourceInfo.getResourceMethod());
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (ENFORCE_AUTHENTICATION && ENFORCE_AUTHORIZATION) {
final Method method = resourceInfo.getResourceMethod();
if (method.isAnnotationPresent(PermissionRequired.class)) {
context.register(AuthorizationFilter.class);
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (ENFORCE_AUTHENTICATION) {
final Method method = resourceInfo.getResourceMethod();
if (!method.isAnnotationPresent(AuthenticationNotRequired.class)) {
context.register(AuthenticationFilter.class);
}
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
Method method = resourceInfo.getResourceMethod();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(Session.class) && !method.getName().startsWith("log")) {
context.register(NiPingAuthFilter.class);
}
}
}
}
@Override
public void configure(final ResourceInfo resourceInfo,
final FeatureContext context) {
final AnnotatedMethod method = new AnnotatedMethod(resourceInfo.getResourceMethod());
final RateLimited rateLimited = method.getAnnotation(RateLimited.class);
if (null != rateLimited) {
context.register(RateLimit429EnforcerFilter.class);
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
Class<?> resourceClass = resourceInfo.getResourceClass();
Method resourceMethod = resourceInfo.getResourceMethod();
if (requiresTransaction(resourceClass, resourceMethod)) {
context.register(MCREnableTransactionFilter.class);
}
super.configure(resourceInfo, context);
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext configuration) {
final Method am = resourceInfo.getResourceMethod();
try {
// DenyAll on the method take precedence over RolesAllowed and PermitAll
if (am.isAnnotationPresent(DenyAll.class)) {
configuration.register(new RolesAllowedRequestFilter());
return;
}
// RolesAllowed on the method takes precedence over PermitAll
Optional<Annotation> ra = Arrays.stream(am.getAnnotations())
.filter(a -> a.annotationType().getName().equals(RolesAllowed.class.getName())).findFirst();
if (ra.isPresent()) {
configuration.register(new RolesAllowedRequestFilter(((RolesAllowed) ra.get()).value()));
return;
}
// PermitAll takes precedence over RolesAllowed on the class
if (am.isAnnotationPresent(PermitAll.class)) {
// Do nothing.
return;
}
// DenyAll can't be attached to classes
// RolesAllowed on the class takes precedence over PermitAll
ra = Arrays.stream(resourceInfo.getResourceClass().getAnnotations())
.filter(a -> a.annotationType().getName().equals(RolesAllowed.class.getName())).findFirst();
if (ra.isPresent()) {
configuration.register(new RolesAllowedRequestFilter(((RolesAllowed) ra.get()).value()));
}
} catch (Exception e) {
logger.error("Error while configuring the roles", e);
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
// DenyAll on the method take precedence over RolesAllowed and PermitAll
if (am.isAnnotationPresent(DenyAll.class)) {
context.register(new RolesAllowedRequestFilter());
return;
}
// RolesAllowed on the method takes precedence over PermitAll
RolesAllowed ra = am.getAnnotation(RolesAllowed.class);
if (ra != null) {
context.register(new RolesAllowedRequestFilter(ra.value()));
return;
}
// PermitAll takes precedence over RolesAllowed on the class
if (am.isAnnotationPresent(PermitAll.class)) {
// Do nothing.
return;
}
// DenyAll can't be attached to classes
// RolesAllowed on the class takes precedence over PermitAll
ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class);
if (ra != null) {
context.register(new RolesAllowedRequestFilter(ra.value()));
}
}
private void addMethodScopes(ResourceInfo resourceInfo, List<String> scopes) {
Method resourceMethod = resourceInfo.getResourceMethod();
ProtectedApi methodAnnotation = resourceMethod.getAnnotation(ProtectedApi.class);
if (methodAnnotation != null) {
scopes.addAll(Stream.of(methodAnnotation.scopes()).collect(Collectors.toList()));
}
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
Method am = resourceInfo.getResourceMethod();
if (logger.isTraceEnabled()) {
logger.trace("configure {} method {}",
resourceInfo.getResourceClass().getSimpleName(), resourceInfo.getResourceMethod().getName());
}
boolean sysadminLocalhostOnly =
Boolean.parseBoolean(properties.getProperty("usergrid.sysadmin.localhost.only", "false"));
if (sysadminLocalhostOnly) {
// priority = PRIORITY_SUPERUSER forces this to run first
featureContext.register( SysadminLocalhostFilter.class, PRIORITY_SUPERUSER );
}
if ( am.isAnnotationPresent( RequireApplicationAccess.class ) ) {
featureContext.register( ApplicationFilter.class, PRIORITY_DEFAULT);
}
else if ( am.isAnnotationPresent( RequireOrganizationAccess.class ) ) {
featureContext.register( OrganizationFilter.class, PRIORITY_DEFAULT);
}
else if ( am.isAnnotationPresent( RequireSystemAccess.class ) ) {
featureContext.register( SystemFilter.class, PRIORITY_DEFAULT);
}
else if ( am.isAnnotationPresent( RequireAdminUserAccess.class ) ) {
featureContext.register( SystemFilter.AdminUserFilter.class, PRIORITY_DEFAULT);
}
else if ( am.isAnnotationPresent( CheckPermissionsForPath.class ) ) {
featureContext.register( PathPermissionsFilter.class, PRIORITY_DEFAULT);
}
}
/** {@inheritDoc} */
@Override
public void writeTo(final Object entity,
final Class<?> type,
final Type genericType,
final Annotation[] annotations,
MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream entityStream) throws IOException, WebApplicationException {
if (mediaType == null
|| MediaTypes.isWildcard(mediaType)
|| (mediaType.getType().equals(LOW_IE_DEFAULT_REQ_TYPE.getType()) &&
mediaType.getSubtype().equals(LOW_IE_DEFAULT_REQ_TYPE.getSubtype()))) {
mediaType = MediaType.TEXT_HTML_TYPE;
}
List<String> templates = Lists.newArrayList();
ResourceInfo resourceInfo = resourceInfoProvider.get();
// 1. resource method name
// 2. _protected/ + req path LOWER_UNDERSCORE
// 3. _protected/ + req raw path
// 4. req path LOWER_UNDERSCORE
// 5. req raw path
// 6. index
// 7. default view
if (resourceInfo != null && resourceInfo.getResourceMethod() != null) {
templates.add(resourceInfo.getResourceMethod().getName());
}
// xxx/{a_b}.httl == xxx/{aB}.httl
String path = getTemplatePath(uriInfoProvider.get());
String _path = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, path);
if (!_path.equals(path)) {
templates.add(Viewables.PROTECTED_DIR_PATH + _path);
}
templates.add(Viewables.PROTECTED_DIR_PATH + path);
if (!_path.equals(path)) {
templates.add(_path);
}
templates.add(path);
templates.add("index");
if (!defaultDataViewDisabled) {
if (entity == null
|| (entity instanceof Collection
&& ((Collection) entity).size() == 0)
|| (entity.getClass().isArray()
&& Array.getLength(entity) == 0)) {
templates.add(dataViewNull);
} else if (isItem(entity)) {
templates.add(dataViewItem);
} else {
templates.add(dataViewList);
}
}
Class clazz = null;
if (resourceInfo != null) {
clazz = resourceInfo.getResourceClass();
}
if (clazz == null) {
List<Object> res = uriInfoProvider.get().getMatchedResources();
if (res != null && res.size() > 0) {
clazz = res.get(0).getClass();
}
}
if (clazz == null) {
clazz = Ameba.class;
}
workersProvider.get().getMessageBodyWriter(
ImplicitViewable.class,
ImplicitViewable.class,
annotations,
mediaType)
.writeTo(new ImplicitViewable(templates, entity, clazz),
ImplicitViewable.class,
ImplicitViewable.class,
annotations, mediaType,
httpHeaders, entityStream);
}