Search in sources :

Example 11 with ReaderInputStream

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

the class IntegrationTestRestWebserviceGeneration method testGetOperation.

@Test
public void testGetOperation() throws Exception {
    Properties p = new Properties();
    p.setProperty("class-name", "org.teiid.resource.adapter.ws.WSManagedConnectionFactory");
    p.setProperty("EndPoint", "http://localhost:8080");
    p.setProperty("RequestTimeout", "20000");
    AdminUtil.createDataSource(admin, "sample-ws", "webservice", p);
    deployVDB();
    assertTrue(AdminUtil.waitForVDBLoad(admin, "sample", 1, 3));
    admin.deploy("sample-ws-vdb.xml", new ReaderInputStream(new StringReader(// simple ws vdb
    "<vdb name=\"sample-ws\" version=\"1\">" + "<model name=\"ws\"><source name=\"ws\" translator-name=\"ws\" connection-jndi-name=\"java:/sample-ws\"/></model>" + "</vdb>"), Charset.forName("UTF-8")));
    assertTrue(AdminUtil.waitForVDBLoad(admin, "sample-ws", 1, 3));
    this.internalConnection = TeiidDriver.getInstance().connect("jdbc:teiid:sample@mm://localhost:31000;user=user;password=user", null);
    // $NON-NLS-1$
    execute("SELECT * FROM Txns.G1");
    this.internalResultSet.next();
    assertTrue("sample_1.war not found", AdminUtil.waitForDeployment(admin, "sample_1.war", 5));
    // get based call
    String response = httpCall("http://localhost:8080/sample_1/View/g1/123?p2=test", "GET", null);
    String expected = "<rows p1=\"123\" p2=\"test\"><row><e1>ABCDEFGHIJ</e1><e2>0</e2></row></rows>";
    assertEquals("response did not match expected", expected, response);
    this.internalConnection.close();
    // try the same thing through a vdb
    this.internalConnection = TeiidDriver.getInstance().connect("jdbc:teiid:sample-ws@mm://localhost:31000;user=user;password=user", null);
    execute("select to_chars(x.result, 'UTF-8') from (call invokeHttp(action=>'GET',endpoint=>'sample_1/View/g1/123?p2=test')) as x");
    this.internalResultSet.next();
    assertEquals(expected, this.internalResultSet.getString(1));
    // test a large doc
    response = httpCall("http://localhost:8080/sample_1/View/largedoc", "GET", null);
    assertEquals(327801, response.length());
    // test streaming xmltable
    execute("select * from xmltable('/rows/row/e1' passing xmlparse(document (select result from (call invokeHttp(action=>'GET',endpoint=>'sample_1/View/g1/123?p2=test')) as d))) as x");
    this.internalResultSet.next();
    assertEquals("<e1>ABCDEFGHIJ</e1>", this.internalResultSet.getString(1));
    admin.deploy("sample2-vdb.xml", new FileInputStream(UnitTestUtil.getTestDataFile("sample2-vdb.xml")));
    assertTrue(AdminUtil.waitForVDBLoad(admin, "sample2", 1, 3));
    // test swagger
    response = httpCall("http://localhost:8080/sample_1/swagger.yaml", "GET", null);
    // wait for the war to come up
    Thread.sleep(2000);
    String response1 = httpCall("http://localhost:8080/sample2_1/swagger.yaml", "GET", null);
    assertNotEquals(response, response1);
    admin.undeploy("sample-vdb.xml");
    Thread.sleep(2000);
}
Also used : ReaderInputStream(org.teiid.core.util.ReaderInputStream) StringReader(java.io.StringReader) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 12 with ReaderInputStream

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

the class FileExecutionFactory method createProcedureExecution.

// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
    if (conn instanceof VirtualFileConnection) {
        return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
    }
    final FileConnection fc = (FileConnection) conn;
    if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                Object file = command.getArguments().get(1).getArgumentValue().getValue();
                if (file == null || filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
                InputStream is = null;
                try {
                    if (file instanceof SQLXML) {
                        is = ((SQLXML) file).getBinaryStream();
                    } else if (file instanceof Clob) {
                        is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
                    } else if (file instanceof Blob) {
                        is = ((Blob) file).getBinaryStream();
                    } else {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("unknown_type"));
                    }
                    ObjectConverterUtil.write(is, fc.getFile(filePath));
                } catch (IOException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (SQLException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    } else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                if (filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
                try {
                    File f = fc.getFile(filePath);
                    if (!f.exists()) {
                        if (exceptionIfFileNotFound) {
                            // $NON-NLS-1$
                            throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
                        }
                    } else if (!f.delete()) {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("error_deleting"));
                    }
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_deleting"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    }
    return new FileProcedureExecution(command, fc);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Clob(java.sql.Clob) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) VirtualFileConnection(org.teiid.file.VirtualFileConnection) FileConnection(org.teiid.translator.FileConnection)

Example 13 with ReaderInputStream

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

the class ConnectorWorkItem method convertToRuntimeType.

static Object convertToRuntimeType(BufferManager bm, Object value, Class<?> desiredType, CommandContext context) throws TransformationException {
    if (desiredType != DataTypeManager.DefaultDataClasses.XML || !(value instanceof Source)) {
        if (value instanceof DataSource) {
            final DataSource ds = (DataSource) value;
            try {
                // Teiid uses the datasource interface in a degenerate way that
                // reuses the stream, so we test for that here
                InputStream initial = ds.getInputStream();
                InputStream other = null;
                try {
                    other = ds.getInputStream();
                } catch (IOException e) {
                // likely streaming
                }
                if (other != null && initial != other) {
                    initial.close();
                    other.close();
                    if (value instanceof InputStreamFactory) {
                        return asLob((InputStreamFactory) value, desiredType);
                    }
                    return asLob(new InputStreamFactory() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return ds.getInputStream();
                        }
                    }, desiredType);
                }
                // $NON-NLS-1$
                FileStore fs = bm.createFileStore("bytes");
                // TODO: guess at the encoding from the content type
                FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
                return asLob(is.getInputStreamFactory(), desiredType);
            } catch (IOException e) {
                throw new TransformationException(QueryPlugin.Event.TEIID30500, e, e.getMessage());
            }
        }
        if (value instanceof InputStreamFactory) {
            return asLob((InputStreamFactory) value, desiredType);
        }
        if (value instanceof GeometryInputSource) {
            GeometryInputSource gis = (GeometryInputSource) value;
            try {
                InputStream is = gis.getEwkb();
                if (is != null) {
                    return GeometryUtils.geometryFromEwkb(is, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
            try {
                Reader r = gis.getGml();
                if (r != null) {
                    return GeometryUtils.geometryFromGml(r, gis.getSrid());
                }
            } catch (Exception e) {
                throw new TransformationException(e);
            }
        }
    }
    if (value instanceof Source) {
        if (!(value instanceof InputStreamFactory)) {
            if (value instanceof StreamSource) {
                StreamSource ss = (StreamSource) value;
                InputStream is = ss.getInputStream();
                Reader r = ss.getReader();
                if (is == null && r != null) {
                    is = new ReaderInputStream(r, Streamable.CHARSET);
                }
                // $NON-NLS-1$
                final FileStore fs = bm.createFileStore("xml");
                final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                value = new SaveOnReadInputStream(is, fsisf).getInputStreamFactory();
                if (context != null) {
                    context.addCreatedLob(fsisf);
                }
            } else if (value instanceof StAXSource) {
                // TODO: do this lazily.  if the first access to get the STaXSource, then
                // it's more efficient to let the processing happen against STaX
                StAXSource ss = (StAXSource) value;
                try {
                    // $NON-NLS-1$
                    final FileStore fs = bm.createFileStore("xml");
                    final FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
                    value = new SaveOnReadInputStream(new XMLInputStream(ss, XMLSystemFunctions.getOutputFactory(true)), fsisf).getInputStreamFactory();
                    if (context != null) {
                        context.addCreatedLob(fsisf);
                    }
                } catch (XMLStreamException e) {
                    throw new TransformationException(e);
                }
            } else {
                // maybe dom or some other source we want to get out of memory
                StandardXMLTranslator sxt = new StandardXMLTranslator((Source) value);
                SQLXMLImpl sqlxml;
                try {
                    sqlxml = XMLSystemFunctions.saveToBufferManager(bm, sxt, context);
                } catch (TeiidComponentException e) {
                    throw new TransformationException(e);
                } catch (TeiidProcessingException e) {
                    throw new TransformationException(e);
                }
                return new XMLType(sqlxml);
            }
        }
        return new XMLType(new SQLXMLImpl((InputStreamFactory) value));
    }
    return DataTypeManager.convertToRuntimeType(value, desiredType != DataTypeManager.DefaultDataClasses.OBJECT);
}
Also used : SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) XMLInputStream(org.teiid.util.XMLInputStream) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLInputStream(org.teiid.util.XMLInputStream) SaveOnReadInputStream(org.teiid.dqp.internal.process.SaveOnReadInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) GeometryInputSource(org.teiid.GeometryInputSource) Reader(java.io.Reader) IOException(java.io.IOException) StAXSource(javax.xml.transform.stax.StAXSource) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) GeometryInputSource(org.teiid.GeometryInputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) DataSource(javax.activation.DataSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) ResourceException(javax.resource.ResourceException) TeiidComponentException(org.teiid.core.TeiidComponentException) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException) IOException(java.io.IOException) DataSource(javax.activation.DataSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) FileStore(org.teiid.common.buffer.FileStore) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 14 with ReaderInputStream

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

the class TeiidRSProvider method handleResult.

private InputStream handleResult(String charSet, Object result) throws SQLException {
    if (result == null) {
        // or should this be an empty result?
        return null;
    }
    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 {
                return ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
            } catch (TransformationException e) {
                throw new SQLException(e);
            }
        }
        return ((SQLXML) result).getBinaryStream();
    } else if (result instanceof Blob) {
        return ((Blob) result).getBinaryStream();
    } else if (result instanceof Clob) {
        return new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
    }
    return new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
}
Also used : SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) Clob(java.sql.Clob)

Example 15 with ReaderInputStream

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

the class LobWorkItem method createLobStream.

/**
 * Create a object which can create a sequence of LobChunk objects on a given
 * LOB object
 * @throws SQLException
 */
private ByteLobChunkStream createLobStream(String referenceStreamId) throws TeiidComponentException, SQLException {
    // get the reference object in the buffer manager, and try to stream off
    // the original sources.
    Streamable<?> streamable = parent.resultsBuffer.getLobReference(referenceStreamId);
    if (streamable instanceof XMLType) {
        XMLType xml = (XMLType) streamable;
        return new ByteLobChunkStream(xml.getBinaryStream(), chunkSize);
    } else if (streamable instanceof ClobType) {
        ClobType clob = (ClobType) streamable;
        return new ByteLobChunkStream(new ReaderInputStream(clob.getCharacterStream(), Charset.forName(Streamable.ENCODING)), chunkSize);
    }
    BlobType blob = (BlobType) streamable;
    return new ByteLobChunkStream(blob.getBinaryStream(), chunkSize);
}
Also used : ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) ReaderInputStream(org.teiid.core.util.ReaderInputStream) BlobType(org.teiid.core.types.BlobType)

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