Java Code Examples for org.springframework.web.bind.annotation.SessionAttributes

The following examples show how to use org.springframework.web.bind.annotation.SessionAttributes. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: SessionAttributesHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new session attributes handler. Session attribute names and types
 * are extracted from the {@code @SessionAttributes} annotation, if present,
 * on the given type.
 * @param handlerType the controller type
 */
public SessionAttributesHandler(Class<?> handlerType) {
	SessionAttributes ann = AnnotatedElementUtils.findMergedAnnotation(handlerType, SessionAttributes.class);
	if (ann != null) {
		Collections.addAll(this.attributeNames, ann.names());
		Collections.addAll(this.attributeTypes, ann.types());
	}
	this.knownAttributeNames.addAll(this.attributeNames);
}
 
Example 2
Source Project: spring-analysis-note   Source File: SessionAttributesHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new session attributes handler. Session attribute names and types
 * are extracted from the {@code @SessionAttributes} annotation, if present,
 * on the given type.
 * @param handlerType the controller type
 * @param sessionAttributeStore used for session access
 */
public SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore) {
	Assert.notNull(sessionAttributeStore, "SessionAttributeStore may not be null");
	this.sessionAttributeStore = sessionAttributeStore;

	SessionAttributes ann = AnnotatedElementUtils.findMergedAnnotation(handlerType, SessionAttributes.class);
	if (ann != null) {
		Collections.addAll(this.attributeNames, ann.names());
		Collections.addAll(this.attributeTypes, ann.types());
	}
	this.knownAttributeNames.addAll(this.attributeNames);
}
 
Example 3
Source Project: java-technology-stack   Source File: SessionAttributesHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new session attributes handler. Session attribute names and types
 * are extracted from the {@code @SessionAttributes} annotation, if present,
 * on the given type.
 * @param handlerType the controller type
 */
public SessionAttributesHandler(Class<?> handlerType) {
	SessionAttributes ann = AnnotatedElementUtils.findMergedAnnotation(handlerType, SessionAttributes.class);
	if (ann != null) {
		Collections.addAll(this.attributeNames, ann.names());
		Collections.addAll(this.attributeTypes, ann.types());
	}
	this.knownAttributeNames.addAll(this.attributeNames);
}
 
Example 4
Source Project: java-technology-stack   Source File: SessionAttributesHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new session attributes handler. Session attribute names and types
 * are extracted from the {@code @SessionAttributes} annotation, if present,
 * on the given type.
 * @param handlerType the controller type
 * @param sessionAttributeStore used for session access
 */
public SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore) {
	Assert.notNull(sessionAttributeStore, "SessionAttributeStore may not be null");
	this.sessionAttributeStore = sessionAttributeStore;

	SessionAttributes ann = AnnotatedElementUtils.findMergedAnnotation(handlerType, SessionAttributes.class);
	if (ann != null) {
		Collections.addAll(this.attributeNames, ann.names());
		Collections.addAll(this.attributeTypes, ann.types());
	}
	this.knownAttributeNames.addAll(this.attributeNames);
}
 
Example 5
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {

	Class<?> clazz = ClassUtils.getUserClass(handler);
	Boolean annotatedWithSessionAttributes = this.sessionAnnotatedClassesCache.get(clazz);
	if (annotatedWithSessionAttributes == null) {
		annotatedWithSessionAttributes = (AnnotationUtils.findAnnotation(clazz, SessionAttributes.class) != null);
		this.sessionAnnotatedClassesCache.put(clazz, annotatedWithSessionAttributes);
	}

	if (annotatedWithSessionAttributes) {
		checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true);
	}
	else {
		checkAndPrepare(request, response, true);
	}

	// Execute invokeHandlerMethod in synchronized block if required.
	if (this.synchronizeOnSession) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object mutex = WebUtils.getSessionMutex(session);
			synchronized (mutex) {
				return invokeHandlerMethod(request, response, handler);
			}
		}
	}

	return invokeHandlerMethod(request, response, handler);
}
 
Example 6
/**
 * Create a new instance for a controller type. Session attribute names and
 * types are extracted from the {@code @SessionAttributes} annotation, if
 * present, on the given type.
 * @param handlerType the controller type
 * @param sessionAttributeStore used for session access
 */
public SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore) {
	Assert.notNull(sessionAttributeStore, "SessionAttributeStore may not be null");
	this.sessionAttributeStore = sessionAttributeStore;

	SessionAttributes annotation =
			AnnotatedElementUtils.findMergedAnnotation(handlerType, SessionAttributes.class);
	if (annotation != null) {
		this.attributeNames.addAll(Arrays.asList(annotation.names()));
		this.attributeTypes.addAll(Arrays.asList(annotation.types()));
	}
	this.knownAttributeNames.addAll(this.attributeNames);
}
 
Example 7
Source Project: lams   Source File: HandlerMethodResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize a new HandlerMethodResolver for the specified handler type.
 * @param handlerType the handler class to introspect
 */
