Search in sources :

Example 36 with FbExceptionBuilder

use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.

the class V13WireOperations method tryKnownServerKeys.

private void tryKnownServerKeys() throws IOException, SQLException {
    boolean initializedEncryption = false;
    SQLExceptionChainBuilder<SQLException> chainBuilder = new SQLExceptionChainBuilder<>();
    for (KnownServerKey.PluginSpecificData pluginSpecificData : getPluginSpecificData()) {
        EncryptionIdentifier encryptionIdentifier = pluginSpecificData.getEncryptionIdentifier();
        EncryptionPluginSpi currentEncryptionSpi = SUPPORTED_ENCRYPTION_PLUGINS.get(encryptionIdentifier);
        if (currentEncryptionSpi == null) {
            continue;
        }
        try (CryptSessionConfig cryptSessionConfig = getCryptSessionConfig(encryptionIdentifier, pluginSpecificData.getSpecificData())) {
            EncryptionPlugin encryptionPlugin = currentEncryptionSpi.createEncryptionPlugin(cryptSessionConfig);
            EncryptionInitInfo encryptionInitInfo = encryptionPlugin.initializeEncryption();
            if (encryptionInitInfo.isSuccess()) {
                enableEncryption(encryptionInitInfo);
                clearServerKeys();
                initializedEncryption = true;
                log.debug("Wire encryption established with " + encryptionIdentifier);
                break;
            } else {
                chainBuilder.append(encryptionInitInfo.getException());
            }
        } catch (SQLException e) {
            chainBuilder.append(e);
        }
    }
    if (!initializedEncryption && getAttachProperties().getWireCryptAsEnum() == WireCrypt.REQUIRED) {
        FbExceptionBuilder exceptionBuilder = new FbExceptionBuilder().nonTransientException(ISCConstants.isc_wirecrypt_incompatible);
        if (chainBuilder.hasException()) {
            exceptionBuilder.cause(chainBuilder.getException());
        }
        throw exceptionBuilder.toFlatSQLException();
    }
    if (chainBuilder.hasException()) {
        SQLException current = chainBuilder.getException();
        if (log.isWarnEnabled()) {
            log.warn(initializedEncryption ? "Wire encryption established, but some plugins failed; see other loglines for details" : "No wire encryption established because of errors");
            log.warn("Encryption plugin failed; see debug level for stacktraces:\n" + ExceptionHelper.collectAllMessages(current));
        }
        if (log.isDebugEnabled()) {
            do {
                log.debug("Encryption plugin failed", current);
            } while ((current = current.getNextException()) != null);
        }
    }
}
Also used : KnownServerKey(org.firebirdsql.gds.ng.wire.crypt.KnownServerKey) SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) EncryptionPlugin(org.firebirdsql.gds.ng.wire.crypt.EncryptionPlugin) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) EncryptionInitInfo(org.firebirdsql.gds.ng.wire.crypt.EncryptionInitInfo) EncryptionIdentifier(org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier) EncryptionPluginSpi(org.firebirdsql.gds.ng.wire.crypt.EncryptionPluginSpi) CryptSessionConfig(org.firebirdsql.gds.ng.wire.crypt.CryptSessionConfig)

Example 37 with FbExceptionBuilder

use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.

the class V10AsynchronousChannel method cancelEvent.

