Search in sources :

Example 6 with GenericResponse

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

the class TestV10WireOperations method testProcessResponse_exception.

/**
 * Test if processResponse throws the exception in the response if the
 * exception is not a warning.
 */
@Test
public void testProcessResponse_exception() throws Exception {
    AbstractWireOperations wire = createDummyWireOperations();
    SQLException exception = new FbExceptionBuilder().exception(ISCConstants.isc_numeric_out_of_range).toSQLException();
    expectedException.expect(sameInstance(exception));
    GenericResponse genericResponse = new GenericResponse(-1, -1, null, exception);
    wire.processResponse(genericResponse);
}
Also used : AbstractWireOperations(org.firebirdsql.gds.ng.wire.AbstractWireOperations) SQLException(java.sql.SQLException) GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) Test(org.junit.Test)

Example 7 with GenericResponse

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

the class TestV10WireOperations method testProcessResponse_warning.

/**
 * Test if processResponse does not throw an exception if the response
 * contains an exception that is warning.
 */
@Test
public void testProcessResponse_warning() throws Exception {
    AbstractWireOperations wire = createDummyWireOperations();
    SQLException exception = new FbExceptionBuilder().warning(ISCConstants.isc_numeric_out_of_range).toSQLException();
    GenericResponse genericResponse = new GenericResponse(-1, -1, null, exception);
    wire.processResponse(genericResponse);
}
Also used : AbstractWireOperations(org.firebirdsql.gds.ng.wire.AbstractWireOperations) SQLException(java.sql.SQLException) GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) Test(org.junit.Test)

Example 8 with GenericResponse

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

the class V13WireOperations method authReceiveResponse.

@Override
public void authReceiveResponse(FbWireAttachment.AcceptPacket acceptPacket, DbCryptCallback dbCryptCallback, FbWireOperations.ProcessAttachCallback processAttachCallback) throws SQLException, IOException {
    assert acceptPacket == null || acceptPacket.operation == op_cond_accept : "Unexpected operation in AcceptPacket";
    final XdrInputStream xdrIn = getXdrIn();
    final XdrOutputStream xdrOut = getXdrOut();
    final ClientAuthBlock clientAuthBlock = getClientAuthBlock();
    final Encoding encoding = getEncoding();
    while (true) {
        String pluginName;
        byte[] data;
        if (acceptPacket != null) {
            data = acceptPacket.p_acpt_data;
            pluginName = acceptPacket.p_acpt_plugin;
            addServerKeys(acceptPacket.p_acpt_keys);
            log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName != null ? pluginName.length() : null, pluginName));
            // TODO handle compression
            acceptPacket = null;
        } else {
            int operation = readNextOperation();
            switch(operation) {
                case op_trusted_auth:
                    // p_trau_data
                    xdrIn.readBuffer();
                    throw new FbExceptionBuilder().nonTransientConnectionException(JaybirdErrorCodes.jb_receiveTrustedAuth_NotSupported).toFlatSQLException();
                case op_cont_auth:
                    // p_data
                    data = xdrIn.readBuffer();
                    // p_name
                    pluginName = xdrIn.readString(encoding);
                    // p_list (ignore?)
                    xdrIn.readBuffer();
                    // p_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cont_auth data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    break;
                case op_crypt_key_callback:
                    log.debug("Handling db crypt callback using plugin " + dbCryptCallback.getDbCryptCallbackName());
                    handleCryptKeyCallback(dbCryptCallback);
                    continue;
                case op_cond_accept:
                    // Note this is the equivalent of handling the acceptPacket != null above
                    // p_acpt_version
                    xdrIn.readInt();
                    // p_acpt_architecture
                    xdrIn.readInt();
                    // p_acpt_type
                    xdrIn.readInt();
                    // p_acpt_data
                    data = xdrIn.readBuffer();
                    // p_acpt_plugin
                    pluginName = xdrIn.readString(encoding);
                    // p_acpt_authenticated
                    xdrIn.readInt();
                    // p_acpt_keys
                    addServerKeys(xdrIn.readBuffer());
                    log.debug(String.format("authReceiveResponse: cond_accept data=%d pluginName=%d '%s'", data.length, pluginName.length(), pluginName));
                    // TODO handle compression
                    break;
                case op_response:
                    GenericResponse response = (GenericResponse) readOperationResponse(operation, null);
                    boolean wasAuthComplete = clientAuthBlock.isAuthComplete();
                    clientAuthBlock.setAuthComplete(true);
                    processAttachCallback.processAttachResponse(response);
                    addServerKeys(response.getData());
                    WireCrypt wireCrypt = getAttachProperties().getWireCryptAsEnum();
                    if (!wasAuthComplete && wireCrypt != WireCrypt.DISABLED) {
                        tryKnownServerKeys();
                    }
                    return;
                default:
                    throw new SQLException(String.format("Unsupported operation code: %d", operation));
            }
        }
        if (pluginName != null && pluginName.length() > 0 && Objects.equals(pluginName, clientAuthBlock.getCurrentPluginName())) {
            pluginName = null;
        }
        if (pluginName != null && pluginName.length() > 0) {
            if (!clientAuthBlock.switchPlugin(pluginName)) {
                break;
            }
        }
        if (!clientAuthBlock.hasPlugin()) {
            break;
        }
        clientAuthBlock.setServerData(data);
        log.debug(String.format("receiveResponse: authenticate(%s)", clientAuthBlock.getCurrentPluginName()));
        clientAuthBlock.authenticate();
        xdrOut.writeInt(op_cont_auth);
        // TODO Move to ClientAuthBlock?
        // p_data
        xdrOut.writeBuffer(clientAuthBlock.getClientData());
        // p_name
        xdrOut.writeString(clientAuthBlock.getCurrentPluginName(), encoding);
        if (clientAuthBlock.isFirstTime()) {
            // p_list
            xdrOut.writeString(clientAuthBlock.getPluginNames(), encoding);
            clientAuthBlock.setFirstTime(false);
        } else {
            // p_list
            xdrOut.writeBuffer(null);
        }
        // p_keys
        xdrOut.writeBuffer(null);
        xdrOut.flush();
    }
    // If we have exited from the cycle, this mean auth failed
    throw new FbExceptionBuilder().exception(ISCConstants.isc_login).toFlatSQLException();
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Encoding(org.firebirdsql.encodings.Encoding) WireCrypt(org.firebirdsql.gds.ng.WireCrypt) ClientAuthBlock(org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)

Example 9 with GenericResponse

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

the class V10Transaction method getTransactionInfo.

@Override
public byte[] getTransactionInfo(byte[] requestItems, int maxBufferLength) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(op_info_transaction);
                xdrOut.writeInt(getHandle());
                // incarnation(?)
                xdrOut.writeInt(0);
                xdrOut.writeBuffer(requestItems);
                xdrOut.writeInt(maxBufferLength);
                xdrOut.flush();
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ioex).toSQLException();
            }
            try {
                final GenericResponse genericResponse = getDatabase().readGenericResponse(null);
                return genericResponse.getData();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
            }
        }
    } catch (SQLException e) {
        exceptionListenerDispatcher.errorOccurred(e);
        throw e;
    }
}
Also used : GenericResponse(org.firebirdsql.gds.ng.wire.GenericResponse) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Aggregations

GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)9 SQLException (java.sql.SQLException)6 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)6 AbstractWireOperations (org.firebirdsql.gds.ng.wire.AbstractWireOperations)6 Test (org.junit.Test)6 SQLWarning (java.sql.SQLWarning)3 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)3 Encoding (org.firebirdsql.encodings.Encoding)2 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)2 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)2 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)2 IOException (java.io.IOException)1