public void init(final Class<?> handlerType) {
	Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
	Class<?> specificHandlerType = null;
	if (!Proxy.isProxyClass(handlerType)) {
		handlerTypes.add(handlerType);
		specificHandlerType = handlerType;
	}
	handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
	for (Class<?> currentHandlerType : handlerTypes) {
		final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType);
		ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) {
				Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
				if (isHandlerMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isHandlerMethod(bridgedMethod))) {
					handlerMethods.add(specificMethod);
				}
				else if (isInitBinderMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isInitBinderMethod(bridgedMethod))) {
					initBinderMethods.add(specificMethod);
				}
				else if (isModelAttributeMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isModelAttributeMethod(bridgedMethod))) {
					modelAttributeMethods.add(specificMethod);
				}
			}
		}, ReflectionUtils.USER_DECLARED_METHODS);
	}
	this.typeLevelMapping = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
	SessionAttributes sessionAttributes = AnnotationUtils.findAnnotation(handlerType, SessionAttributes.class);
	this.sessionAttributesFound = (sessionAttributes != null);
	if (this.sessionAttributesFound) {
		this.sessionAttributeNames.addAll(Arrays.asList(sessionAttributes.names()));
		this.sessionAttributeTypes.addAll(Arrays.asList(sessionAttributes.types()));
	}
}
 
Example 8
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception {

	Class<?> clazz = ClassUtils.getUserClass(handler);
	Boolean annotatedWithSessionAttributes = this.sessionAnnotatedClassesCache.get(clazz);
	if (annotatedWithSessionAttributes == null) {
		annotatedWithSessionAttributes = (AnnotationUtils.findAnnotation(clazz, SessionAttributes.class) != null);
		this.sessionAnnotatedClassesCache.put(clazz, annotatedWithSessionAttributes);
	}

	if (annotatedWithSessionAttributes) {
		checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true);
	}
	else {
		checkAndPrepare(request, response, true);
	}

	// Execute invokeHandlerMethod in synchronized block if required.
	if (this.synchronizeOnSession) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object mutex = WebUtils.getSessionMutex(session);
			synchronized (mutex) {
				return invokeHandlerMethod(request, response, handler);
			}
		}
	}

	return invokeHandlerMethod(request, response, handler);
}
 
Example 9
/**
 * Create a new instance for a controller type. Session attribute names and
 * types are extracted from the {@code @SessionAttributes} annotation, if
 * present, on the given type.
 * @param handlerType the controller type
 * @param sessionAttributeStore used for session access
 */
public SessionAttributesHandler(Class<?> handlerType, SessionAttributeStore sessionAttributeStore) {
	Assert.notNull(sessionAttributeStore, "SessionAttributeStore may not be null.");
	this.sessionAttributeStore = sessionAttributeStore;

	SessionAttributes annotation = AnnotationUtils.findAnnotation(handlerType, SessionAttributes.class);
	if (annotation != null) {
		this.attributeNames.addAll(Arrays.asList(annotation.names()));
		this.attributeTypes.addAll(Arrays.asList(annotation.types()));
	}

	for (String attributeName : this.attributeNames) {
		this.knownAttributeNames.add(attributeName);
	}
}
 
Example 10
/**
 * Initialize a new HandlerMethodResolver for the specified handler type.
 * @param handlerType the handler class to introspect
 */
public void init(final Class<?> handlerType) {
	Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
	Class<?> specificHandlerType = null;
	if (!Proxy.isProxyClass(handlerType)) {
		handlerTypes.add(handlerType);
		specificHandlerType = handlerType;
	}
	handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
	for (Class<?> currentHandlerType : handlerTypes) {
		final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType);
		ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) {
				Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
				if (isHandlerMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isHandlerMethod(bridgedMethod))) {
					handlerMethods.add(specificMethod);
				}
				else if (isInitBinderMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isInitBinderMethod(bridgedMethod))) {
					initBinderMethods.add(specificMethod);
				}
				else if (isModelAttributeMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isModelAttributeMethod(bridgedMethod))) {
					modelAttributeMethods.add(specificMethod);
				}
			}
		}, ReflectionUtils.USER_DECLARED_METHODS);
	}
	this.typeLevelMapping = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
	SessionAttributes sessionAttributes = AnnotationUtils.findAnnotation(handlerType, SessionAttributes.class);
	this.sessionAttributesFound = (sessionAttributes != null);
	if (this.sessionAttributesFound) {
		this.sessionAttributeNames.addAll(Arrays.asList(sessionAttributes.names()));
		this.sessionAttributeTypes.addAll(Arrays.asList(sessionAttributes.types()));
	}
}
 
Example 11
Source Project: sinavi-jfw   Source File: Controllers.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
static String[] findSessionAttributeNames(Class<?> controllerClass) {
    SessionAttributes attrs = AnnotationUtils.findAnnotation(controllerClass, SessionAttributes.class);
    if (attrs != null) {
      return attrs.value();
    }
    return null;
}
 
Example 12
Source Project: sinavi-jfw   Source File: Controllers.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
static Class<?>[] findSessionAttributeTypes(Class<?> controllerClass) {
    SessionAttributes attrs = AnnotationUtils.findAnnotation(controllerClass, SessionAttributes.class);
    if (attrs != null) {
      return attrs.types();
    }
    return null;
}