public static <T extends Annotation> T getAnnotation(Class<?> objectClass, Class<T> annotationClass) { if (objectClass == null || Object.class == objectClass) { return null; } T annotation = objectClass.getAnnotation(annotationClass); if (annotation != null) { return annotation; } return getAnnotation(objectClass.getSuperclass(), annotationClass); }
/** * Extract the annotation from the method or the declaring class. * * @param method * @param annotationClass * @param <T> * @return the annotation or null */ public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) { T t = method.getAnnotation(annotationClass); if (t == null) { t = getAnnotation(method.getDeclaringClass(), annotationClass); } return t; }
public static List<String> getProduces(Method method) { Set<String> contentTypes = new LinkedHashSet<>(); Produces produces = ClassUtil.getAnnotation(method, Produces.class); if (produces != null) { for (String value : produces.value()) { contentTypes.add(value.trim()); } } return new ArrayList<>(contentTypes); }
protected void checkRequireAuthenticated(Account account, Method method) { RequireAuthenticated annotation = ClassUtil.getAnnotation(method, RequireAuthenticated.class); if (annotation != null) { account.checkAuthenticated(); } }
protected void checkRequireAdministrator(Account account, Method method) { RequireAdministrator annotation = ClassUtil.getAnnotation(method, RequireAdministrator.class); if (annotation != null) { account.checkAdministrator(); } }
public static List<String> getProduces(Method method) { Set<String> contentTypes = new LinkedHashSet<>(); Produces produces = ClassUtil.getAnnotation(method, Produces.class); if (produces != null) { for (String value : produces.value()) { contentTypes.add(value.trim()); } } return new ArrayList<>(contentTypes); }
protected void checkRequireAuthenticated(Account account, Method method) { RequireAuthenticated annotation = ClassUtil.getAnnotation(method, RequireAuthenticated.class); if (annotation != null) { account.checkAuthenticated(); } }
public static List<String> getConsumes(Method method) { Set<String> types = new LinkedHashSet<>(); Consumes consumes = ClassUtil.getAnnotation(method, Consumes.class); if (consumes != null) { for (String value : consumes.value()) { types.add(value.trim()); } } return new ArrayList<>(types); }
protected void checkRequireAdministrator(Account account, Method method) { RequireAdministrator annotation = ClassUtil.getAnnotation(method, RequireAdministrator.class); if (annotation != null) { account.checkAdministrator(); } }
protected void checkRequireGuest(Account account, Method method) { RequireGuest annotation = ClassUtil.getAnnotation(method, RequireGuest.class); if (annotation != null) { account.checkGuest(); } }
public static List<String> getConsumes(Method method) { Set<String> types = new LinkedHashSet<>(); Consumes consumes = ClassUtil.getAnnotation(method, Consumes.class); if (consumes != null) { for (String value : consumes.value()) { types.add(value.trim()); } } return new ArrayList<>(types); }
protected void checkRequireGuest(Account account, Method method) { RequireGuest annotation = ClassUtil.getAnnotation(method, RequireGuest.class); if (annotation != null) { account.checkGuest(); } }
public void addMethodGroup(Class<?> methodGroupClass) { String methodGroup = methodGroupClass.getName(); if (methodGroupClass.isAnnotationPresent(XmlRpc.class)) { XmlRpc xmlrpc = ClassUtil.getAnnotation(methodGroupClass, XmlRpc.class); methodGroup = Optional.fromNullable(Strings.emptyToNull(xmlrpc.value())).or(methodGroup); } addMethodGroup(methodGroup, methodGroupClass); }
public void addMethodGroup(Class<?> methodGroupClass) { String methodGroup = methodGroupClass.getName(); if (methodGroupClass.isAnnotationPresent(XmlRpc.class)) { XmlRpc xmlrpc = ClassUtil.getAnnotation(methodGroupClass, XmlRpc.class); methodGroup = Optional.fromNullable(Strings.emptyToNull(xmlrpc.value())).or(methodGroup); } addMethodGroup(methodGroup, methodGroupClass); }
/** * Configures the content-type suffixes * * @param engines * @return acceptable content-type suffixes */ protected Set<String> configureContentTypeSuffixes(ContentTypeEngines engines) { if (null == ClassUtil.getAnnotation(method, ContentTypeBySuffix.class)) { return Collections.emptySet(); } Set<String> suffixes = new TreeSet<>(); for (String suffix : engines.getContentTypeSuffixes()) { String contentType = engines.getContentTypeEngine(suffix).getContentType(); if (declaredProduces.contains(contentType)) { suffixes.add(suffix); } } return suffixes; }
/** * Configures the content-type suffixes * * @param engines * @return acceptable content-type suffixes */ protected Set<String> configureContentTypeSuffixes(ContentTypeEngines engines) { if (null == ClassUtil.getAnnotation(method, ContentTypeBySuffix.class)) { return Collections.emptySet(); } Set<String> suffixes = new TreeSet<>(); for (String suffix : engines.getContentTypeSuffixes()) { String contentType = engines.getContentTypeEngine(suffix).getContentType(); if (declaredProduces.contains(contentType)) { suffixes.add(suffix); } } return suffixes; }
private void configureRegistration(RouteRegistration registration, Method method) { // specify optional route name if (method.isAnnotationPresent(Named.class)) { Named named = method.getAnnotation(Named.class); registration.setName(named.value()); } // specify optional or required content-type suffixes ContentTypeBySuffix bySuffix = ClassUtil.getAnnotation(method, ContentTypeBySuffix.class); if (bySuffix != null) { Collection<String> suffixes = ControllerUtil.getSuffixes(method); if (bySuffix.required()) { registration.requireContentTypeSuffixes(suffixes); } else { registration.contentTypeSuffixes(suffixes); } } }
private void configureRegistration(RouteRegistration registration, Method method) { // specify optional route name if (method.isAnnotationPresent(Named.class)) { Named named = method.getAnnotation(Named.class); registration.setName(named.value()); } // specify optional or required content-type suffixes ContentTypeBySuffix bySuffix = ClassUtil.getAnnotation(method, ContentTypeBySuffix.class); if (bySuffix != null) { Collection<String> suffixes = ControllerUtil.getSuffixes(method); if (bySuffix.required()) { registration.requireContentTypeSuffixes(suffixes); } else { registration.contentTypeSuffixes(suffixes); } } }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { RequireToken requireToken = ClassUtil.getAnnotation(invocation.getMethod(), RequireToken.class); String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } Account account = getAccount(); if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } return invocation.proceed(); }
protected Account checkRequireToken(Method method) { Account account = getAccount(); RequireToken requireToken = ClassUtil.getAnnotation(method, RequireToken.class); if (requireToken != null) { String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } } return account; }