Java Code Examples for org.springframework.web.util.WebUtils#getSessionMutex()

The following examples show how to use org.springframework.web.util.WebUtils#getSessionMutex() . 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 File: AbstractController.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	if (HttpMethod.OPTIONS.matches(request.getMethod())) {
		response.setHeader("Allow", getAllowHeader());
		return null;
	}

	// Delegate to WebContentGenerator for checking and preparing.
	checkRequest(request);
	prepareResponse(response);

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

	return handleRequestInternal(request, response);
}
 
Example 2
Source File: UserSessionInterceptor.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER_KEY);
    if (user != null) {
        return true;
    }
    Object obj = WebUtils.getSessionMutex(request.getSession());
    synchronized (obj) {
        log.debug("WorkflowUser not found in session under key: " + SESSION_USER_KEY);
        String currentUser = request.getParameter(PARAMETER_USER_KEY);
        if (Strings.isNullOrEmpty(currentUser)) {
            throw new InvalidAccessException("No user found");
        }
        user = identityService.createUserQuery().userId(currentUser).singleResult();
        if (user == null) {
            throw new InvalidAccessException("WorkflowUser: " + currentUser + " is unknown");
        }
        WebUtils.setSessionAttribute(request, SESSION_USER_KEY, user);
        return true;
    }
}
 
Example 3
Source File: AbstractController.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	// Delegate to WebContentGenerator for checking and preparing.
	checkRequest(request);
	prepareResponse(response);

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

	return handleRequestInternal(request, response);
}
 
Example 4
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected ModelAndView handleInternal(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

	checkRequest(request);

	if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
		applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
	}
	else {
		prepareResponse(response);
	}

	// 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, handlerMethod);
			}
		}
	}

	return invokeHandlerMethod(request, response, handlerMethod);
}
 
Example 5
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ModelAndView handleInternal(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

	ModelAndView mav;
	checkRequest(request);

	// 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) {
				mav = invokeHandlerMethod(request, response, handlerMethod);
			}
		}
		else {
			// No HttpSession available -> no mutex necessary
			mav = invokeHandlerMethod(request, response, handlerMethod);
		}
	}
	else {
		// No synchronization on session demanded at all...
		mav = invokeHandlerMethod(request, response, handlerMethod);
	}

	if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
		if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
			applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
		}
		else {
			prepareResponse(response);
		}
	}

	return mav;
}
 
Example 6
Source File: AbstractController.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	if (HttpMethod.OPTIONS.matches(request.getMethod())) {
		response.setHeader("Allow", getAllowHeader());
		return null;
	}

	// Delegate to WebContentGenerator for checking and preparing.
	checkRequest(request);
	prepareResponse(response);

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

	return handleRequestInternal(request, response);
}
 
Example 7
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 8
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected ModelAndView handleInternal(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

	ModelAndView mav;
	checkRequest(request);

	// 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) {
				mav = invokeHandlerMethod(request, response, handlerMethod);
			}
		}
		else {
			// No HttpSession available -> no mutex necessary
			mav = invokeHandlerMethod(request, response, handlerMethod);
		}
	}
	else {
		// No synchronization on session demanded at all...
		mav = invokeHandlerMethod(request, response, handlerMethod);
	}

	if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
		if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
			applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
		}
		else {
			prepareResponse(response);
		}
	}

	return mav;
}
 
Example 9
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ModelAndView handleInternal(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
	// 注释 10. 调用适配器方法
	ModelAndView mav;
	checkRequest(request);

	// Execute invokeHandlerMethod in synchronized block if required.
	// 判断 synchronizeOnSession 是否开启,开启的话,同一个 session 的请求将会串行执行
	if (this.synchronizeOnSession) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object mutex = WebUtils.getSessionMutex(session);
			synchronized (mutex) {
				mav = invokeHandlerMethod(request, response, handlerMethod);
			}
		}
		else {
			// No HttpSession available -> no mutex necessary
			mav = invokeHandlerMethod(request, response, handlerMethod);
		}
	}
	else {
		// No synchronization on session demanded at all...
		// 执行适配中真正的方法
		mav = invokeHandlerMethod(request, response, handlerMethod);
	}

	if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
		if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
			applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
		}
		else {
			prepareResponse(response);
		}
	}

	return mav;
}
 
Example 10
Source File: SessionUtils.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * mutexを取得します。
 *
 * @param request
 * @return
 */
public static Object getMutex(HttpServletRequest request) {
    val session = getSession(request);

    if (session != null) {
        val mutex = WebUtils.getSessionMutex(session);
        return mutex;
    }

    return null;
}
 
Example 11
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: AbstractController.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	if (HttpMethod.OPTIONS.matches(request.getMethod())) {
		response.setHeader("Allow", getAllowHeader());
		return null;
	}

	// Delegate to WebContentGenerator for checking and preparing.
	checkRequest(request);
	prepareResponse(response);

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

	return handleRequestInternal(request, response);
}
 
Example 13
Source File: ServletRequestAttributes.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object getSessionMutex() {
	return WebUtils.getSessionMutex(obtainSession());
}
 
Example 14
Source File: ServletRequestAttributes.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object getSessionMutex() {
	return WebUtils.getSessionMutex(getSession(true));
}
 
Example 15
Source File: ServletRequestAttributes.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Object getSessionMutex() {
	return WebUtils.getSessionMutex(obtainSession());
}
 
Example 16
Source File: ServletRequestAttributes.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Object getSessionMutex() {
	return WebUtils.getSessionMutex(getSession(true));
}
 
Example 17
Source File: SessionFlashMapManager.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Exposes the best available session mutex.
 * @see org.springframework.web.util.WebUtils#getSessionMutex
 * @see org.springframework.web.util.HttpSessionMutexListener
 */
@Override
protected Object getFlashMapsMutex(HttpServletRequest request) {
	return WebUtils.getSessionMutex(request.getSession());
}
 
Example 18
Source File: SessionFlashMapManager.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Exposes the best available session mutex.
 * @see org.springframework.web.util.WebUtils#getSessionMutex
 * @see org.springframework.web.util.HttpSessionMutexListener
 */
@Override
protected Object getFlashMapsMutex(HttpServletRequest request) {
	return WebUtils.getSessionMutex(request.getSession());
}
 
Example 19
Source File: SessionFlashMapManager.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Exposes the best available session mutex.
 * @see org.springframework.web.util.WebUtils#getSessionMutex
 * @see org.springframework.web.util.HttpSessionMutexListener
 */
@Override
protected Object getFlashMapsMutex(HttpServletRequest request) {
	return WebUtils.getSessionMutex(request.getSession());
}
 
Example 20
Source File: SessionFlashMapManager.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Exposes the best available session mutex.
 * @see org.springframework.web.util.WebUtils#getSessionMutex
 * @see org.springframework.web.util.HttpSessionMutexListener
 */
@Override
protected Object getFlashMapsMutex(HttpServletRequest request) {
	return WebUtils.getSessionMutex(request.getSession());
}