Java tutorial
/** * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.harvest.resolver; import java.lang.annotation.Annotation; import org.springframework.core.MethodParameter; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; import com.nec.harvest.stereotype.UserPrincipal; import com.nec.harvest.userdetails.AuthenticatedUserDetails; /** * Allows resolving the {@link com.nec.harvest.model.User} using the * {@link com.nec.harvest.stereotype.UserPrincipal} annotation. * For example, the following {@link org.springframework.stereotype.Controller}: * * <pre> * @Controller * public class MyController { * @RequestMapping("/user/authen/show") * public String show(@UserPrincipal User user) { * return "view"; * } * </pre> * * <p> * Will resolve the User argument using {@link com.nec.harvest.model.User} * from the {@link javax.servlet.http.HttpSession} if the person is an * attribute, else will use the * {@link org.springframework.security.core.context.SecurityContextHolder} to * get the information to retrieve the user from the * {@link com.nec.harvest.service.UserService}. If the * {@link org.springframework.security.core.Authentication} or * {@link org.springframework.security.core.Authentication#getPrincipal()} is * null, it will return null. * </p> * * @author <a href="mailto:sondn@nec.vn">Ngoc Son Dang</a> * @version AuthenticatedArgumentResolver.java * @since 2014/05/21 * */ public final class AuthenticatedArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return findMethodAnnotation(UserPrincipal.class, parameter) != null; } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { return AuthenticatedUserDetails.getUserPrincipal(); } /** * Obtains the specified {@link java.lang.annotation.Annotation} on the specified {@link org.springframework.core.MethodParameter}. * * @param annotationClass the class of the {@link java.lang.annotation.Annotation} to find on the {@link org.springframework.core.MethodParameter} * @param parameter the {@link org.springframework.core.MethodParameter} to search for an {@link java.lang.annotation.Annotation} * @return the {@link java.lang.annotation.Annotation} that was found or null. */ private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) { T annotation = parameter.getParameterAnnotation(annotationClass); if (annotation != null) { return annotation; } Annotation[] annotationsToSearch = parameter.getParameterAnnotations(); for (Annotation toSearch : annotationsToSearch) { annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass); if (annotation != null) { return annotation; } } return null; } }