org.springframework.boot.web.servlet.error.ErrorController Java Examples

The following examples show how to use org.springframework.boot.web.servlet.error.ErrorController. 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: ServletErrorsAutoConfiguration.java    From errors-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Registers a custom {@link ErrorController} to change the default error handling approach.
 *
 * @param errorAttributes    Will be used to enrich error responses.
 * @param serverProperties   Will be used to access error related configurations.
 * @param errorViewResolvers All possible view resolvers to render the whitelabel error page.
 * @return The custom error controller instance.
 */
@Bean
@ConditionalOnBean(WebErrorHandlers.class)
@ConditionalOnMissingBean(ErrorController.class)
public BasicErrorController customErrorController(ErrorAttributes errorAttributes,
                                             ServerProperties serverProperties,
                                             ObjectProvider<ErrorViewResolver> errorViewResolvers) {
    List<ErrorViewResolver> resolvers = errorViewResolvers.orderedStream().collect(toList());
    return new CustomServletErrorController(errorAttributes, serverProperties.getError(), resolvers);
}
 
Example #2
Source File: TransactionHandlerInterceptor.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
		throws Exception {
	if (HandlerMethod.class.isInstance(handler) == false) {
		return;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	if (TransactionCoordinatorController.class.equals(clazz)) {
		return;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return;
	}

	String transactionText = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionText)) {
		return;
	}

	SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
	TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory();
	TransactionManager transactionManager = beanFactory.getTransactionManager();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	Transaction transaction = transactionManager.getTransactionQuietly();
	TransactionContext transactionContext = transaction.getTransactionContext();

	// byte[] byteArray = SerializeUtils.serializeObject(transactionContext);
	// String transactionStr = ByteUtils.byteArrayToString(byteArray);
	// response.setHeader(HEADER_TRANCACTION_KEY, transactionStr);
	// response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);

	TransactionResponseImpl resp = new TransactionResponseImpl();
	resp.setTransactionContext(transactionContext);
	resp.setSourceTransactionCoordinator(beanRegistry.getConsumeCoordinator(null));

	transactionInterceptor.beforeSendResponse(resp);

}
 
Example #3
Source File: BladeErrorMvcAutoConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
	return new BladeErrorController(errorAttributes, serverProperties.getError());
}
 
Example #4
Source File: CrnkErrorControllerAutoConfiguration.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController jsonapiErrorController(ErrorAttributes errorAttributes) {
	return new CrnkErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);
}
 
Example #5
Source File: CompensableHandlerInterceptor.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	String transactionStr = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionStr)) {
		return true;
	}

	if (HandlerMethod.class.isInstance(handler) == false) {
		logger.warn("CompensableHandlerInterceptor cannot handle current request(uri= {}, handler= {}) correctly.",
				request.getRequestURI(), handler);
		return true;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	Method method = hm.getMethod();
	if (CompensableCoordinatorController.class.equals(clazz)) {
		return true;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return true;
	}

	String propagationStr = request.getHeader(HEADER_PROPAGATION_KEY);

	String transactionText = StringUtils.trimToNull(transactionStr);
	String propagationText = StringUtils.trimToNull(propagationStr);

	Transactional globalTransactional = clazz.getAnnotation(Transactional.class);
	Transactional methodTransactional = method.getAnnotation(Transactional.class);
	boolean transactionalDefined = globalTransactional != null || methodTransactional != null;
	Compensable annotation = clazz.getAnnotation(Compensable.class);
	if (transactionalDefined && annotation == null) {
		logger.warn("Invalid transaction definition(uri={}, handler= {})!", request.getRequestURI(), handler,
				new IllegalStateException());
		return true;
	}

	SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
	CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	byte[] byteArray = transactionText == null ? new byte[0] : Base64.getDecoder().decode(transactionText);

	TransactionContext transactionContext = null;
	if (byteArray != null && byteArray.length > 0) {
		transactionContext = (TransactionContext) SerializeUtils.deserializeObject(byteArray);
		transactionContext.setPropagated(true);
		transactionContext.setPropagatedBy(propagationText);
	}

	TransactionRequestImpl req = new TransactionRequestImpl();
	req.setTransactionContext(transactionContext);
	req.setTargetTransactionCoordinator(beanRegistry.getConsumeCoordinator(propagationText));

	transactionInterceptor.afterReceiveRequest(req);

	CompensableManager compensableManager = beanFactory.getCompensableManager();
	CompensableTransaction compensable = compensableManager.getCompensableTransactionQuietly();
	String propagatedBy = (String) compensable.getTransactionContext().getPropagatedBy();

	byte[] responseByteArray = SerializeUtils.serializeObject(compensable.getTransactionContext());
	String compensableStr = Base64.getEncoder().encodeToString(responseByteArray);
	response.setHeader(HEADER_TRANCACTION_KEY, compensableStr);
	response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);

	String sourceApplication = CommonUtils.getApplication(propagatedBy);
	String targetApplication = CommonUtils.getApplication(propagationText);
	if (StringUtils.isNotBlank(sourceApplication) && StringUtils.isNotBlank(targetApplication)) {
		response.setHeader(HEADER_RECURSIVELY_KEY,
				String.valueOf(StringUtils.equalsIgnoreCase(sourceApplication, targetApplication) == false));
	}

	return true;
}
 
