Search in sources :

Example 1 with BlobInputStreamFactory

use of org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory 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 BlobInputStreamFactory

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

use of org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory in project teiid by teiid.

the class LobManager method persistLob.

public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
    long byteLength = Integer.MAX_VALUE;
    try {
        byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
    } catch (SQLException e) {
    // just ignore for now - for a single read resource computing the length invalidates
    // TODO - inline small persisted lobs
    }
    try {
        // inline
        if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
            lob.setReferenceStreamId(null);
            if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
                return;
            }
            if (lob instanceof BlobType) {
                BlobType b = (BlobType) lob;
                byte[] blobBytes = b.getBytes(1, (int) byteLength);
                b.setReference(new SerialBlob(blobBytes));
            } else if (lob instanceof ClobType) {
                ClobType c = (ClobType) lob;
                // $NON-NLS-1$
                String s = "";
                // some clob impls return null for 0 length
                if (byteLength != 0) {
                    s = c.getSubString(1, (int) (byteLength >>> 1));
                }
                c.setReference(new ClobImpl(s));
            } else {
                XMLType x = (XMLType) lob;
                String s = x.getString();
                x.setReference(new SQLXMLImpl(s));
            }
            return;
        }
        InputStream is = null;
        if (lob instanceof BlobType) {
            is = new BlobInputStreamFactory((Blob) lob).getInputStream();
        } else if (lob instanceof ClobType) {
            is = new ClobInputStreamFactory((Clob) lob).getInputStream();
        } else {
            is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
        }
        long offset = store.getLength();
        OutputStream fsos = store.createOutputStream();
        byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
        // re-construct the new lobs based on the file store
        final long lobOffset = offset;
        final long lobLength = byteLength;
        /*
			 * Using an inner class here will hold a reference to the LobManager
			 * which prevents the removal of the FileStore until all of the
			 * lobs have been gc'd
			 */
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return store.createInputStream(lobOffset, lobLength);
            }

            @Override
            public StorageMode getStorageMode() {
                return StorageMode.PERSISTENT;
            }
        };
        isf.setLength(byteLength);
        if (lob instanceof BlobType) {
            ((BlobType) lob).setReference(new BlobImpl(isf));
        } else if (lob instanceof ClobType) {
            long length = -1;
            try {
                length = ((ClobType) lob).length();
            } catch (SQLException e) {
            // could be streaming
            }
            ((ClobType) lob).setReference(new ClobImpl(isf, length));
        } else {
            ((XMLType) lob).setReference(new SQLXMLImpl(isf));
        }
    } catch (SQLException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
    } catch (IOException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
    }
}
Also used : SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) SQLXML(java.sql.SQLXML) TeiidComponentException(org.teiid.core.TeiidComponentException) Clob(java.sql.Clob) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

Aggregations

ClobImpl (org.teiid.core.types.ClobImpl)3 ClobType (org.teiid.core.types.ClobType)3 BlobInputStreamFactory (org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory)3 IOException (java.io.IOException)2 BlobType (org.teiid.core.types.BlobType)2 InputStreamFactory (org.teiid.core.types.InputStreamFactory)2 FilterReader (java.io.FilterReader)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Charset (java.nio.charset.Charset)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1