Java Code Examples for org.springframework.web.context.request.async.WebAsyncUtils

The following are top voted examples for showing how to use org.springframework.web.context.request.async.WebAsyncUtils. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: microservices-sample-project   File: AsyncResponseEntityReturnHandler.java   View source code 8 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final AsyncResponseEntity<?> asyncResponseEntity = AsyncResponseEntity.class.cast(returnValue);
    
    Observable<?> observable = asyncResponseEntity.getObservable();
    Single<?> single = asyncResponseEntity.getSingle();
    MultiValueMap<String, String> headers =  asyncResponseEntity.getHeaders();
    HttpStatus status = asyncResponseEntity.getStatus();
    
    if(observable != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new ObservableDeferredResult<>(observable, headers, status), mavContainer);
    else if(single != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new SingleDeferredResult<>(single, headers, status), mavContainer);
    
}
 
Example 2
Project: pokemon   File: AsyncResponseEntityReturnHandler.java   View source code 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final AsyncResponseEntity<?> asyncResponseEntity = AsyncResponseEntity.class.cast(returnValue);
    
    Observable<?> observable = asyncResponseEntity.getObservable();
    Single<?> single = asyncResponseEntity.getSingle();
    MultiValueMap<String, String> headers =  asyncResponseEntity.getHeaders();
    HttpStatus status = asyncResponseEntity.getStatus();
    
    if(observable != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new ObservableDeferredResult<>(observable, headers, status), mavContainer);
    else if(single != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new SingleDeferredResult<>(single, headers, status), mavContainer);
    
}
 
Example 3
Project: spring4-understanding   File: ListenableFutureReturnValueHandler.java   View source code 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);

	ListenableFuture<?> future = (ListenableFuture<?>) returnValue;
	future.addCallback(new ListenableFutureCallback<Object>() {
		@Override
		public void onSuccess(Object result) {
			deferredResult.setResult(result);
		}
		@Override
		public void onFailure(Throwable ex) {
			deferredResult.setErrorResult(ex);
		}
	});
}
 
Example 4
Project: spring4-understanding   File: ResponseBodyEmitterReturnValueHandlerTests.java   View source code 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	List<HttpMessageConverter<?>> converters = Arrays.asList(
			new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());

	this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example 5
Project: spring4-understanding   File: ResponseBodyEmitterReturnValueHandlerTests.java   View source code 6 votes vote down vote up
@Test
public void timeoutValueAndCallback() throws Exception {

	AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
	WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);

	ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
	emitter.onTimeout(mock(Runnable.class));
	emitter.onCompletion(mock(Runnable.class));

	MethodParameter returnType = returnType(TestController.class, "handle");
	this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);

	verify(asyncWebRequest).setTimeout(19000L);
	verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
	verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
	verify(asyncWebRequest).startAsync();
}
 
Example 6
Project: searchbox-core   File: AsyncResponseEntityReturnHandler.java   View source code 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final AsyncResponseEntity<?> asyncResponseEntity = AsyncResponseEntity.class.cast(returnValue);
    
    Observable<?> observable = asyncResponseEntity.getObservable();
    Single<?> single = asyncResponseEntity.getSingle();
    MultiValueMap<String, String> headers =  asyncResponseEntity.getHeaders();
    HttpStatus status = asyncResponseEntity.getStatus();
    
    if(observable != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new ObservableDeferredResult<>(observable, headers, status), mavContainer);
    else if(single != null)
    	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(new SingleDeferredResult<>(single, headers, status), mavContainer);
    
}
 
Example 7
Project: daikon   File: UserIdLoggingFilter.java   View source code 6 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
        FilterChain filterChain) throws ServletException, IOException {
    setMdc();

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(httpServletRequest);

    UserIdLoggingFilter tenancyProcessingInterceptor = (UserIdLoggingFilter) asyncManager
            .getCallableInterceptor(CALLABLE_INTERCEPTOR_KEY);
    if (tenancyProcessingInterceptor == null) {
        asyncManager.registerCallableInterceptor(CALLABLE_INTERCEPTOR_KEY, new UserIdCallableProcessingInterceptorAdapter());
    }
    try {
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    } finally {
        removeMdc();
    }

}
 
Example 8
Project: chassis   File: ListenableFutureReturnValueHandler.java   View source code 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    final DeferredResult<Object> deferredResult = new DeferredResult<>();
    Futures.addCallback((ListenableFuture<?>) returnValue, new FutureCallback<Object>() {
        @Override
        public void onSuccess(@Nullable Object result) {
            deferredResult.setResult(result);
        }

        @Override
        public void onFailure(Throwable t) {
            deferredResult.setErrorResult(t);
        }
    });
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult,mavContainer);
}
 
Example 9
Project: trenako-v2   File: CompletableFutureReturnValueHandler.java   View source code 6 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue,
                              MethodParameter returnType,
                              ModelAndViewContainer mavContainer,
                              NativeWebRequest webRequest) throws Exception {
    final DeferredResult<Object> deferredResult = new DeferredResult<>();
    final CompletableFuture<?> future = (CompletableFuture<?>) returnValue;
    final CompletableFuture<Object> completedFuture = future.handle((ret, err) -> {
        if (ret != null) {
            deferredResult.setResult(ret);
            return ret;
        }
        else {
            deferredResult.setErrorResult(err);
            return null;
        }
    });

    WebAsyncUtils
            .getAsyncManager(webRequest)
            .startDeferredResultProcessing(deferredResult, mavContainer);
}
 
Example 10
Project: lams   File: OpenEntityManagerInViewInterceptor.java   View source code 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
Project: lams   File: OpenSessionInViewInterceptor.java   View source code 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 12
Project: lams   File: OpenSessionInViewInterceptor.java   View source code 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
@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 ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
		SessionFactoryUtils.isDeferredCloseActive(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 {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			SessionHolder sessionHolder = new SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
Example 13
Project: springboot-scala-withswagger   File: ObservableReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final Observable<?> observable = Observable.class.cast(returnValue);
    log.debug("handleReturnValue begin to process");
    WebAsyncUtils.getAsyncManager(webRequest)
            .startDeferredResultProcessing(new ObservableAdapter<>(observable), mavContainer);
    log.debug("handleReturnValue stop to process");
}
 
Example 14
Project: spring4-understanding   File: OpenEntityManagerInViewInterceptor.java   View source code 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 15
Project: spring4-understanding   File: OpenSessionInViewInterceptor.java   View source code 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
@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 ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
		SessionFactoryUtils.isDeferredCloseActive(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 {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			SessionHolder sessionHolder = new SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
Example 16
Project: spring4-understanding   File: StreamingResponseBodyReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
	ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

	if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) {
		ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
		outputMessage.setStatusCode(responseEntity.getStatusCode());
		outputMessage.getHeaders().putAll(responseEntity.getHeaders());

		returnValue = responseEntity.getBody();
		if (returnValue == null) {
			mavContainer.setRequestHandled(true);
			return;
		}
	}

	ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
	ShallowEtagHeaderFilter.disableContentCaching(request);

	Assert.isInstanceOf(StreamingResponseBody.class, returnValue);
	StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;

	Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
	WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
 
Example 17
Project: spring4-understanding   File: CallableMethodReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	Callable<?> callable = (Callable<?>) returnValue;
	WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
 
Example 18
Project: spring4-understanding   File: AsyncTaskMethodReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	WebAsyncTask<?> webAsyncTask = (WebAsyncTask<?>) returnValue;
	webAsyncTask.setBeanFactory(this.beanFactory);
	WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(webAsyncTask, mavContainer);
}
 
Example 19
Project: spring4-understanding   File: CompletionStageReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);

	@SuppressWarnings("unchecked")
	CompletionStage<Object> future = (CompletionStage<Object>) returnValue;
	future.thenAccept(new Consumer<Object>() {
		@Override
		public void accept(Object result) {
			deferredResult.setResult(result);
		}
	});
	future.exceptionally(new Function<Throwable, Object>() {
		@Override
		public Object apply(Throwable ex) {
			deferredResult.setErrorResult(ex);
			return null;
		}
	});
}
 
Example 20
Project: spring4-understanding   File: DeferredResultMethodReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
		ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	DeferredResult<?> deferredResult = (DeferredResult<?>) returnValue;
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
}
 