Example #6
Source File: CompensableHandlerInterceptor.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
		throws Exception {
	String transactionStr = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionStr)) {
		return;
	}

	if (HandlerMethod.class.isInstance(handler) == false) {
		return;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	Method method = hm.getMethod();
	if (CompensableCoordinatorController.class.equals(clazz)) {
		return;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return;
	}

	Transactional globalTransactional = clazz.getAnnotation(Transactional.class);
	Transactional methodTransactional = method.getAnnotation(Transactional.class);
	boolean transactionalDefined = globalTransactional != null || methodTransactional != null;
	Compensable annotation = clazz.getAnnotation(Compensable.class);
	if (transactionalDefined && annotation == null) {
		return;
	}

	SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
	CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
	CompensableManager compensableManager = beanFactory.getCompensableManager();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	CompensableTransaction compensable = compensableManager.getCompensableTransactionQuietly();
	TransactionContext transactionContext = compensable.getTransactionContext();

	// byte[] byteArray = SerializeUtils.serializeObject(transactionContext);
	// String compensableStr = ByteUtils.byteArrayToString(byteArray);
	// response.setHeader(HEADER_TRANCACTION_KEY, compensableStr);
	// response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);

	TransactionResponseImpl resp = new TransactionResponseImpl();
	resp.setTransactionContext(transactionContext);
	resp.setSourceTransactionCoordinator(beanRegistry.getConsumeCoordinator(null));

	transactionInterceptor.beforeSendResponse(resp);

}
 
Example #7
Source File: CompensableHandlerInterceptor.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	String transactionStr = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionStr)) {
		return true;
	}

	if (HandlerMethod.class.isInstance(handler) == false) {
		return true;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	Method method = hm.getMethod();
	if (CompensableCoordinatorController.class.equals(clazz)) {
		return true;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return true;
	}

	String propagationStr = request.getHeader(HEADER_PROPAGATION_KEY);

	String transactionText = StringUtils.trimToNull(transactionStr);
	String propagationText = StringUtils.trimToNull(propagationStr);

	Transactional globalTransactional = clazz.getAnnotation(Transactional.class);
	Transactional methodTransactional = method.getAnnotation(Transactional.class);
	boolean transactionalDefined = globalTransactional != null || methodTransactional != null;
	Compensable annotation = clazz.getAnnotation(Compensable.class);
	if (transactionalDefined && annotation == null) {
		logger.warn("Invalid transaction definition(uri={}, handler= {})!", request.getRequestURI(), handler,
				new IllegalStateException());
		return true;
	}

	SpringBootBeanRegistry beanRegistry = SpringBootBeanRegistry.getInstance();
	CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	byte[] byteArray = transactionText == null ? new byte[0] : Base64.getDecoder().decode(transactionText);

	TransactionContext transactionContext = null;
	if (byteArray != null && byteArray.length > 0) {
		transactionContext = (TransactionContext) SerializeUtils.deserializeObject(byteArray);
		transactionContext.setPropagated(true);
		transactionContext.setPropagatedBy(propagationText);
	}

	TransactionRequestImpl req = new TransactionRequestImpl();
	req.setTransactionContext(transactionContext);
	req.setTargetTransactionCoordinator(beanRegistry.getConsumeCoordinator(propagationText));

	transactionInterceptor.afterReceiveRequest(req);

	CompensableManager compensableManager = beanFactory.getCompensableManager();
	CompensableTransaction compensable = compensableManager.getCompensableTransactionQuietly();
	String propagatedBy = (String) compensable.getTransactionContext().getPropagatedBy();

	byte[] responseByteArray = SerializeUtils.serializeObject(compensable.getTransactionContext());
	String compensableStr = Base64.getEncoder().encodeToString(responseByteArray);
	response.setHeader(HEADER_TRANCACTION_KEY, compensableStr);
	response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);
	response.setHeader(HEADER_RECURSIVELY_KEY,
			String.valueOf(StringUtils.equalsIgnoreCase(propagatedBy, propagationText) == false));

	return true;
}
 
