use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class SwaggerTypeManager method convertTeiidRuntimeType.
static Object convertTeiidRuntimeType(Object value, Class<?> expectedType) throws TranslatorException {
if (value == null) {
return null;
}
if (expectedType.isAssignableFrom(value.getClass())) {
return value;
} else {
if (expectedType.isAssignableFrom(Timestamp.class) && value instanceof Long) {
return new Timestamp((Long) value);
} else if (expectedType.isAssignableFrom(java.sql.Timestamp.class) && value instanceof String) {
return formTimestamp((String) value);
} else if (expectedType.isAssignableFrom(java.util.Date.class) && value instanceof Long) {
return new java.util.Date((Long) value);
} else if (expectedType.isAssignableFrom(java.util.Date.class) && value instanceof String) {
return formDate((String) value);
} else if (expectedType.isArray() && value instanceof List) {
List<?> values = (List<?>) value;
Class<?> expectedArrayComponentType = expectedType.getComponentType();
Object array = Array.newInstance(expectedArrayComponentType, values.size());
for (int i = 0; i < values.size(); i++) {
Object arrayItem = convertTeiidRuntimeType(values.get(i), expectedArrayComponentType);
Array.set(array, i, arrayItem);
}
return array;
}
Transform transform = DataTypeManager.getTransform(value.getClass(), expectedType);
if (transform != null) {
try {
value = transform.transform(value, expectedType);
} catch (TransformationException e) {
throw new TranslatorException(e);
}
}
}
return value;
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class SolrExecutionFactory method convertFromSolrType.
public Object convertFromSolrType(final Object value, final Class<?> expectedType) {
if (value == null) {
return null;
}
if (expectedType.isInstance(value)) {
return value;
}
try {
if (expectedType.isArray()) {
ArrayList multiValues = (ArrayList) value;
Object transformed = Array.newInstance(expectedType.getComponentType(), multiValues.size());
for (int i = 0; i < multiValues.size(); i++) {
Object obj = multiValues.get(i);
if (obj == null) {
Array.set(transformed, i, obj);
continue;
}
if (expectedType.getComponentType().isInstance(obj)) {
Array.set(transformed, i, obj);
continue;
}
if (DataTypeManager.isTransformable(obj.getClass(), expectedType.getComponentType())) {
Array.set(transformed, i, DataTypeManager.transformValue(obj, expectedType.getComponentType()));
} else {
throw new TeiidRuntimeException(SolrPlugin.Event.TEIID20001, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20001, value, expectedType.getName()));
}
}
return transformed;
}
if (DataTypeManager.isTransformable(value.getClass(), expectedType)) {
return DataTypeManager.transformValue(value, expectedType);
}
throw new TeiidRuntimeException(SolrPlugin.Event.TEIID20001, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20001, value, expectedType.getName()));
} catch (TransformationException e) {
throw new TeiidRuntimeException(e);
}
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class DefaultAuthorizationValidator method ignoreUnathorizedInAsterisk.
private boolean ignoreUnathorizedInAsterisk(Command command, CommandContext commandContext) {
if (!(command instanceof Query)) {
return false;
}
Query query = (Query) command;
if (query.getInto() != null) {
return false;
}
if (ignoreUnauthorizedAsteriskDefault) {
return true;
}
Object value = commandContext.getSessionVariable(IGNORE_UNAUTHORIZED_ASTERISK);
if (value != null) {
try {
return Boolean.TRUE.equals(DataTypeManager.transformValue(value, DataTypeManager.DefaultDataClasses.BOOLEAN));
} catch (TransformationException e) {
}
}
return false;
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class Request method createParseInfo.
public static ParseInfo createParseInfo(RequestMessage requestMsg, SessionMetadata sessionMetadata) {
ParseInfo parseInfo = new ParseInfo();
parseInfo.ansiQuotedIdentifiers = requestMsg.isAnsiQuotedIdentifiers();
// $NON-NLS-1$
Object value = sessionMetadata.getSessionVariables().get("backslashDefaultMatchEscape");
try {
if (value != null && Boolean.TRUE.equals(DataTypeManager.transformValue(value, DataTypeManager.DefaultDataClasses.BOOLEAN))) {
parseInfo.setBackslashDefaultMatchEscape(true);
}
} catch (TransformationException e) {
}
return parseInfo;
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class FunctionDescriptor method invokeFunction.
/**
* Invoke the function described in the function descriptor, using the
* values provided. Return the result of the function.
* @param values Values that should match 1-to-1 with the types described in the
* function descriptor
* @param context
* @param functionTarget TODO
* @param fd Function descriptor describing the name and types of the arguments
* @return Result of invoking the function
*/
public Object invokeFunction(Object[] values, CommandContext context, Object functionTarget) throws FunctionExecutionException, BlockedException {
if (!isNullDependent()) {
for (int i = requiresContext ? 1 : 0; i < values.length; i++) {
if (values[i] == null) {
return null;
}
}
}
checkMethod();
// Invoke the method and return the result
try {
if (hasWrappedArgs) {
for (int i = 0; i < values.length; i++) {
Object val = values[i];
if (val != null && types[i] == DataTypeManager.DefaultDataClasses.VARBINARY) {
values[i] = ((BinaryType) val).getBytesDirect();
}
}
}
if (method.isVarArgs()) {
if (calledWithVarArgArrayParam) {
ArrayImpl av = (ArrayImpl) values[values.length - 1];
if (av != null) {
Object[] vals = av.getValues();
values[values.length - 1] = vals;
if (hasWrappedArgs && types[types.length - 1] == DataTypeManager.DefaultDataClasses.VARBINARY) {
vals = Arrays.copyOf(vals, vals.length);
for (int i = 0; i < vals.length; i++) {
if (vals[i] != null) {
vals[i] = ((BinaryType) vals[i]).getBytesDirect();
}
}
values[values.length - 1] = vals;
}
Class<?> arrayType = invocationMethod.getParameterTypes()[types.length - 1];
if (arrayType.getComponentType() != Object.class && vals.getClass() != arrayType) {
Object varArgs = Array.newInstance(arrayType.getComponentType(), vals.length);
for (int i = 0; i < vals.length; i++) {
Array.set(varArgs, i, vals[i]);
}
values[values.length - 1] = varArgs;
}
}
} else {
int i = invocationMethod.getParameterTypes().length;
Object[] newValues = Arrays.copyOf(values, i);
Object varArgs = null;
if (invocationMethod.getParameterTypes()[i - 1].getComponentType() != Object.class) {
int varArgCount = values.length - i + 1;
varArgs = Array.newInstance(invocationMethod.getParameterTypes()[i - 1].getComponentType(), varArgCount);
for (int j = 0; j < varArgCount; j++) {
Array.set(varArgs, j, values[i - 1 + j]);
}
} else {
varArgs = Arrays.copyOfRange(values, i - 1, values.length);
}
newValues[i - 1] = varArgs;
values = newValues;
}
}
Object result = null;
ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
try {
if (this.classLoader != null) {
Thread.currentThread().setContextClassLoader(this.classLoader);
}
result = invocationMethod.invoke(functionTarget, values);
} finally {
Thread.currentThread().setContextClassLoader(originalCL);
}
if (context != null && getDeterministic().ordinal() <= Determinism.USER_DETERMINISTIC.ordinal()) {
context.setDeterminismLevel(getDeterministic());
}
return importValue(result, getReturnType());
} catch (ArithmeticException e) {
throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, getFullName()));
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof BlockedException) {
throw (BlockedException) e.getTargetException();
}
throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e.getTargetException(), QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, getFullName()));
} catch (IllegalAccessException e) {
throw new FunctionExecutionException(QueryPlugin.Event.TEIID30385, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30385, method.toString()));
} catch (TransformationException e) {
throw new FunctionExecutionException(e);
}
}
Aggregations