Java Code Examples for org.springframework.web.context.request.WebRequest#setAttribute()

The following examples show how to use org.springframework.web.context.request.WebRequest#setAttribute() . 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: OpenPersistenceManagerInViewInterceptor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
		// Do not modify the PersistenceManager: just mark the request accordingly.
		String participateAttributeName = getParticipateAttributeName();
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
		PersistenceManager pm =
				PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
		TransactionSynchronizationManager.bindResource(
				getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
	}
}
 
Example 2
Source File: OpenPersistenceManagerInViewInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();
	Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	if (count != null) {
		// Do not modify the PersistenceManager: just clear the marker.
		if (count > 1) {
			request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
		}
		else {
			request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		}
	}
	else {
		PersistenceManagerHolder pmHolder = (PersistenceManagerHolder)
				TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
		logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
		PersistenceManagerFactoryUtils.releasePersistenceManager(
				pmHolder.getPersistenceManager(), getPersistenceManagerFactory());
	}
}
 
Example 3
Source File: AdminController.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@RequestMapping(path = "/SessionServlet", method = RequestMethod.GET)
public String sessionServlet(WebRequest webRequest, Model model) {
 String counterString = (String) webRequest.getAttribute("counter", RequestAttributes.SCOPE_SESSION);
 int counter = 0;
 try {
     counter = Integer.parseInt(counterString, 10);
    }
    catch (NumberFormatException ignored) {
    }

    model.addAttribute("counter", counter);

 webRequest.setAttribute("counter", Integer.toString(counter+1), RequestAttributes.SCOPE_SESSION);

 return "session";
}
 
Example 4
Source File: DefaultSessionAttributeStore.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {
	Assert.notNull(request, "WebRequest must not be null");
	Assert.notNull(attributeName, "Attribute name must not be null");
	Assert.notNull(attributeValue, "Attribute value must not be null");
	String storeAttributeName = getAttributeNameInSession(request, attributeName);
	request.setAttribute(storeAttributeName, attributeValue, WebRequest.SCOPE_SESSION);
}
 
Example 5
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean decrementParticipateCount(WebRequest request) {
	String participateAttributeName = getParticipateAttributeName();
	Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	if (count == null) {
		return false;
	}
	// Do not modify the Session: just clear the marker.
	if (count > 1) {
		request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
	}
	else {
		request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	}
	return true;
}
 
Example 6
Source File: OpenSessionInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
private boolean decrementParticipateCount(WebRequest request) {
	String participateAttributeName = getParticipateAttributeName();
	Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	if (count == null) {
		return false;
	}
	// Do not modify the Session: just clear the marker.
	if (count > 1) {
		request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
	}
	else {
		request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	}
	return true;
}
 
Example 7
Source File: OpenSessionInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applySessionBindingInterceptor(asyncManager, key)) {
		return;
	}

	if (TransactionSynchronizationManager.hasResource(obtainSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(obtainSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(obtainSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(key, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(key, asyncRequestInterceptor);
	}
}
 
Example 8
Source File: OpenEntityManagerInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applyEntityManagerBindingInterceptor(asyncManager, key)) {
		return;
	}

	EntityManagerFactory emf = obtainEntityManagerFactory();
	if (TransactionSynchronizationManager.hasResource(emf)) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(emf, emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
			asyncManager.registerCallableInterceptor(key, interceptor);
			asyncManager.registerDeferredResultInterceptor(key, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 9
Source File: OpenSessionInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 10
Source File: OpenEntityManagerInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example 11
Source File: DefaultSessionAttributeStore.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {
	Assert.notNull(request, "WebRequest must not be null");
	Assert.notNull(attributeName, "Attribute name must not be null");
	Assert.notNull(attributeValue, "Attribute value must not be null");
	String storeAttributeName = getAttributeNameInSession(request, attributeName);
	request.setAttribute(storeAttributeName, attributeValue, WebRequest.SCOPE_SESSION);
}
 
Example 12
Source File: OpenSessionInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according and bind it to the thread via the
 * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
 */
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
		// Do not modify the Session: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
		Session session = openSession();
		SessionHolder sessionHolder = new SessionHolder(session);
		TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

		AsyncRequestInterceptor asyncRequestInterceptor =
				new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
		asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
		asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
	}
}
 
Example 13
Source File: OpenSessionInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean decrementParticipateCount(WebRequest request) {
	String participateAttributeName = getParticipateAttributeName();
	Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	if (count == null) {
		return false;
	}
	// Do not modify the Session: just clear the marker.
	if (count > 1) {
		request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
	}
	else {
		request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	}
	return true;
}
 
Example 14
Source File: SessionConversationAttributeStore.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
public void storeAttribute(WebRequest request, String attributeName, Object attributeValue) {
    Assert.notNull(request, "WebRequest must not be null");
    Assert.notNull(attributeName, "Attribute name must not be null");
    Assert.notNull(attributeValue, "Attribute value must not be null");
    
    // look for a conversation id as a request parameter
    String cId = getConversationIdFromRequest(request, attributeName);
    
    // create a new conversation id if it does not exist.
    if (cId == null || cId.trim().length() == 0) {
        cId = String.valueOf(System.currentTimeMillis());

        // set a request attribute so that the view can use it to pass along the 
        // conversation id.
        request.setAttribute(attributeName + "_cId", cId, WebRequest.SCOPE_REQUEST);
    }
    
    // calculate the session lookup key.
    String sessionLookupKey = calculateSessionLookupKey(attributeName, cId);
    
    _logger.debug("storeAttribute - storing bean reference for (" + sessionLookupKey + ").");
    
    // set the attribute value in the session.
    request.setAttribute(sessionLookupKey, attributeValue, WebRequest.SCOPE_SESSION);

    // handles adding to the queue and pruning old conversations if needed.
    handleQueueActions(request, attributeName, cId);
}
 
Example 15
Source File: OpenEntityManagerInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean decrementParticipateCount(WebRequest request) {
	String participateAttributeName = getParticipateAttributeName();
	Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	if (count == null) {
		return false;
	}
	// Do not modify the Session: just clear the marker.
	if (count > 1) {
		request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
	}
	else {
		request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
	}
	return true;
}
 
Example 16
Source File: ExceptionTranslator.java    From openapi-petstore with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, Object> processConstraintViolationException(WebRequest request) {
    request.setAttribute("javax.servlet.error.status_code", HttpStatus.BAD_REQUEST.value(), RequestAttributes.SCOPE_REQUEST);
    return errorAttributes.getErrorAttributes(request, false);
}
 
Example 17
Source File: ComplexWebApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws Exception {
	request.setAttribute("test3y", "test3y", WebRequest.SCOPE_REQUEST);
}
 
Example 18
Source File: ComplexWebApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws Exception {
	request.setAttribute("test3", "test3", WebRequest.SCOPE_REQUEST);
}
 
Example 19
Source File: ComplexWebApplicationContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, Exception ex) throws Exception {
	request.setAttribute("test3y", "test3y", WebRequest.SCOPE_REQUEST);
}
 
Example 20
Source File: ResponseEntityExceptionHandler.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * A single place to customize the response body of all Exception types.
 * <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE}
 * request attribute and creates a {@link ResponseEntity} from the given
 * body, headers, and status.
 * @param ex the exception
 * @param body the body for the response
 * @param headers the headers for the response
 * @param status the response status
 * @param request the current request
 */
protected ResponseEntity<Object> handleExceptionInternal(
		Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {

	if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
		request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
	}
	return new ResponseEntity<>(body, headers, status);
}