Search in sources :

Example 1 with ClobImpl

use of org.teiid.core.types.ClobImpl in project teiid by teiid.

the class S3ProcedureExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    if (this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.SAVEFILE) || this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.DELETEFILE)) {
        return null;
    }
    if (this.execution == null || this.execution.getResponseCode() < 200 || this.execution.getResponseCode() > 300) {
        return null;
    }
    Blob contents = (Blob) execution.getOutputParameterValues().get(0);
    BlobInputStreamFactory isf = new BlobInputStreamFactory(contents);
    String length = getHeader("Content-Length");
    if (length != null) {
        isf.setLength(Long.parseLong(length));
    }
    Object value = null;
    if (isText) {
        ClobImpl clob = new ClobImpl(isf, -1);
        clob.setCharset(Charset.forName(this.ef.getEncoding()));
        value = new ClobType(clob);
        if (!streaming) {
            value = new InputStreamFactory.ClobInputStreamFactory(clob);
        }
    } else {
        if (streaming) {
            value = new BlobType(contents);
        } else {
            value = isf;
        }
    }
    String lastModified = getHeader("Last-Modified");
    ArrayList<Object> result = new ArrayList<Object>(2);
    result.add(value);
    if (!isList) {
        result.add(endpoint);
        try {
            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzz");
            result.add(lastModified == null ? null : new Timestamp(df.parse(lastModified).getTime()));
        } catch (ParseException e) {
            result.add(null);
        }
        result.add(getHeader("ETag"));
        result.add(length);
    }
    this.execution = null;
    return result;
}
Also used : Blob(java.sql.Blob) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) Timestamp(java.sql.Timestamp) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) BlobType(org.teiid.core.types.BlobType) ParseException(java.text.ParseException) ClobImpl(org.teiid.core.types.ClobImpl) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ClobImpl

use of org.teiid.core.types.ClobImpl 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 3 with ClobImpl

use of org.teiid.core.types.ClobImpl in project teiid by teiid.

the class FunctionMethods method toChars.

@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_chars")
public static ClobType toChars(BlobType value, String encoding, boolean wellFormed) throws SQLException, IOException {
    Charset cs = getCharset(encoding);
    BlobInputStreamFactory bisf = new BlobInputStreamFactory(value.getReference());
    ClobImpl clob = new ClobImpl(bisf, -1);
    clob.setCharset(cs);
    if (!wellFormed && !CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) && !CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
        // validate that the charcter conversion is possible
        // TODO: cache the result in a filestore
        Reader r = clob.getCharacterStream();
        try {
            while (r.read() != -1) {
            }
        } catch (IOException e) {
            CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
            if (cce != null) {
                throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10082, cs.displayName()), cce);
            }
            throw e;
        } finally {
            r.close();
        }
    }
    return new ClobType(clob);
}
Also used : ClobType(org.teiid.core.types.ClobType) Charset(java.nio.charset.Charset) FilterReader(java.io.FilterReader) Reader(java.io.Reader) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ClobImpl(org.teiid.core.types.ClobImpl) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory)

Example 4 with ClobImpl

use of org.teiid.core.types.ClobImpl in project teiid by teiid.

the class GeometryUtils method geometryToClob.

public static ClobType geometryToClob(GeometryType geometry, boolean withSrid) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    int srid = jtsGeometry.getSRID();
    StringBuilder geomText = new StringBuilder();
    if (withSrid && srid != GeometryType.UNKNOWN_SRID) {
        // $NON-NLS-1$ //$NON-NLS-2$
        geomText.append("SRID=").append(jtsGeometry.getSRID()).append(";");
    }
    geomText.append(jtsGeometry.toText());
    return new ClobType(new ClobImpl(geomText.toString()));
}
Also used : ClobType(org.teiid.core.types.ClobType) ClobImpl(org.teiid.core.types.ClobImpl)

Example 5 with ClobImpl

use of org.teiid.core.types.ClobImpl in project teiid by teiid.

the class GeometryUtils method geometryToGeoJson.

public static ClobType geometryToGeoJson(GeometryType geometry) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    GeoJSONWriter writer = new GeoJSONWriter();
    try {
        GeoJSON geoJson = writer.write(jtsGeometry);
        ClobType result = new ClobType(new ClobImpl(geoJson.toString()));
        result.setType(Type.JSON);
        return result;
    } catch (Exception e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GeoJSON(org.wololo.geojson.GeoJSON) GeoJSONWriter(org.wololo.jts2geojson.GeoJSONWriter) ClobImpl(org.teiid.core.types.ClobImpl) SQLException(java.sql.SQLException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException)

Aggregations

ClobImpl (org.teiid.core.types.ClobImpl)28 ClobType (org.teiid.core.types.ClobType)21 InputStreamFactory (org.teiid.core.types.InputStreamFactory)14 IOException (java.io.IOException)10 Test (org.junit.Test)10 BlobImpl (org.teiid.core.types.BlobImpl)8 SQLException (java.sql.SQLException)7 BlobType (org.teiid.core.types.BlobType)6 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)6 Blob (java.sql.Blob)5 List (java.util.List)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Reader (java.io.Reader)3 BigInteger (java.math.BigInteger)3 Clob (java.sql.Clob)3 SQLXML (java.sql.SQLXML)3 HashMap (java.util.HashMap)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3