Spring Framework WebRequest - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework WebRequest class.

This code example shows how to use the following methods:getAttribute, setAttribute
 * @see org.springframework.transaction.support.TransactionSynchronizationManager 
 */ 
public class OpenEntityManagerInViewInterceptor extends EntityManagerFactoryAccessor implements AsyncWebRequestInterceptor { 
 
	/** 
	 * Suffix that gets appended to the EntityManagerFactory toString 
	 * representation for the "participate in existing entity manager 
	 * handling" request attribute. 
	 * @see #getParticipateAttributeName 
	 */ 
	public static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; 
 
 
	@Override 
	public void preHandle(WebRequest request) throws DataAccessException { 
 
		String participateAttributeName = getParticipateAttributeName(); 
 
		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); 
		if (asyncManager.hasConcurrentResult()) { 

8
CodeRank
This code example shows how to use the following methods:getAttribute, removeAttribute, setAttribute
	} 
 
	@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( 
7
CodeRank
This code example shows how to use the following methods:getAttribute, setAttribute
 * {@code Session} for the processing of an entire request. In particular, the 
 * reassociation of persistent objects with a Hibernate {@code Session} has to 
 * occur at the very beginning of request processing, to avoid clashes with already 
 * loaded instances of the same objects. 
 * 
 * @author Juergen Hoeller 
 * @since 3.1 
 * @see OpenSessionInViewFilter 
 * @see OpenSessionInterceptor 
 * @see org.springframework.orm.hibernate4.HibernateTransactionManager 
 * @see org.springframework.transaction.support.TransactionSynchronizationManager 
 * @see org.hibernate.SessionFactory#getCurrentSession() 
 */ 
public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor { 
 
	/** 
	 * Suffix that gets appended to the {@code SessionFactory} 
	 * {@code toString()} representation for the "participate in existing 
	 * session handling" request attribute. 
	 * @see #getParticipateAttributeName 

7
CodeRank
This code example shows how to use the following methods:getAttribute, setAttribute
	 * Obtain the {@link WebAsyncManager} for the current request, or if not 
	 * found, create and associate it with the request. 
	 */ 
	public static WebAsyncManager getAsyncManager(WebRequest webRequest) { 
		int scope = RequestAttributes.SCOPE_REQUEST; 
		WebAsyncManager asyncManager = (WebAsyncManager) webRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, scope); 
		if (asyncManager == null) { 
			asyncManager = new WebAsyncManager(); 
			webRequest.setAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, asyncManager, scope); 
		} 
		return asyncManager; 
	} 
 
	/** 
	 * Create an AsyncWebRequest instance. By default an instance of 
	 * {@link StandardServletAsyncWebRequest} is created if running in Servlet 
	 * 3.0 (or higher) environment or as a fallback, an instance of 
	 * {@link NoSupportAsyncWebRequest} is returned. 
	 * 
	 * @param request the current request 
7
CodeRank
This code example shows how to use the following methods:getAttribute, setAttribute
 * changing the default flush mode (through the 
 * {@link #setFlushMode(int) "flushMode"} property). 
 * 
 * <p>In contrast to {@link OpenSessionInViewFilter}, this interceptor is 
 * configured in a Spring application context and can thus take advantage of bean 
 * wiring. It inherits common Hibernate configuration properties from 
 * {@link org.springframework.orm.hibernate3.HibernateAccessor}, 
 * to be configured in a bean definition. 
 * 
 * <p><b>WARNING:</b> Applying this interceptor to existing logic can cause issues 
 * that have not appeared before, through the use of a single Hibernate 
 * {@code Session} for the processing of an entire request. In particular, the 
 * reassociation of persistent objects with a Hibernate {@code Session} has to 
 * occur at the very beginning of request processing, to avoid clashes with already 
 * loaded instances of the same objects. 
 * 
 * <p>Alternatively, turn this interceptor into deferred close mode, by specifying 
 * "singleSession"="false": It will not use a single session per request then, 
 * but rather let each data access operation or transaction use its own session 
 * (as would be the case without Open Session in View). Each of those sessions will 

7
CodeRank
This code example shows how to use the following methods:getAttribute, getParameter
	protected String getRequestValueForAttribute(String attributeName, NativeWebRequest request) { 
		Map<String, String> variables = getUriTemplateVariables(request); 
		if (StringUtils.hasText(variables.get(attributeName))) { 
			return variables.get(attributeName); 
		} 
		else if (StringUtils.hasText(request.getParameter(attributeName))) { 
			return request.getParameter(attributeName); 
		} 
		else { 
			return null; 
		} 
	} 
 
	@SuppressWarnings("unchecked") 
	protected final Map<String, String> getUriTemplateVariables(NativeWebRequest request) { 
		Map<String, String> variables = 
				(Map<String, String>) request.getAttribute( 
						HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); 
		return (variables != null ? variables : Collections.<String, String>emptyMap()); 
	} 

6
CodeRank
This code example shows how to use the following methods:getAttribute
	@Override 
	public Object retrieveAttribute(WebRequest request, String attributeName) {
		Assert.notNull(request, "WebRequest must not be null");
		Assert.notNull(attributeName, "Attribute name must not be null");
		String storeAttributeName = getAttributeNameInSession(request, attributeName);
		return request.getAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);
	} 
 
	@Override 
	public void cleanupAttribute(WebRequest request, String attributeName) {
		Assert.notNull(request, "WebRequest must not be null");
		Assert.notNull(attributeName, "Attribute name must not be null");
		String storeAttributeName = getAttributeNameInSession(request, attributeName);
		request.removeAttribute(storeAttributeName, WebRequest.SCOPE_SESSION);
	} 
 
 
	/** 
	 * Calculate the attribute name in the backend session. 
	 * <p>The default implementation simply prepends the configured 
6
CodeRank
This code example shows how to use the following methods:setAttribute
	 */ 
	protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, 
			HttpHeaders headers, HttpStatus status, WebRequest request) { 
 
		if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) { 
			request.setAttribute("javax.servlet.error.exception", ex, WebRequest.SCOPE_REQUEST); 
		} 
 
		return new ResponseEntity<Object>(body, headers, status); 
	} 
 
	/** 
	 * Customize the response for NoSuchRequestHandlingMethodException. 
	 * This method logs a warning and delegates to 
	 * {@link #handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}. 
	 * @param ex the exception 
	 * @param headers the headers to be written to the response 
	 * @param status the selected response status 
	 * @param request the current request 
	 * @return a {@code ResponseEntity} instance 
4
CodeRank
See Code Examples for Spring Framework WebRequest Methods: