Search in sources :

Example 1 with SerializableValueType

use of org.camunda.bpm.engine.variable.type.SerializableValueType in project camunda-bpm-platform by camunda.

the class VariableValueDto method toTypedValue.

public TypedValue toTypedValue(ProcessEngine processEngine, ObjectMapper objectMapper) {
    ValueTypeResolver valueTypeResolver = processEngine.getProcessEngineConfiguration().getValueTypeResolver();
    if (type == null) {
        if (valueInfo != null && valueInfo.get(ValueType.VALUE_INFO_TRANSIENT) instanceof Boolean) {
            return Variables.untypedValue(value, (Boolean) valueInfo.get(ValueType.VALUE_INFO_TRANSIENT));
        }
        return Variables.untypedValue(value);
    }
    ValueType valueType = valueTypeResolver.typeForName(fromRestApiTypeName(type));
    if (valueType == null) {
        throw new RestException(Status.BAD_REQUEST, String.format("Unsupported value type '%s'", type));
    } else {
        if (valueType instanceof PrimitiveValueType) {
            PrimitiveValueType primitiveValueType = (PrimitiveValueType) valueType;
            Class<?> javaType = primitiveValueType.getJavaType();
            Object mappedValue = null;
            try {
                if (value != null) {
                    if (javaType.isAssignableFrom(value.getClass())) {
                        mappedValue = value;
                    } else {
                        // use jackson to map the value to the requested java type
                        mappedValue = objectMapper.readValue("\"" + value + "\"", javaType);
                    }
                }
                return valueType.createValue(mappedValue, valueInfo);
            } catch (Exception e) {
                throw new InvalidRequestException(Status.BAD_REQUEST, e, String.format("Cannot convert value '%s' of type '%s' to java type %s", value, type, javaType.getName()));
            }
        } else if (valueType instanceof SerializableValueType) {
            if (value != null && !(value instanceof String)) {
                throw new InvalidRequestException(Status.BAD_REQUEST, "Must provide 'null' or String value for value of SerializableValue type '" + type + "'.");
            }
            return ((SerializableValueType) valueType).createValueFromSerialized((String) value, valueInfo);
        } else if (valueType instanceof FileValueType) {
            if (value instanceof String) {
                value = Base64.decodeBase64((String) value);
            }
            return valueType.createValue(value, valueInfo);
        } else {
            return valueType.createValue(value, valueInfo);
        }
    }
}
Also used : SerializableValueType(org.camunda.bpm.engine.variable.type.SerializableValueType) SerializableValueType(org.camunda.bpm.engine.variable.type.SerializableValueType) FileValueType(org.camunda.bpm.engine.variable.type.FileValueType) ValueType(org.camunda.bpm.engine.variable.type.ValueType) PrimitiveValueType(org.camunda.bpm.engine.variable.type.PrimitiveValueType) PrimitiveValueType(org.camunda.bpm.engine.variable.type.PrimitiveValueType) RestException(org.camunda.bpm.engine.rest.exception.RestException) ValueTypeResolver(org.camunda.bpm.engine.variable.type.ValueTypeResolver) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) FileValueType(org.camunda.bpm.engine.variable.type.FileValueType) InvalidRequestException(org.camunda.bpm.engine.rest.exception.InvalidRequestException) MimeTypeParseException(javax.activation.MimeTypeParseException) RestException(org.camunda.bpm.engine.rest.exception.RestException)

Aggregations

MimeTypeParseException (javax.activation.MimeTypeParseException)1 InvalidRequestException (org.camunda.bpm.engine.rest.exception.InvalidRequestException)1 RestException (org.camunda.bpm.engine.rest.exception.RestException)1 FileValueType (org.camunda.bpm.engine.variable.type.FileValueType)1 PrimitiveValueType (org.camunda.bpm.engine.variable.type.PrimitiveValueType)1 SerializableValueType (org.camunda.bpm.engine.variable.type.SerializableValueType)1 ValueType (org.camunda.bpm.engine.variable.type.ValueType)1 ValueTypeResolver (org.camunda.bpm.engine.variable.type.ValueTypeResolver)1