Search in sources :

Example 11 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class ClientListenerNioListener method onHandshake.

/**
 * Perform handshake.
 *
 * @param ses Session.
 * @param msg Message bytes.
 */
private void onHandshake(GridNioSession ses, ClientMessage msg) {
    BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
    BinaryMarshaller marsh = new BinaryMarshaller();
    marsh.setContext(new MarshallerContextImpl(null, null));
    ctx.configure(marsh);
    BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(msg.payload()), null, true);
    byte cmd = reader.readByte();
    if (cmd != ClientListenerRequest.HANDSHAKE) {
        U.warn(log, "Unexpected client request (will close session): " + ses.remoteAddress());
        ses.close();
        return;
    }
    short verMajor = reader.readShort();
    short verMinor = reader.readShort();
    short verMaintenance = reader.readShort();
    ClientListenerProtocolVersion ver = ClientListenerProtocolVersion.create(verMajor, verMinor, verMaintenance);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(8), null, null);
    byte clientType = reader.readByte();
    ClientListenerConnectionContext connCtx = null;
    try {
        connCtx = prepareContext(clientType, ses);
        ensureClientPermissions(clientType);
        if (connCtx.isVersionSupported(ver)) {
            connCtx.initializeFromHandshake(ses, ver, reader);
            ses.addMeta(CONN_CTX_META_KEY, connCtx);
        } else
            throw new IgniteCheckedException("Unsupported version: " + ver.asString());
        cancelHandshakeTimeout(ses);
        connCtx.handler().writeHandshake(writer);
        metrics.onHandshakeAccept(clientType);
    } catch (IgniteAccessControlException authEx) {
        metrics.onFailedAuth();
        writer.writeBoolean(false);
        writer.writeShort((short) 0);
        writer.writeShort((short) 0);
        writer.writeShort((short) 0);
        writer.doWriteString(authEx.getMessage());
        if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
            writer.writeInt(ClientStatus.AUTH_FAILED);
    } catch (IgniteCheckedException e) {
        U.warn(log, "Error during handshake [rmtAddr=" + ses.remoteAddress() + ", msg=" + e.getMessage() + ']');
        metrics.onGeneralReject();
        ClientListenerProtocolVersion currVer;
        if (connCtx == null)
            currVer = ClientListenerProtocolVersion.create(0, 0, 0);
        else
            currVer = connCtx.defaultVersion();
        writer.writeBoolean(false);
        writer.writeShort(currVer.major());
        writer.writeShort(currVer.minor());
        writer.writeShort(currVer.maintenance());
        writer.doWriteString(e.getMessage());
        if (ver.compareTo(ClientConnectionContext.VER_1_1_0) >= 0)
            writer.writeInt(ClientStatus.FAILED);
    }
    ses.send(new ClientMessage(writer.array()));
}
Also used : MarshallerContextImpl(org.apache.ignite.internal.MarshallerContextImpl) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) IgniteAccessControlException(org.apache.ignite.internal.processors.authentication.IgniteAccessControlException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 12 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class ClientMessageParser method decode.

/**
 * {@inheritDoc}
 */
@Override
public ClientListenerRequest decode(ClientMessage msg) {
    assert msg != null;
    BinaryInputStream inStream = new BinaryHeapInputStream(msg.payload());
    // skipHdrCheck must be true (we have 103 op code).
    BinaryReaderExImpl reader = new BinaryReaderExImpl(marsh.context(), inStream, null, null, true, true);
    return decode(reader);
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)

Example 13 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcThinTcpIo method handshake.

/**
 * Used for versions: 2.1.5 and 2.3.0. The protocol version is changed but handshake format isn't changed.
 *
 * @param ver JDBC client version.
 * @throws IOException On IO error.
 * @throws SQLException On connection reject.
 */
private HandshakeResult handshake(ClientListenerProtocolVersion ver) throws IOException, SQLException {
    BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
    BinaryMarshaller marsh = new BinaryMarshaller();
    marsh.setContext(new MarshallerContextImpl(null, null));
    ctx.configure(marsh);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
    writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
    writer.writeShort(ver.major());
    writer.writeShort(ver.minor());
    writer.writeShort(ver.maintenance());
    writer.writeByte(ClientListenerNioListener.JDBC_CLIENT);
    writer.writeBoolean(connProps.isDistributedJoins());
    writer.writeBoolean(connProps.isEnforceJoinOrder());
    writer.writeBoolean(connProps.isCollocated());
    writer.writeBoolean(connProps.isReplicatedOnly());
    writer.writeBoolean(connProps.isAutoCloseServerCursor());
    writer.writeBoolean(connProps.isLazy());
    writer.writeBoolean(connProps.isSkipReducerOnUpdate());
    if (ver.compareTo(VER_2_7_0) >= 0)
        writer.writeString(connProps.nestedTxMode());
    if (ver.compareTo(VER_2_8_0) >= 0) {
        writer.writeByte(nullableBooleanToByte(connProps.isDataPageScanEnabled()));
        JdbcUtils.writeNullableInteger(writer, connProps.getUpdateBatchSize());
    }
    if (ver.compareTo(VER_2_9_0) >= 0) {
        String userAttrs = connProps.getUserAttributesFactory();
        if (F.isEmpty(userAttrs))
            writer.writeMap(null);
        else {
            try {
                Class<Factory<Map<String, String>>> cls = (Class<Factory<Map<String, String>>>) JdbcThinSSLUtil.class.getClassLoader().loadClass(userAttrs);
                Map<String, String> attrs = cls.newInstance().create();
                writer.writeMap(attrs);
            } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                throw new SQLException("Could not found user attributes factory class: " + userAttrs, SqlStateCode.CLIENT_CONNECTION_FAILED, e);
            }
        }
        writer.writeByteArray(ThinProtocolFeature.featuresAsBytes(enabledFeatures()));
    }
    if (!F.isEmpty(connProps.getUsername())) {
        assert ver.compareTo(VER_2_5_0) >= 0 : "Authentication is supported since 2.5";
        writer.writeString(connProps.getUsername());
        writer.writeString(connProps.getPassword());
    }
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(read()), null, null, false);
    boolean accepted = reader.readBoolean();
    if (accepted) {
        HandshakeResult handshakeRes = new HandshakeResult();
        if (reader.available() > 0) {
            byte maj = reader.readByte();
            byte min = reader.readByte();
            byte maintenance = reader.readByte();
            String stage = reader.readString();
            long ts = reader.readLong();
            byte[] hash = reader.readByteArray();
            if (ver.compareTo(VER_2_8_0) >= 0)
                handshakeRes.nodeId(reader.readUuid());
            handshakeRes.igniteVersion(new IgniteProductVersion(maj, min, maintenance, stage, ts, hash));
            if (ver.compareTo(VER_2_9_0) >= 0) {
                byte[] srvFeatures = reader.readByteArray();
                EnumSet<JdbcThinFeature> features = JdbcThinFeature.enumSet(srvFeatures);
                handshakeRes.features(features);
            }
        } else {
            handshakeRes.igniteVersion(new IgniteProductVersion((byte) 2, (byte) 0, (byte) 0, "Unknown", 0L, null));
        }
        handshakeRes.serverProtocolVersion(ver);
        return handshakeRes;
    } else {
        short maj = reader.readShort();
        short min = reader.readShort();
        short maintenance = reader.readShort();
        String err = reader.readString();
        ClientListenerProtocolVersion srvProtoVer0 = ClientListenerProtocolVersion.create(maj, min, maintenance);
        if (srvProtoVer0.compareTo(VER_2_5_0) < 0 && !F.isEmpty(connProps.getUsername())) {
            throw new SQLException("Authentication doesn't support by remote server[driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ", url=" + connProps.getUrl() + " address=" + sockAddr + ']', SqlStateCode.CONNECTION_REJECTED);
        }
        if (VER_2_8_0.equals(srvProtoVer0) || VER_2_7_0.equals(srvProtoVer0) || VER_2_5_0.equals(srvProtoVer0) || VER_2_4_0.equals(srvProtoVer0) || VER_2_3_0.equals(srvProtoVer0) || VER_2_1_5.equals(srvProtoVer0))
            return handshake(srvProtoVer0);
        else if (VER_2_1_0.equals(srvProtoVer0))
            return handshake_2_1_0();
        else {
            throw new SQLException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ']', SqlStateCode.CONNECTION_REJECTED);
        }
    }
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) SQLException(java.sql.SQLException) Factory(javax.cache.configuration.Factory) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) MarshallerContextImpl(org.apache.ignite.internal.MarshallerContextImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientListenerProtocolVersion(org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) JdbcThinFeature(org.apache.ignite.internal.processors.odbc.jdbc.JdbcThinFeature) Map(java.util.Map)

Example 14 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcThinTcpIo method readResponse.

/**
 * @return Server response.
 * @throws IOException In case of IO error.
 */
JdbcResponse readResponse() throws IOException {
    BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(read()), null, true);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader, protoCtx);
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)

