Search in sources :

Example 1 with TransformationException

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);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Timestamp(java.sql.Timestamp) GeometryType(org.teiid.core.types.GeometryType) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.sql.Date) BigInteger(java.math.BigInteger) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with TransformationException

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);
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) BlobType(org.teiid.core.types.BlobType) TransformationException(org.teiid.core.types.TransformationException) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Clob(java.sql.Clob)

Example 3 with TransformationException

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;
}
Also used : TransformationException(org.teiid.core.types.TransformationException) Time(java.sql.Time) TransformationException(org.teiid.core.types.TransformationException)

Example 4 with TransformationException

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;
}
Also used : TransformationException(org.teiid.core.types.TransformationException) TimeZone(java.util.TimeZone) ParseException(java.text.ParseException) Timestamp(java.sql.Timestamp) SimpleDateFormat(java.text.SimpleDateFormat) TransformationException(org.teiid.core.types.TransformationException) ParseException(java.text.ParseException)

Example 5 with TransformationException

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));
    }
}
Also used : Array(java.sql.Array) TransformationException(org.teiid.core.types.TransformationException) SQLException(java.sql.SQLException) ArrayImpl(org.teiid.core.types.ArrayImpl) Transform(org.teiid.core.types.Transform)

Aggregations

TransformationException (org.teiid.core.types.TransformationException)22 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 XMLType (org.teiid.core.types.XMLType)4 TranslatorException (org.teiid.translator.TranslatorException)4 InputStream (java.io.InputStream)3 Blob (java.sql.Blob)3 Date (java.sql.Date)3 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)3 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)3 BinaryType (org.teiid.core.types.BinaryType)3 Transform (org.teiid.core.types.Transform)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BigInteger (java.math.BigInteger)2 SQLXML (java.sql.SQLXML)2 BlobImpl (org.teiid.core.types.BlobImpl)2 BlobType (org.teiid.core.types.BlobType)2 ClobImpl (org.teiid.core.types.ClobImpl)2