Example #8
Source File: CompensableHandlerInterceptor.java    From ByteTCC with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
		throws Exception {
	String transactionStr = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionStr)) {
		return;
	}

	if (HandlerMethod.class.isInstance(handler) == false) {
		return;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	Method method = hm.getMethod();
	if (CompensableCoordinatorController.class.equals(clazz)) {
		return;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return;
	}

	Transactional globalTransactional = clazz.getAnnotation(Transactional.class);
	Transactional methodTransactional = method.getAnnotation(Transactional.class);
	boolean transactionalDefined = globalTransactional != null || methodTransactional != null;
	Compensable annotation = clazz.getAnnotation(Compensable.class);
	if (transactionalDefined && annotation == null) {
		return;
	}

	SpringBootBeanRegistry beanRegistry = SpringBootBeanRegistry.getInstance();
	CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
	CompensableManager compensableManager = beanFactory.getCompensableManager();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	CompensableTransaction compensable = compensableManager.getCompensableTransactionQuietly();
	TransactionContext transactionContext = compensable.getTransactionContext();

	byte[] byteArray = SerializeUtils.serializeObject(transactionContext);
	String compensableStr = Base64.getEncoder().encodeToString(byteArray);
	response.setHeader(HEADER_TRANCACTION_KEY, compensableStr);
	response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);

	TransactionResponseImpl resp = new TransactionResponseImpl();
	resp.setTransactionContext(transactionContext);
	resp.setSourceTransactionCoordinator(beanRegistry.getConsumeCoordinator(null));

	transactionInterceptor.beforeSendResponse(resp);

}
 
Example #9
Source File: TransactionHandlerInterceptor.java    From ByteJTA with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	if (HandlerMethod.class.isInstance(handler) == false) {
		logger.warn("TransactionHandlerInterceptor cannot handle current request(uri= {}, handler= {}) correctly.",
				request.getRequestURI(), handler);
		return true;
	}

	HandlerMethod hm = (HandlerMethod) handler;
	Class<?> clazz = hm.getBeanType();
	if (TransactionCoordinatorController.class.equals(clazz)) {
		return true;
	} else if (ErrorController.class.isInstance(hm.getBean())) {
		return true;
	}

	String transactionStr = request.getHeader(HEADER_TRANCACTION_KEY);
	if (StringUtils.isBlank(transactionStr)) {
		return true;
	}

	String propagationStr = request.getHeader(HEADER_PROPAGATION_KEY);

	String transactionText = StringUtils.trimToNull(transactionStr);
	String propagationText = StringUtils.trimToNull(propagationStr);

	SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
	TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory();
	TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();

	byte[] byteArray = transactionText == null ? new byte[0] : Base64.getDecoder().decode(transactionText);

	TransactionContext transactionContext = null;
	if (byteArray != null && byteArray.length > 0) {
		transactionContext = (TransactionContext) SerializeUtils.deserializeObject(byteArray);
		transactionContext.setPropagated(true);
		transactionContext.setPropagatedBy(propagationText);
	}

	TransactionRequestImpl req = new TransactionRequestImpl();
	req.setTransactionContext(transactionContext);
	req.setTargetTransactionCoordinator(beanRegistry.getConsumeCoordinator(propagationText));

	transactionInterceptor.afterReceiveRequest(req);

	TransactionManager transactionManager = beanFactory.getTransactionManager();
	Transaction transaction = transactionManager.getTransactionQuietly();
	byte[] responseByteArray = SerializeUtils.serializeObject(transaction.getTransactionContext());
	String responseTransactionStr = Base64.getEncoder().encodeToString(responseByteArray);
	response.setHeader(HEADER_TRANCACTION_KEY, responseTransactionStr);
	response.setHeader(HEADER_PROPAGATION_KEY, this.identifier);

	return true;
}