Example 15 with BinaryHeapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryHeapInputStream in project ignite by apache.

the class JdbcThinTcpIo method handshake_2_1_0.

/**
 * Compatibility handshake for server version 2.1.0
 *
 * @throws IOException On IO error.
 * @throws SQLException On connection reject.
 */
private HandshakeResult handshake_2_1_0() throws IOException, SQLException {
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
    writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
    writer.writeShort(VER_2_1_0.major());
    writer.writeShort(VER_2_1_0.minor());
    writer.writeShort(VER_2_1_0.maintenance());
    writer.writeByte(ClientListenerNioListener.JDBC_CLIENT);
    writer.writeBoolean(connProps.isDistributedJoins());
    writer.writeBoolean(connProps.isEnforceJoinOrder());
    writer.writeBoolean(connProps.isCollocated());
    writer.writeBoolean(connProps.isReplicatedOnly());
    writer.writeBoolean(connProps.isAutoCloseServerCursor());
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    boolean accepted = reader.readBoolean();
    if (accepted) {
        HandshakeResult handshakeRes = new HandshakeResult();
        handshakeRes.igniteVersion(new IgniteProductVersion((byte) 2, (byte) 1, (byte) 0, "Unknown", 0L, null));
        handshakeRes.serverProtocolVersion(VER_2_1_0);
        return handshakeRes;
    } else {
        short maj = reader.readShort();
        short min = reader.readShort();
        short maintenance = reader.readShort();
        String err = reader.readString();
        ClientListenerProtocolVersion ver = ClientListenerProtocolVersion.create(maj, min, maintenance);
        throw new SQLException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + ver + ", err=" + err + ']', SqlStateCode.CONNECTION_REJECTED);
    }
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) SQLException(java.sql.SQLException) ClientListenerProtocolVersion(org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Aggregations

BinaryHeapInputStream (org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)16 BinaryReaderExImpl (org.apache.ignite.internal.binary.BinaryReaderExImpl)13 BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)9 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)8 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 JdbcResponse (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)3 SQLException (java.sql.SQLException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 MarshallerContextImpl (org.apache.ignite.internal.MarshallerContextImpl)2 BinaryContext (org.apache.ignite.internal.binary.BinaryContext)2 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)2 ClientListenerProtocolVersion (org.apache.ignite.internal.processors.odbc.ClientListenerProtocolVersion)2 ClientListenerRequest (org.apache.ignite.internal.processors.odbc.ClientListenerRequest)2 IgniteProductVersion (org.apache.ignite.lang.IgniteProductVersion)2 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1