Search in sources :

Example 11 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V13Statement method writeSqlData.

/**
 * Write a set of SQL data from a list of {@link FieldValue} instances.
 *
 * @param rowDescriptor
 *         The row descriptor
 * @param fieldValues
 *         The List containing the SQL data to be written
 * @throws IOException
 *         if an error occurs while writing to the underlying output stream
 */
protected void writeSqlData(final RowDescriptor rowDescriptor, final RowValue fieldValues) throws IOException, SQLException {
    final XdrOutputStream xdrOut = getXdrOut();
    final BlrCalculator blrCalculator = getDatabase().getBlrCalculator();
    // null indicator bitmap
    final BitSet nullBits = new BitSet(fieldValues.getCount());
    for (int idx = 0; idx < fieldValues.getCount(); idx++) {
        final FieldValue fieldValue = fieldValues.getFieldValue(idx);
        nullBits.set(idx, fieldValue.getFieldData() == null);
    }
    // Note only amount of bytes necessary for highest bit set
    final byte[] nullBitsBytes = nullBits.toByteArray();
    xdrOut.write(nullBitsBytes);
    final int requiredBytes = (rowDescriptor.getCount() + 7) / 8;
    final int remainingBytes = requiredBytes - nullBitsBytes.length;
    if (remainingBytes > 0) {
        xdrOut.write(new byte[remainingBytes]);
    }
    xdrOut.writeAlignment(requiredBytes);
    for (int idx = 0; idx < fieldValues.getCount(); idx++) {
        if (nullBits.get(idx)) {
            continue;
        }
        final FieldValue fieldValue = fieldValues.getFieldValue(idx);
        final FieldDescriptor fieldDescriptor = rowDescriptor.getFieldDescriptor(idx);
        final int len = blrCalculator.calculateIoLength(fieldDescriptor, fieldValue);
        final byte[] buffer = fieldValue.getFieldData();
        final int fieldType = fieldDescriptor.getType();
        writeColumnData(xdrOut, len, buffer, fieldType);
    }
}
Also used : BitSet(java.util.BitSet) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Example 12 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V13WireOperations method enableEncryption.

protected void enableEncryption(EncryptionInitInfo encryptionInitInfo) throws SQLException, IOException {
    final XdrInputStream xdrIn = getXdrIn();
    final XdrOutputStream xdrOut = getXdrOut();
    final Encoding encoding = getEncoding();
    final EncryptionIdentifier encryptionIdentifier = encryptionInitInfo.getEncryptionIdentifier();
    xdrOut.writeInt(op_crypt);
    xdrOut.writeString(encryptionIdentifier.getPluginName(), encoding);
    xdrOut.writeString(encryptionIdentifier.getType(), encoding);
    xdrOut.flush();
    xdrIn.setCipher(encryptionInitInfo.getDecryptionCipher());
    xdrOut.setCipher(encryptionInitInfo.getEncryptionCipher());
    readOperationResponse(readNextOperation(), null);
}
Also used : XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Encoding(org.firebirdsql.encodings.Encoding) EncryptionIdentifier(org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)

Example 13 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class WireConnection method identify.

/**
 * Performs the connection identification phase of the Wire protocol and
 * returns the FbWireDatabase implementation for the agreed protocol.
 *
 * @return FbWireDatabase
 * @throws SQLTimeoutException
 * @throws SQLException
 */
