Search in sources :

Example 1 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class WebSphereMsSqlServerDbmsSupport method updateBlob.

public void updateBlob(ResultSet rs, String column, Object blobUpdateHandle) throws SQLException, JdbcException {
    File blobFile = (File) blobUpdateHandle;
    try {
        InputStream is = new FileInputStream(blobFile);
        rs.updateBinaryStream(column, is, (int) blobFile.length());
    } catch (FileNotFoundException e) {
        throw new JdbcException("cannot read blob for column [" + column + "] from file [" + blobFile.toString() + "]", e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) FileInputStream(java.io.FileInputStream)

Example 2 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method getBlobAsString.

public static String getBlobAsString(Blob blob, String column, String charset, boolean xmlEncode, boolean blobIsCompressed, boolean blobSmartGet, boolean encodeBlobBase64) throws IOException, JdbcException, SQLException, JMSException {
    if (encodeBlobBase64) {
        InputStream blobStream = JdbcUtil.getBlobInputStream(blob, column, blobIsCompressed);
        return Misc.streamToString(new Base64InputStream(blobStream, true), null, false);
    }
    if (blobSmartGet) {
        if (blob == null) {
            log.debug("no blob found in column [" + column + "]");
            return null;
        }
        int bl = (int) blob.length();
        InputStream is = blob.getBinaryStream();
        byte[] buf = new byte[bl];
        int bl1 = is.read(buf);
        Inflater decompressor = new Inflater();
        decompressor.setInput(buf);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);
        byte[] bufDecomp = new byte[1024];
        boolean decompresOK = true;
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(bufDecomp);
                if (count == 0) {
                    break;
                }
                bos.write(bufDecomp, 0, count);
            } catch (DataFormatException e) {
                log.debug("message in column [" + column + "] is not compressed");
                decompresOK = false;
                break;
            }
        }
        bos.close();
        if (decompresOK)
            buf = bos.toByteArray();
        Object result = null;
        ObjectInputStream ois = null;
        boolean objectOK = true;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(buf);
            ois = new ObjectInputStream(bis);
            result = ois.readObject();
        } catch (Exception e) {
            log.debug("message in column [" + column + "] is probably not a serialized object: " + e.getClass().getName());
            objectOK = false;
        }
        if (ois != null)
            ois.close();
        String rawMessage;
        if (objectOK) {
            if (result instanceof IMessageWrapper) {
                rawMessage = ((IMessageWrapper) result).getText();
            } else if (result instanceof TextMessage) {
                rawMessage = ((TextMessage) result).getText();
            } else {
                rawMessage = (String) result;
            }
        } else {
            rawMessage = new String(buf, charset);
        }
        String message = XmlUtils.encodeCdataString(rawMessage);
        return message;
    }
    return Misc.readerToString(getBlobReader(blob, column, charset, blobIsCompressed), null, xmlEncode);
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) IMessageWrapper(nl.nn.adapterframework.core.IMessageWrapper) DataFormatException(java.util.zip.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) TextMessage(javax.jms.TextMessage) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method streamBlob.

public static void streamBlob(Blob blob, String column, String charset, boolean blobIsCompressed, String blobBase64Direction, Object target, boolean close) throws JdbcException, SQLException, IOException {
    if (target == null) {
        throw new JdbcException("cannot stream Blob to null object");
    }
    OutputStream outputStream = StreamUtil.getOutputStream(target);
    if (outputStream != null) {
        InputStream inputStream = JdbcUtil.getBlobInputStream(blob, column, blobIsCompressed);
        if ("decode".equalsIgnoreCase(blobBase64Direction)) {
            Base64InputStream base64DecodedStream = new Base64InputStream(inputStream);
            StreamUtil.copyStream(base64DecodedStream, outputStream, 50000);
        } else if ("encode".equalsIgnoreCase(blobBase64Direction)) {
            Base64InputStream base64EncodedStream = new Base64InputStream(inputStream, true);
            StreamUtil.copyStream(base64EncodedStream, outputStream, 50000);
        } else {
            StreamUtil.copyStream(inputStream, outputStream, 50000);
        }
        if (close) {
            outputStream.close();
        }
        return;
    }
    Writer writer = StreamUtil.getWriter(target);
    if (writer != null) {
        Reader reader = JdbcUtil.getBlobReader(blob, column, charset, blobIsCompressed);
        StreamUtil.copyReaderToWriter(reader, writer, 50000, false, false);
        if (close) {
            writer.close();
        }
        return;
    }
    throw new IOException("cannot stream Blob to [" + target.getClass().getName() + "]");
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 4 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method executeBlobQuery.

public static String executeBlobQuery(Connection connection, String query) throws JdbcException {
    PreparedStatement stmt = null;
    try {
        if (log.isDebugEnabled())
            log.debug("prepare and execute query [" + query + "]");
        stmt = connection.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();
        try {
            if (!rs.next()) {
                return null;
            }
            return getBlobAsString(rs, 1, Misc.DEFAULT_INPUT_STREAM_ENCODING, false, true, true, false);
        } finally {
            rs.close();
        }
    } catch (Exception e) {
        throw new JdbcException("could not obtain value using query [" + query + "]", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement of query [" + query + "]", e);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 5 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method executeStringQuery.

/**
 * exectues query that returns a string. Returns null if no results are found.
 */
public static String executeStringQuery(Connection connection, String query) throws JdbcException {
    PreparedStatement stmt = null;
    try {
        if (log.isDebugEnabled())
            log.debug("prepare and execute query [" + query + "]");
        stmt = connection.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();
        try {
            if (!rs.next()) {
                return null;
            }
            return rs.getString(1);
        } finally {
            rs.close();
        }
    } catch (Exception e) {
        throw new JdbcException("could not obtain value using query [" + query + "]", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement of query [" + query + "]", e);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Aggregations

JdbcException (nl.nn.adapterframework.jdbc.JdbcException)26 SQLException (java.sql.SQLException)19 IOException (java.io.IOException)15 PreparedStatement (java.sql.PreparedStatement)13 ResultSet (java.sql.ResultSet)9 JMSException (javax.jms.JMSException)9 DataFormatException (java.util.zip.DataFormatException)8 Connection (java.sql.Connection)7 InputStream (java.io.InputStream)6 SenderException (nl.nn.adapterframework.core.SenderException)6 FileNotFoundException (java.io.FileNotFoundException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)4 InputStreamReader (java.io.InputStreamReader)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)3 JmsException (nl.nn.adapterframework.jms.JmsException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2