use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class AccumuloDataTypeManager method deserialize.
public static Object deserialize(final byte[] value, final Class<?> expectedType) {
if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
return null;
}
try {
if (expectedType.isAssignableFrom(Clob.class)) {
return new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}, -1);
} else if (expectedType.isAssignableFrom(Blob.class)) {
return new BlobType(new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}));
} else if (expectedType.isAssignableFrom(SQLXML.class)) {
return new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
});
} else if (expectedType.isAssignableFrom(BinaryType.class)) {
return new BinaryType(value);
} else if (expectedType.isAssignableFrom(GeometryType.class)) {
GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
result.setSrid(srid);
return result;
} else if (expectedType.isAssignableFrom(byte[].class)) {
return value;
} else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
} else {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
Object obj = ois.readObject();
ois.close();
return obj;
}
} catch (ClassNotFoundException e) {
throw new TeiidRuntimeException(e);
} catch (IOException e) {
throw new TeiidRuntimeException(e);
} catch (TransformationException e) {
throw new TeiidRuntimeException(e);
}
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class TeiidServiceHandler method handleLobResult.
private void handleLobResult(String charSet, Object result, ServiceResponse response) throws SQLException {
if (result == null) {
// or should this be an empty result?
return;
}
if (result instanceof SQLXML) {
if (charSet != null) {
XMLSerialize serialize = new XMLSerialize();
// $NON-NLS-1$
serialize.setTypeString("blob");
serialize.setDeclaration(true);
serialize.setEncoding(charSet);
serialize.setDocument(true);
try {
InputStream content = ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
} catch (TransformationException e) {
throw new SQLException(e);
}
} else {
InputStream content = ((SQLXML) result).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_XML);
}
} else if (result instanceof Blob) {
InputStream content = ((Blob) result).getBinaryStream();
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
} else if (result instanceof Clob) {
InputStream content = new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
response.writeContent(content, 200, false);
response.writeOK(ContentType.TEXT_PLAIN);
} else {
InputStream content = new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
response.writeContent(content, 200, false);
response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
}
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class StringToTimeTransform method transformDirect.
/**
* This method transforms a value of the source type into a value
* of the target type.
* @param value Incoming value of source type
* @return Outgoing value of target type
* @throws TransformationException if value is an incorrect input type or
* the transformation fails
*/
public Object transformDirect(Object value) throws TransformationException {
value = ((String) value).trim();
Time result = null;
try {
result = Time.valueOf((String) value);
} catch (Exception e) {
if (!validate && pattern.matcher((String) value).matches()) {
throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
}
throw new TransformationException(CorePlugin.Event.TEIID10068, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10068, value));
}
if (validate && !result.toString().equals(value)) {
throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
}
return result;
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class StringToTimestampTransform method transformDirect.
/**
* This method transforms a value of the source type into a value
* of the target type.
* @param value Incoming value of source type
* @return Outgoing value of target type
* @throws TransformationException if value is an incorrect input type or
* the transformation fails
*/
public Object transformDirect(Object value) throws TransformationException {
String val = ((String) value).trim();
Timestamp result = null;
try {
result = Timestamp.valueOf(val);
} catch (Exception e) {
if (!validate && pattern.matcher(val).matches()) {
throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
}
throw new TransformationException(CorePlugin.Event.TEIID10059, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10059, value));
}
// validate everything except for fractional seconds
String substring = result.toString().substring(0, 19);
if (!val.startsWith(substring)) {
TimeZone tz = TimeZone.getDefault();
if (tz.useDaylightTime()) {
// check for a transition with a more costly SimpleDateFormat using a non-DST timezone
// $NON-NLS-1$
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setLenient(false);
// $NON-NLS-1$
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
try {
sdf.parse(val.substring(0, 19));
return result;
} catch (ParseException e) {
// let the exception happen
}
}
throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
}
return result;
}
use of org.teiid.core.types.TransformationException in project teiid by teiid.
the class ObjectToAnyTransform method transform.
@Override
public Object transform(Object value, Class<?> targetType) throws TransformationException {
if (value == null) {
return null;
}
if (targetType.isAssignableFrom(value.getClass())) {
return value;
}
Transform transform = DataTypeManager.getTransform(value.getClass(), targetType);
boolean valid = true;
if (transform instanceof ObjectToAnyTransform) {
Object v1 = DataTypeManager.convertToRuntimeType(value, true);
if (v1 != value) {
try {
return transform(v1, targetType);
} catch (TransformationException e) {
throw new TransformationException(CorePlugin.Event.TEIID10076, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, getSourceType(), targetClass, value));
}
}
if (targetType.isArray()) {
if (value instanceof Array) {
try {
// TODO: need to use the base type information for non-ArrayImpl values
Object array = ((Array) value).getArray();
if (targetType.isAssignableFrom(array.getClass())) {
if (!(value instanceof ArrayImpl)) {
return new ArrayImpl((Object[]) array);
}
return value;
}
value = array;
} catch (SQLException e) {
throw new TransformationException(e);
}
}
if (value.getClass().isArray()) {
if (value.getClass().getComponentType().isPrimitive() && targetType.getComponentType().isAssignableFrom(convertPrimitiveToObject(value.getClass().getComponentType()))) {
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(targetType.getComponentType(), java.lang.reflect.Array.getLength(value));
for (int i = 0; i < result.length; i++) {
result[i] = java.lang.reflect.Array.get(value, i);
}
return new ArrayImpl(result);
}
Class<?> targetComponentType = targetType.getComponentType();
Object[] result = (Object[]) java.lang.reflect.Array.newInstance(targetComponentType, java.lang.reflect.Array.getLength(value));
for (int i = 0; i < result.length; i++) {
Object v = java.lang.reflect.Array.get(value, i);
if (v.getClass() == targetComponentType || DefaultDataClasses.OBJECT == targetComponentType) {
result[i] = v;
} else {
Transform subTransform = DataTypeManager.getTransform(v.getClass(), targetComponentType);
if (subTransform == null) {
valid = false;
break;
}
result[i] = subTransform.transform(v, targetComponentType);
}
}
if (valid) {
return new ArrayImpl(result);
}
}
}
valid = false;
}
if (transform == null || !valid) {
Object[] params = new Object[] { getSourceType(), targetType, value };
throw new TransformationException(CorePlugin.Event.TEIID10076, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, params));
}
try {
return transform.transform(value, targetType);
} catch (TransformationException e) {
Object[] params = new Object[] { getSourceType(), targetType, value };
throw new TransformationException(CorePlugin.Event.TEIID10076, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10076, params));
}
}
Aggregations