@Override
public final C identify() throws SQLException {
    try {
        xdrIn = new XdrInputStream(socket.getInputStream());
        xdrOut = new XdrOutputStream(socket.getOutputStream());
        xdrOut.writeInt(op_connect);
        xdrOut.writeInt(op_attach);
        xdrOut.writeInt(CONNECT_VERSION3);
        xdrOut.writeInt(arch_generic);
        xdrOut.writeString(getAttachObjectName(), getEncoding());
        // Count of protocols understood
        xdrOut.writeInt(protocols.getProtocolCount());
        xdrOut.writeBuffer(createUserIdentificationBlock());
        for (ProtocolDescriptor protocol : protocols) {
            // Protocol version
            xdrOut.writeInt(protocol.getVersion());
            // Architecture of client
            xdrOut.writeInt(protocol.getArchitecture());
            // Minimum type
            xdrOut.writeInt(protocol.getMinimumType());
            // Maximum type
            xdrOut.writeInt(protocol.getMaximumType());
            // Preference weight
            xdrOut.writeInt(protocol.getWeight());
        }
        xdrOut.flush();
        final int operation = readNextOperation();
        if (operation == op_accept || operation == op_cond_accept || operation == op_accept_data) {
            FbWireAttachment.AcceptPacket acceptPacket = new FbWireAttachment.AcceptPacket();
            acceptPacket.operation = operation;
            // Protocol version
            protocolVersion = xdrIn.readInt();
            // Architecture for protocol
            protocolArchitecture = xdrIn.readInt();
            // Minimum type
            protocolMinimumType = xdrIn.readInt();
            if (protocolVersion < 0) {
                protocolVersion = (protocolVersion & FB_PROTOCOL_MASK) | FB_PROTOCOL_FLAG;
            }
            if (operation == op_cond_accept || operation == op_accept_data) {
                byte[] data = acceptPacket.p_acpt_data = xdrIn.readBuffer();
                acceptPacket.p_acpt_plugin = xdrIn.readString(getEncoding());
                final int isAuthenticated = xdrIn.readInt();
                byte[] serverKeys = acceptPacket.p_acpt_keys = xdrIn.readBuffer();
                clientAuthBlock.setServerData(data);
                clientAuthBlock.setAuthComplete(isAuthenticated == 1);
                addServerKeys(serverKeys);
                clientAuthBlock.resetClient(serverKeys);
                clientAuthBlock.switchPlugin(acceptPacket.p_acpt_plugin);
            } else {
                clientAuthBlock.resetClient(null);
            }
            ProtocolDescriptor descriptor = protocols.getProtocolDescriptor(protocolVersion);
            if (descriptor == null) {
                throw new SQLException(String.format("Unsupported or unexpected protocol version %d connecting to database %s. Supported version(s): %s", protocolVersion, getServerName(), protocols.getProtocolVersions()));
            }
            C connectionHandle = createConnectionHandle(descriptor);
            if (operation == op_cond_accept) {
                connectionHandle.authReceiveResponse(acceptPacket);
            }
            return connectionHandle;
        } else {
            try {
                if (operation == op_response) {
                    // Handle exception from response
                    AbstractWireOperations wireOperations = getDefaultWireOperations();
                    wireOperations.processResponse(wireOperations.processOperation(operation));
                }
            } finally {
                try {
                    close();
                } catch (Exception ex) {
                    log.debug("Ignoring exception on disconnect in connect phase of protocol", ex);
                }
            }
            throw new FbExceptionBuilder().exception(ISCConstants.isc_connect_reject).toFlatSQLException();
        }
    } catch (SocketTimeoutException ste) {
        throw new FbExceptionBuilder().timeoutException(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ste).toSQLException();
    } catch (IOException ioex) {
        throw new FbExceptionBuilder().exception(ISCConstants.isc_network_error).messageParameter(getServerName()).cause(ioex).toSQLException();
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) IOException(java.io.IOException) XdrInputStream(org.firebirdsql.gds.impl.wire.XdrInputStream)

Example 14 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class AbstractFbWireBlob method getBlobInfo.

// NOTE If we need to override some of the blob operations below in the future, consider introducing a separate
// object that is injected by the ProtocolDescriptor so that we don't need to override separately for input and output.
@Override
public byte[] getBlobInfo(final byte[] requestItems, final int bufferLength) throws SQLException {
    try {
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getDatabase().getXdrStreamAccess().getXdrOut();
                xdrOut.writeInt(WireProtocolConstants.op_info_blob);
                xdrOut.writeInt(getHandle());
                // incarnation
                xdrOut.writeInt(0);
                xdrOut.writeBuffer(requestItems);
                xdrOut.writeInt(bufferLength);
                xdrOut.flush();
            } catch (IOException ex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
            }
            try {
                // TODO: Blob warning callback or just database default?
                GenericResponse response = getDatabase().readGenericResponse(null);
                return response.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 : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 15 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream 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)

Aggregations

XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)41 SQLException (java.sql.SQLException)25 IOException (java.io.IOException)24 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)22 SimpleServer (org.firebirdsql.common.SimpleServer)4 Encoding (org.firebirdsql.encodings.Encoding)3 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)2 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 BitSet (java.util.BitSet)1 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)1 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1