@Override
public void cancelEvent(EventHandle eventHandle) throws SQLException {
    if (!(eventHandle instanceof WireEventHandle))
        throw new SQLNonTransientException("Invalid event handle type: " + eventHandle.getClass().getName());
    final WireEventHandle wireEventHandle = (WireEventHandle) eventHandle;
    removeChannelListener(wireEventHandle);
    synchronized (database.getSynchronizationObject()) {
        try {
            final XdrOutputStream dbXdrOut = database.getXdrStreamAccess().getXdrOut();
            dbXdrOut.writeInt(op_cancel_events);
            dbXdrOut.writeInt(database.getHandle());
            dbXdrOut.writeInt(wireEventHandle.getLocalId());
            dbXdrOut.flush();
        } catch (IOException e) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
        }
        try {
            database.readGenericResponse(null);
        } catch (IOException e) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 38 with FbExceptionBuilder

use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.

the class V10AsynchronousChannel method queueEvent.

@Override
public void queueEvent(EventHandle eventHandle) throws SQLException {
    if (!(eventHandle instanceof WireEventHandle))
        throw new SQLNonTransientException("Invalid event handle type: " + eventHandle.getClass().getName());
    final WireEventHandle wireEventHandle = (WireEventHandle) eventHandle;
    wireEventHandle.assignNewLocalId();
    addChannelListener(wireEventHandle);
    synchronized (database.getSynchronizationObject()) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Queue event: " + wireEventHandle);
            }
            final XdrOutputStream dbXdrOut = database.getXdrStreamAccess().getXdrOut();
            dbXdrOut.writeInt(op_que_events);
            dbXdrOut.writeInt(auxHandle);
            dbXdrOut.writeBuffer(wireEventHandle.toByteArray());
            // AST info
            dbXdrOut.writeLong(0);
            dbXdrOut.writeInt(wireEventHandle.getLocalId());
            dbXdrOut.flush();
        } catch (IOException e) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
        }
        try {
            final GenericResponse response = database.readGenericResponse(null);
            wireEventHandle.setEventId(response.getObjectHandle());
        } catch (IOException e) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 39 with FbExceptionBuilder

use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.

the class V10AsynchronousChannel method connect.

@Override
public void connect(String hostName, int portNumber, int auxHandle) throws SQLException {
    if (isConnected())
        throw new SQLException("Asynchronous channel already established");
    this.auxHandle = auxHandle;
    try {
        socketChannel = SocketChannel.open();
        socketChannel.socket().setTcpNoDelay(true);
        SocketAddress socketAddress = new InetSocketAddress(hostName, portNumber);
        socketChannel.connect(socketAddress);
        socketChannel.configureBlocking(false);
    } catch (IOException ex) {
        throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
    }
}
Also used : SQLException(java.sql.SQLException) InetSocketAddress(java.net.InetSocketAddress) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 40 with FbExceptionBuilder

use of org.firebirdsql.gds.ng.FbExceptionBuilder in project jaybird by FirebirdSQL.

the class V10InputBlob method getSegment.

@Override
public byte[] getSegment(final int sizeRequested) throws SQLException {
    try {
        if (sizeRequested <= 0) {
            throw new FbExceptionBuilder().exception(jb_blobGetSegmentNegative).messageParameter(sizeRequested).toSQLException();
        }
        // TODO Is this actually a real limitation, or are larger sizes possible?
        int actualSize = 2 + Math.min(sizeRequested, getMaximumSegmentSize());
        synchronized (getSynchronizationObject()) {
            checkDatabaseAttached();
            checkTransactionActive();
            checkBlobOpen();
            final GenericResponse response;
            final FbWireDatabase database = getDatabase();
            try {
                final XdrOutputStream xdrOut = database.getXdrStreamAccess().getXdrOut();
                xdrOut.writeInt(op_get_segment);
                xdrOut.writeInt(getHandle());
                xdrOut.writeInt(actualSize);
                // length of segment send buffer (always 0 in get)
                xdrOut.writeInt(0);
                xdrOut.flush();
            } catch (IOException e) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
            }
            try {
                response = database.readGenericResponse(null);
                // TODO Meaning of 2
                if (response.getObjectHandle() == 2) {
                    // TODO what if I seek on a stream blob?
                    setEof();
                }
            } catch (IOException e) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
            }
            final byte[] responseBuffer = response.getData();
            if (responseBuffer.length == 0) {
                return responseBuffer;
            }
            final ByteArrayOutputStream bos = new ByteArrayOutputStream(actualSize);
            int position = 0;
            while (position < responseBuffer.length) {
                final int segmentLength = iscVaxInteger2(responseBuffer, position);
                position += 2;
                bos.write(responseBuffer, position, segmentLength);
                position += segmentLength;
            }
            return bos.toByteArray();
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)51 SQLException (java.sql.SQLException)31 IOException (java.io.IOException)27 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)22 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)6 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)6 SQLNonTransientException (java.sql.SQLNonTransientException)4 AbstractWireOperations (org.firebirdsql.gds.ng.wire.AbstractWireOperations)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)3 BigInteger (java.math.BigInteger)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 SQLWarning (java.sql.SQLWarning)2 Cipher (javax.crypto.Cipher)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 Encoding (org.firebirdsql.encodings.Encoding)2