Search in sources :

Example 1 with Encoding

use of org.firebirdsql.encodings.Encoding in project jaybird by FirebirdSQL.

the class AbstractStatementTest method testInsertSelectUTF8Value.

@Test
public void testInsertSelectUTF8Value() throws Exception {
    allocateStatement();
    // Insert UTF8 columns
    statement.prepare(INSERT_THEUTFVALUE);
    final Encoding utf8Encoding = db.getEncodingFactory().getEncodingForFirebirdName("UTF8");
    final String aEuro = "a\u20AC";
    final byte[] insertFieldData = utf8Encoding.encodeToCharset(aEuro);
    final RowDescriptor parametersInsert = statement.getParameterDescriptor();
    final RowValue parameterValuesInsert = RowValue.of(parametersInsert, db.getDatatypeCoder().encodeInt(1), insertFieldData, insertFieldData);
    statement.execute(parameterValuesInsert);
    // Retrieve the just inserted UTF8 values from the database for comparison
    statement.prepare(SELECT_THEUTFVALUE);
    final RowDescriptor parametersSelect = statement.getParameterDescriptor();
    final RowValue parameterValuesSelect = RowValue.of(parametersSelect, db.getDatatypeCoder().encodeInt(1));
    final SimpleStatementListener statementListener = new SimpleStatementListener();
    statement.addStatementListener(statementListener);
    statement.execute(parameterValuesSelect);
    statement.fetchRows(1);
    final List<RowValue> rows = statementListener.getRows();
    assertEquals("Expected a row", 1, rows.size());
    final RowValue selectResult = rows.get(0);
    final byte[] selectVarcharFieldData = selectResult.getFieldData(0);
    final byte[] selectCharFieldData = selectResult.getFieldData(1);
    assertEquals("Length of selected varchar field data", 4, selectVarcharFieldData.length);
    assertEquals("Length of selected char field data", 20, selectCharFieldData.length);
    String decodedVarchar = utf8Encoding.decodeFromCharset(selectVarcharFieldData);
    String decodedChar = utf8Encoding.decodeFromCharset(selectCharFieldData);
    assertEquals("Unexpected value for varchar", aEuro, decodedVarchar);
    assertEquals("Unexpected value for trimmed char", aEuro, decodedChar.trim());
    // Note artificial result from the way UTF8 is handled
    assertEquals("Unexpected length for char", 18, decodedChar.length());
    char[] spaceChars16 = new char[16];
    Arrays.fill(spaceChars16, ' ');
    assertEquals("Unexpected trailing characters for char", new String(spaceChars16), decodedChar.substring(2));
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) RowValue(org.firebirdsql.gds.ng.fields.RowValue) Encoding(org.firebirdsql.encodings.Encoding) RowDescriptor(org.firebirdsql.gds.ng.fields.RowDescriptor) Test(org.junit.Test)

Example 2 with Encoding

use of org.firebirdsql.encodings.Encoding in project jaybird by FirebirdSQL.

the class V13ParameterConverter method createServiceParameterBuffer.

protected ServiceParameterBuffer createServiceParameterBuffer(WireServiceConnection connection) {
    final Encoding stringEncoding = connection.getEncodingFactory().getEncodingForFirebirdName("UTF8");
    ServiceParameterBuffer spb = new ServiceParameterBufferImp(ServiceParameterBufferImp.SpbMetaData.SPB_VERSION_3_ATTACH, stringEncoding);
    spb.addArgument(SpbItems.isc_spb_utf8_filename);
    return spb;
}
Also used : Encoding(org.firebirdsql.encodings.Encoding) ServiceParameterBufferImp(org.firebirdsql.gds.impl.ServiceParameterBufferImp)

Example 3 with Encoding

use of org.firebirdsql.encodings.Encoding 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 4 with Encoding

use of org.firebirdsql.encodings.Encoding in project jaybird by FirebirdSQL.

the class V13WireOperations method authReceiveResponse.

@Override
public void authReceiveResponse(FbWireAttachment.AcceptPacket acceptPacket, 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_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().getWireCrypt();
                    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 5 with Encoding

use of org.firebirdsql.encodings.Encoding in project jaybird by FirebirdSQL.

the class FBLongVarCharField method copyCharacterStream.

private void copyCharacterStream(Reader in, long length) throws SQLException {
    FBBlob blob = new FBBlob(gdsHelper);
    // TODO Push this down into FBBlob?
    Encoding encoding = getDatatypeCoder().getEncoding();
    blob.copyCharacterStream(in, length, encoding);
    setFieldData(getDatatypeCoder().encodeLong(blob.getBlobId()));
    blobExplicitNull = false;
}
Also used : Encoding(org.firebirdsql.encodings.Encoding) FBBlob(org.firebirdsql.jdbc.FBBlob)

Aggregations

Encoding (org.firebirdsql.encodings.Encoding)10 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)4 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)3 SQLException (java.sql.SQLException)2 DatabaseParameterBufferImp (org.firebirdsql.gds.impl.DatabaseParameterBufferImp)2 ServiceParameterBufferImp (org.firebirdsql.gds.impl.ServiceParameterBufferImp)2 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)2 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)2 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)2 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)2 DatabaseParameterBuffer (org.firebirdsql.gds.DatabaseParameterBuffer)1 ServiceParameterBuffer (org.firebirdsql.gds.ServiceParameterBuffer)1 RowDescriptor (org.firebirdsql.gds.ng.fields.RowDescriptor)1 RowValue (org.firebirdsql.gds.ng.fields.RowValue)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1 FBBlob (org.firebirdsql.jdbc.FBBlob)1 Test (org.junit.Test)1