org.apache.camel.TypeConversionException Java Examples

The following examples show how to use org.apache.camel.TypeConversionException. 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: ExchangeUtils.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the value of the Camel header that contains the userId to be set as the process initiator.
 * Returns null if no header name was specified on the Camel route.
 * 
 * @param exchange The Camel Exchange object
 * @param activitiEndpoint The ActivitiEndpoint implementation
 * @return The userId of the user to be set as the process initiator
 */
public static String prepareInitiator(Exchange exchange, ActivitiEndpoint activitiEndpoint) {
   
    String initiator = null;
    if (activitiEndpoint.isSetProcessInitiator()) {
        try {
            initiator = exchange.getIn().getHeader(activitiEndpoint.getProcessInitiatorHeaderName(), String.class);
        }
        catch (TypeConversionException e) {
            throw new ActivitiException("Initiator header '" + 
                    activitiEndpoint.getProcessInitiatorHeaderName() + "': Value must be of type String.", e);
        }
        
        if (StringUtils.isEmpty(initiator)) {
            throw new ActivitiException("Initiator header '" + 
                    activitiEndpoint.getProcessInitiatorHeaderName() + "': Value must be provided");
        }
    }
    return initiator;
}
 
Example #2
Source File: ExchangeUtils.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the value of the Camel header that contains the userId to be set as the process initiator. Returns null if no header name was specified on the Camel route.
 *
 * @param exchange The Camel Exchange object
 * @param endpoint The endPoint implementation
 * @return The userId of the user to be set as the process initiator
 */
public static String prepareInitiator(Exchange exchange, FlowableEndpoint endpoint) {

    String initiator = null;
    if (endpoint.isSetProcessInitiator()) {
        try {
            initiator = exchange.getIn().getHeader(endpoint.getProcessInitiatorHeaderName(), String.class);
        } catch (TypeConversionException e) {
            throw new FlowableException("Initiator header '" +
                    endpoint.getProcessInitiatorHeaderName() + "': Value must be of type String.", e);
        }

        if (StringUtils.isEmpty(initiator)) {
            throw new FlowableException("Initiator header '" +
                    endpoint.getProcessInitiatorHeaderName() + "': Value must be provided");
        }
    }
    return initiator;
}
 
Example #3
Source File: SpringTypeConverter.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    // do not attempt to convert Camel types
    if (type.getCanonicalName().startsWith("org.apache")) {
        return null;
    }
    
    // do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
    if (type.isAssignableFrom(Map.class) && isArrayOrCollection(value)) {
        return null;
    }

    TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
    TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);

    for (ConversionService conversionService : conversionServices) {
        if (conversionService.canConvert(sourceType, targetType)) {
            try {
                return (T)conversionService.convert(value, sourceType, targetType);
            } catch (ConversionFailedException e) {
                // if value is a collection or an array the check ConversionService::canConvert
                // may return true but then the conversion of specific objects may fail
                //
                // https://issues.apache.org/jira/browse/CAMEL-10548
                // https://jira.spring.io/browse/SPR-14971
                //
                if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
                    return null;
                } else {
                    throw new TypeConversionException(value, type, e);
                }
            }
        }
    }

    return null;
}
 
Example #4
Source File: SimpleFunctionConverter.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public <T> T convertTo(Class<T> type, Object value) throws TypeConversionException {
    if (value == null && !allowNull) {
        return null;
    }
    if (!fromType.isAssignableFrom(value.getClass())) {
        return null;
    }
    O result = conversion.apply(fromType.cast(value));
    if (type.isAssignableFrom(toType)) {
        return type.cast(result);
    }
    return null;
}
 
Example #5
Source File: SimpleFunctionConverter.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T mandatoryConvertTo(Class<T> type, Object value) throws TypeConversionException,
    NoTypeConversionAvailableException {
    if (value == null && !allowNull) {
        throw new NoTypeConversionAvailableException(value, type);
    }
    if (!fromType.isAssignableFrom(value.getClass())) {
        throw new NoTypeConversionAvailableException(value, type);
    }
    O result = conversion.apply(fromType.cast(value));
    if (type.isAssignableFrom(toType)) {
        return type.cast(result);
    }
    throw new NoTypeConversionAvailableException(value, type);
}
 
Example #6
Source File: SimpleFunctionConverter.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T tryConvertTo(Class<T> type, Object value) {
    try {
        return convertTo(type, value);
    } catch (TypeConversionException swallow) {
        return null;
    }
}
 
Example #7
Source File: SimpleFunctionConverter.java    From container with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    return convertTo(type, value);
}
 
Example #8
Source File: SimpleFunctionConverter.java    From container with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException,
    NoTypeConversionAvailableException {
    return mandatoryConvertTo(type, value);
}