Example 21
Project: spring4-understanding   File: StreamingResponseBodyReturnValueHandlerTests.java   View source code 5 votes vote down vote up
@Before
public void setUp() throws Exception {

	this.handler = new StreamingResponseBodyReturnValueHandler();
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest("GET", "/path");
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example 22
Project: spring4-understanding   File: OpenSessionInViewInterceptor.java   View source code 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 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 23
Project: spring4-understanding   File: OpenSessionInViewInterceptor.java   View source code 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 24
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: ErrorPageFilterTests.java   View source code 5 votes vote down vote up
private void setUpAsyncDispatch() throws Exception {
	this.request.setAsyncSupported(true);
	this.request.setAsyncStarted(true);
	DeferredResult<String> result = new DeferredResult<String>();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setAsyncWebRequest(
			new StandardServletAsyncWebRequest(this.request, this.response));
	asyncManager.startDeferredResultProcessing(result);
}
 
Example 25
Project: rxjava-spring-boot-starter   File: SingleReturnValueHandler.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final Single<?> single = Single.class.cast(returnValue);
    WebAsyncUtils.getAsyncManager(webRequest)
            .startDeferredResultProcessing(new SingleDeferredResult(single), mavContainer);
}
 
Example 26
Project: rxjava-spring-boot-starter   File: ObservableReturnValueHandler.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final Observable<?> observable = Observable.class.cast(returnValue);
    WebAsyncUtils.getAsyncManager(webRequest)
            .startDeferredResultProcessing(new ObservableDeferredResult(observable), mavContainer);
}
 
Example 27
Project: spring-boot-concourse   File: ErrorPageFilterTests.java   View source code 5 votes vote down vote up
private void setUpAsyncDispatch() throws Exception {
	this.request.setAsyncSupported(true);
	this.request.setAsyncStarted(true);
	DeferredResult<String> result = new DeferredResult<String>();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setAsyncWebRequest(
			new StandardServletAsyncWebRequest(this.request, this.response));
	asyncManager.startDeferredResultProcessing(result);
}
 
Example 28
Project: contestparser   File: ErrorPageFilterTests.java   View source code 5 votes vote down vote up
private void setUpAsyncDispatch() throws Exception {
	this.request.setAsyncSupported(true);
	this.request.setAsyncStarted(true);
	DeferredResult<String> result = new DeferredResult<String>();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setAsyncWebRequest(
			new StandardServletAsyncWebRequest(this.request, this.response));
	asyncManager.startDeferredResultProcessing(result);
}
 
Example 29
Project: daikon   File: TenancyContextIntegrationFilter.java   View source code 5 votes vote down vote up
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

    TenancyContextIntegrationFilter tenancyProcessingInterceptor = (TenancyContextIntegrationFilter) asyncManager
            .getCallableInterceptor(CALLABLE_INTERCEPTOR_KEY);
    if (tenancyProcessingInterceptor == null) {
        asyncManager.registerCallableInterceptor(CALLABLE_INTERCEPTOR_KEY, new TenancyContextCallableProcessingInterceptor());
    }

    TenancyContext contextBeforeChainExecution = determineTenancyContext(request);

    try {
        TenancyContextHolder.setContext(contextBeforeChainExecution);
        setMdc(contextBeforeChainExecution);

        chain.doFilter(request, response);

    } finally {
        // Crucial removal of ContextHolder contents - do this
        // before anything else.
        TenancyContextHolder.clearContext();
        removeMdc();
    }

}
 
Example 30
Project: daikon   File: MultiTenantApplication.java   View source code 5 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    asyncManager.registerCallableInterceptor(KEY, this);
    filterChain.doFilter(request, response);
    this.checkContextIsClean();
}
 
