Search in sources :

Example 1 with ClobInputStreamFactory

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

the class FunctionMethods method toBytes.

@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_bytes")
public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException {
    Charset cs = getCharset(encoding);
    ClobInputStreamFactory cisf = new ClobInputStreamFactory(value.getReference());
    cisf.setCharset(cs);
    if (!wellFormed || CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) || CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
        // validate that the binary conversion is possible
        // TODO: cache the result in a filestore
        InputStream is = new ReaderInputStream(value.getCharacterStream(), cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT));
        try {
            while (is.read() != -1) {
            }
        } catch (IOException e) {
            CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
            if (cce != null) {
                throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, cs.displayName()), cce);
            }
            throw e;
        } finally {
            is.close();
        }
    }
    return new BlobType(new BlobImpl(cisf));
}
Also used : ReaderInputStream(org.teiid.core.util.ReaderInputStream) BlobType(org.teiid.core.types.BlobType) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) BlobImpl(org.teiid.core.types.BlobImpl)

Example 2 with ClobInputStreamFactory

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

Example 3 with ClobInputStreamFactory

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

the class TestODataUpdateExecution method helpExecute.

private UpdateExecution helpExecute(MetadataFactory mf, String query, String expectedPayload, final String resultJson, String expectedURL, String expectedMethod, int responseCode) throws Exception {
    ODataExecutionFactory translator = new ODataExecutionFactory();
    translator.start();
    TranslationUtility utility = new TranslationUtility(TestODataMetadataProcessor.getTransformationMetadata(mf, translator));
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    WSConnection connection = Mockito.mock(WSConnection.class);
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
    headers.put(WSConnection.STATUS_CODE, new Integer(responseCode));
    Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
    Mockito.stub(dispatch.getRequestContext()).toReturn(headers);
    Mockito.stub(dispatch.getResponseContext()).toReturn(headers);
    Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
    DataSource ds = new DataSource() {

        @Override
        public OutputStream getOutputStream() throws IOException {
            return new ByteArrayOutputStream();
        }

        @Override
        public String getName() {
            return "result";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            ByteArrayInputStream in = new ByteArrayInputStream(resultJson.getBytes());
            return in;
        }

        @Override
        public String getContentType() {
            return "application/json";
        }
    };
    ArgumentCaptor<DataSource> data = ArgumentCaptor.forClass(DataSource.class);
    Mockito.stub(dispatch.invoke(data.capture())).toReturn(ds);
    UpdateExecution execution = translator.createUpdateExecution(cmd, context, utility.createRuntimeMetadata(), connection);
    execution.execute();
    ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
    Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
    assertEquals(expectedURL, URLDecoder.decode(endpoint.getValue(), "utf-8"));
    String payload = new String(ObjectConverterUtil.convertToByteArray(((ClobInputStreamFactory) data.getValue()).getInputStream()));
    assertEquals(expectedPayload, payload);
    assertEquals(expectedMethod, dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_METHOD));
    return execution;
}
Also used : WSConnection(org.teiid.translator.WSConnection) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) HashMap(java.util.HashMap) TranslationUtility(org.teiid.cdk.api.TranslationUtility) UpdateExecution(org.teiid.translator.UpdateExecution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List)

Aggregations

ClobInputStreamFactory (org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 BlobImpl (org.teiid.core.types.BlobImpl)2 BlobType (org.teiid.core.types.BlobType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Charset (java.nio.charset.Charset)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DataSource (javax.activation.DataSource)1 SerialBlob (javax.sql.rowset.serial.SerialBlob)1 TranslationUtility (org.teiid.cdk.api.TranslationUtility)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 ClobImpl (org.teiid.core.types.ClobImpl)1