Search in sources :

Example 1 with ReaderInputStream

use of org.teiid.core.util.ReaderInputStream 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 ReaderInputStream

use of org.teiid.core.util.ReaderInputStream in project teiid by teiid.

the class XMLSystemFunctions method serialize.

public static Object serialize(XMLSerialize xs, XMLType value) throws TransformationException {
    Type type = value.getType();
    final Charset encoding;
    if (xs.getEncoding() != null) {
        encoding = Charset.forName(xs.getEncoding());
    } else {
        encoding = UTF_8;
    }
    if (Boolean.TRUE.equals(xs.getDeclaration())) {
        // need to replace existing/default declaration
        if (type == Type.ELEMENT || type == Type.DOCUMENT) {
            XMLEventFactory xmlEventFactory = threadLocalEventtFactory.get();
            xmlEventFactory.setLocation(dummyLocation);
            XMLEvent start = null;
            if (xs.getVersion() != null) {
                start = xmlEventFactory.createStartDocument(encoding.name(), xs.getVersion());
            } else {
                // use the encoding regardless as different stax impls have different default
                // behavior
                start = xmlEventFactory.createStartDocument(encoding.name());
            }
            StAXSourceProvider sourceProvider = new DeclarationStaxSourceProvider(start, value);
            value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
            value.setType(type);
        }
    // else just ignore, since the result is likely invalid
    } else if (type == Type.DOCUMENT && Boolean.FALSE.equals(xs.getDeclaration())) {
        final XMLType v = value;
        StAXSourceProvider sourceProvider = new StAXSourceProvider() {

            @Override
            public StAXSource getStaxSource() throws SQLException {
                try {
                    XMLEventReader eventReader = getXMLEventReader(v.getSource(StAXSource.class));
                    eventReader = XMLType.getXmlInputFactory().createFilteredReader(eventReader, declarationOmittingFilter);
                    return new StAXSource(eventReader);
                } catch (XMLStreamException e) {
                    throw new SQLException(e);
                }
            }
        };
        value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
        value.setType(Type.DOCUMENT);
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.STRING) {
        return DataTypeManager.transformValue(value, xs.getType());
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.CLOB) {
        InputStreamFactory isf = Evaluator.getInputStreamFactory(value);
        return new ClobType(new ClobImpl(isf, -1));
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.VARBINARY) {
        try {
            InputStream is = null;
            if (!Charset.forName(value.getEncoding()).equals(encoding)) {
                is = new ReaderInputStream(value.getCharacterStream(), encoding);
            } else {
                is = value.getBinaryStream();
            }
            byte[] bytes = ObjectConverterUtil.convertToByteArray(is, DataTypeManager.MAX_VARBINARY_BYTES);
            return new BinaryType(bytes);
        } catch (SQLException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        } catch (IOException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        }
    }
    InputStreamFactory isf = null;
    if (!Charset.forName(value.getEncoding()).equals(encoding)) {
        // create a wrapper for the input stream
        isf = new InputStreamFactory.SQLXMLInputStreamFactory(value) {

            public InputStream getInputStream() throws IOException {
                try {
                    return new ReaderInputStream(sqlxml.getCharacterStream(), encoding);
                } catch (SQLException e) {
                    throw new IOException(e);
                }
            }
        };
    } else {
        isf = Evaluator.getInputStreamFactory(value);
    }
    return new BlobType(new BlobImpl(isf));
}
Also used : SQLException(java.sql.SQLException) XMLEventFactory(javax.xml.stream.XMLEventFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSQLXML(org.teiid.util.StAXSQLXML) StAXSourceProvider(org.teiid.util.StAXSQLXML.StAXSourceProvider) ReaderInputStream(org.teiid.core.util.ReaderInputStream) Charset(java.nio.charset.Charset) StAXSource(javax.xml.transform.stax.StAXSource) Type(org.teiid.core.types.XMLType.Type) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 3 with ReaderInputStream

use of org.teiid.core.util.ReaderInputStream 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 4 with ReaderInputStream

use of org.teiid.core.util.ReaderInputStream in project teiid by teiid.

the class CompactObjectOutputStream method replaceObject.

@Override
protected Object replaceObject(Object obj) throws IOException {
    if (obj instanceof BaseLob) {
        try {
            if (obj instanceof SQLXMLImpl) {
                streams.add(((SQLXMLImpl) obj).getBinaryStream());
                StreamFactoryReference sfr = new SQLXMLImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof ClobImpl) {
                streams.add(new ReaderInputStream(((ClobImpl) obj).getCharacterStream(), Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new ClobImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof BlobImpl) {
                streams.add(((Blob) obj).getBinaryStream());
                StreamFactoryReference sfr = new BlobImpl();
                references.add(sfr);
                return sfr;
            }
        } catch (SQLException e) {
            throw new IOException(e);
        }
    } else if (obj instanceof Serializable) {
        return obj;
    } else {
        try {
            if (obj instanceof Reader) {
                streams.add(new ReaderInputStream((Reader) obj, Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new SerializableReader();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof InputStream) {
                streams.add((InputStream) obj);
                StreamFactoryReference sfr = new SerializableInputStream();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof SQLXML) {
                streams.add(((SQLXML) obj).getBinaryStream());
                StreamFactoryReference sfr = new SQLXMLImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof Clob) {
                streams.add(new ReaderInputStream(((Clob) obj).getCharacterStream(), Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new ClobImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof Blob) {
                streams.add(((Blob) obj).getBinaryStream());
                StreamFactoryReference sfr = new BlobImpl();
                references.add(sfr);
                return sfr;
            }
        } catch (SQLException e) {
            throw new IOException(e);
        }
    }
    return super.replaceObject(obj);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLXML(java.sql.SQLXML) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference) Clob(java.sql.Clob)

Example 5 with ReaderInputStream

use of org.teiid.core.util.ReaderInputStream in project teiid by teiid.

the class TestObjectDecoderInputStream method testReplaceObject.

@Test
public void testReplaceObject() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectEncoderOutputStream out = new ObjectEncoderOutputStream(new DataOutputStream(baos), 512);
    ClobImpl clob = new ClobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            // $NON-NLS-1$
            return new ReaderInputStream(new StringReader("Clob contents"), Charset.forName(Streamable.ENCODING));
        }
    }, -1);
    out.writeObject(clob);
    ObjectDecoderInputStream in = new ObjectDecoderInputStream(new AccessibleBufferedInputStream(new ByteArrayInputStream(baos.toByteArray()), 1024), Thread.currentThread().getContextClassLoader(), 1024);
    Object result = in.readObject();
    assertTrue(result instanceof ClobImpl);
}
Also used : DataOutputStream(java.io.DataOutputStream) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ClobImpl(org.teiid.core.types.ClobImpl) Test(org.junit.Test)

Aggregations

ReaderInputStream (org.teiid.core.util.ReaderInputStream)18 StringReader (java.io.StringReader)9 Test (org.junit.Test)9 InputStream (java.io.InputStream)5 SQLException (java.sql.SQLException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 Blob (java.sql.Blob)4 Clob (java.sql.Clob)4 SQLXML (java.sql.SQLXML)4 Response (javax.ws.rs.core.Response)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)4 BlobType (org.teiid.core.types.BlobType)4 ClobImpl (org.teiid.core.types.ClobImpl)3 ClobType (org.teiid.core.types.ClobType)3 InputStreamFactory (org.teiid.core.types.InputStreamFactory)3 Charset (java.nio.charset.Charset)2 Properties (java.util.Properties)2 ResourceException (javax.resource.ResourceException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2