Example 31
Project: class-guard   File: OpenEntityManagerInViewInterceptor.java   View source code 5 votes vote down vote up
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 32
Project: class-guard   File: OpenSessionInViewInterceptor.java   View source code 5 votes vote down vote up
/**
 * Open a new Hibernate {@code Session} according to the settings of this
 * {@code HibernateAccessor} and bind it to the thread via the
 * {@link TransactionSynchronizationManager}.
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession
 */
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

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

	if ((isSingleSession() && TransactionSynchronizationManager.hasResource(getSessionFactory())) ||
		SessionFactoryUtils.isDeferredCloseActive(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 {
		if (isSingleSession()) {
			// single session mode
			logger.debug("Opening single Hibernate Session in OpenSessionInViewInterceptor");
			Session session = SessionFactoryUtils.getSession(
					getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
			applyFlushMode(session, false);
			SessionHolder sessionHolder = new SessionHolder(session);
			TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);

			AsyncRequestInterceptor asyncRequestInterceptor =
					new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
		}
		else {
			// deferred close mode
			SessionFactoryUtils.initDeferredClose(getSessionFactory());
		}
	}
}
 
Example 33
Project: class-guard   File: CallableMethodReturnValueHandler.java   View source code 5 votes vote down vote up
public void handleReturnValue(Object returnValue,
		MethodParameter returnType, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	Callable<?> callable = (Callable<?>) returnValue;
	WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
 
Example 34
Project: class-guard   File: AsyncTaskMethodReturnValueHandler.java   View source code 5 votes vote down vote up
public void handleReturnValue(Object returnValue,
		MethodParameter returnType, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	WebAsyncTask<?> webAsyncTask = (WebAsyncTask<?>) returnValue;
	webAsyncTask.setBeanFactory(this.beanFactory);
	WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(webAsyncTask, mavContainer);
}
 
Example 35
Project: class-guard   File: DeferredResultMethodReturnValueHandler.java   View source code 5 votes vote down vote up
public void handleReturnValue(Object returnValue,
		MethodParameter returnType, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest) throws Exception {

	if (returnValue == null) {
		mavContainer.setRequestHandled(true);
		return;
	}

	DeferredResult<?> deferredResult = (DeferredResult<?>) returnValue;
	WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
}
 
Example 36
Project: class-guard   File: OpenSessionInViewInterceptor.java   View source code 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}.
 */
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 37
Project: chassis   File: ObserableReturnValueHandler.java   View source code 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    final DeferredResult<Object> deferredResult = new DeferredResult<>();
    ((Observable<?>) returnValue).subscribe( new Observer<Object>() {
        @Override
        public void onCompleted() {
            if (!deferredResult.hasResult()) {
                logger.error( "onComplete before onNext");
                deferredResult.setResult(null);
            }
        }

        @Override
        public void onError(Throwable e) {
            deferredResult.setErrorResult(e);
        }

        @Override
        public void onNext(Object args) {
            deferredResult.setResult(args);
        }
    });
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult,mavContainer);
}
 
Example 38
Project: grpc-java-contrib   File: GuavaLFReturnValueHandler.java   View source code 4 votes vote down vote up
@VisibleForTesting
protected void startDeferredResultProcessing(ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
                                   final DeferredResult<Object> deferredResult) throws Exception {
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
}
 
Example 39
Project: spring4-understanding   File: OpenEntityManagerInViewTests.java   View source code 4 votes vote down vote up
@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {

	// Initial request thread

	OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
	interceptor.setEntityManagerFactory(factory);

	given(factory.createEntityManager()).willReturn(this.manager);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	asyncManager.clearConcurrentResult();

	// check that further invocations simply participate
	interceptor.preHandle(new ServletWebRequest(request));

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.preHandle(new ServletWebRequest(request));
	interceptor.postHandle(new ServletWebRequest(request), null);
	interceptor.afterCompletion(new ServletWebRequest(request), null);

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(factory));

	given(this.manager.isOpen()).willReturn(true);

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(factory));

	verify(this.manager).close();
}
 
Example 40
Project: spring4-understanding   File: OpenSessionInViewTests.java   View source code 4 votes vote down vote up
@Test
public void testOpenSessionInViewInterceptorAsyncScenario() throws Exception {
	// Initial request thread

	final SessionFactory sf = mock(SessionFactory.class);
	Session session = mock(Session.class);

	OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
	interceptor.setSessionFactory(sf);

	given(sf.openSession()).willReturn(session);
	given(session.getSessionFactory()).willReturn(sf);

	interceptor.preHandle(this.webRequest);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
	asyncManager.setTaskExecutor(new SyncTaskExecutor());
	asyncManager.setAsyncWebRequest(asyncWebRequest);
	asyncManager.startCallableProcessing(new Callable<String>() {
		@Override
		public String call() throws Exception {
			return "anything";
		}
	});

	interceptor.afterConcurrentHandlingStarted(this.webRequest);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	// Async dispatch thread

	interceptor.preHandle(this.webRequest);
	assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf));

	interceptor.postHandle(this.webRequest, null);
	assertTrue(TransactionSynchronizationManager.hasResource(sf));

	verify(session, never()).close();

	interceptor.afterCompletion(this.webRequest, null);
	assertFalse(TransactionSynchronizationManager.hasResource(sf));

	verify(session).setFlushMode(FlushMode.MANUAL);
	verify(session).close();
}