30
30
31
31
class InitialHandshakeCommandCodec extends AuthenticationCommandBaseCodec <Connection , InitialHandshakeCommand > {
32
32
33
- private static final int ST_CONNECTING = 0 ;
34
- private static final int ST_AUTHENTICATING = 1 ;
35
- private static final int ST_CONNECTED = 2 ;
36
- private static final int ST_CONNECT_FAILED = 3 ;
33
+ private static enum ConnectionState {
34
+ CONNECTING , AUTHENTICATING , CONNECTED , CONNECT_FAILED
35
+ }
37
36
38
37
private static final int TARGET_SECURITY_MEASURE = DRDAConstants .SECMEC_USRIDPWD ;
39
38
@@ -42,7 +41,7 @@ class InitialHandshakeCommandCodec extends AuthenticationCommandBaseCodec<Connec
42
41
// It is saved like the prddta in case it is needed for a connect reflow.
43
42
private byte [] correlationToken ;
44
43
45
- private int status = ST_CONNECTING ;
44
+ private ConnectionState status = ConnectionState . CONNECTING ;
46
45
47
46
InitialHandshakeCommandCodec (InitialHandshakeCommand cmd ) {
48
47
super (cmd );
@@ -53,66 +52,20 @@ void encode(DB2Encoder encoder) {
53
52
super .encode (encoder );
54
53
encoder .connMetadata .databaseName = cmd .database ();
55
54
encoder .socketConnection .closeHandler (h -> {
56
- if (status == ST_CONNECTING ) {
55
+ if (status == ConnectionState . CONNECTING ) {
57
56
// Sometimes DB2 closes the connection when sending an invalid Database name.
58
57
// -4499 = A fatal error occurred that resulted in a disconnect from the data
59
58
// source.
60
59
// 08001 = "The connection was unable to be established"
61
- cmd .fail (new DB2Exception ("The connection was closed by the database server." ,
62
- SqlCode .CONNECTION_REFUSED ,
60
+ cmd .fail (new DB2Exception ("The connection was closed by the database server." , SqlCode .CONNECTION_REFUSED ,
63
61
SQLState .AUTH_DATABASE_CONNECTION_REFUSED ));
64
62
}
65
63
});
66
- sendInitialHandshake ();
67
- }
68
-
69
- @ Override
70
- void decodePayload (ByteBuf payload , int payloadLength ) {
71
- DRDAConnectResponse response = new DRDAConnectResponse (payload , encoder .connMetadata );
72
- switch (status ) {
73
- case ST_CONNECTING :
74
- response .readExchangeServerAttributes ();
75
- // readAccessSecurity can throw a DB2Exception if there are problems connecting.
76
- // In that case, we want to catch that exception and
77
- // make sure to set the status to something other than ST_CONNECTING so we don't
78
- // try to complete the result twice (when we hit encode)
79
- try {
80
- response .readAccessSecurity (TARGET_SECURITY_MEASURE );
81
- } catch (DB2Exception de ) {
82
- status = ST_CONNECT_FAILED ;
83
- throw de ;
84
- }
85
- status = ST_AUTHENTICATING ;
86
- ByteBuf packet = allocateBuffer ();
87
- int packetStartIdx = packet .writerIndex ();
88
- DRDAConnectRequest securityCheck = new DRDAConnectRequest (packet , encoder .connMetadata );
89
- correlationToken = securityCheck .getCorrelationToken (encoder .socketConnection .socket ().localAddress ().port ());
90
- securityCheck .buildSECCHK (TARGET_SECURITY_MEASURE , cmd .database (), cmd .username (), cmd .password (), null , // sectkn,
91
- null ); // sectkn2
92
- securityCheck .buildACCRDB (cmd .database (), false , // readOnly,
93
- correlationToken , DRDAConstants .SYSTEM_ASC );
94
- securityCheck .completeCommand ();
95
- int lenOfPayload = packet .writerIndex () - packetStartIdx ;
96
- sendPacket (packet , lenOfPayload );
97
- return ;
98
- case ST_AUTHENTICATING :
99
- response .readSecurityCheck ();
100
- RDBAccessData accData = response .readAccessDatabase ();
101
- if (accData .correlationToken != null )
102
- correlationToken = accData .correlationToken ;
103
- status = ST_CONNECTED ;
104
- completionHandler .handle (CommandResponse .success (cmd .connection ()));
105
- return ;
106
- default :
107
- throw new IllegalStateException ("Unknown state: " + status );
108
- }
109
- }
110
64
111
- private void sendInitialHandshake () {
112
65
ByteBuf packet = allocateBuffer ();
113
66
int packetStartIdx = packet .writerIndex ();
114
- DRDAConnectRequest cmd = new DRDAConnectRequest (packet , encoder .connMetadata );
115
- cmd .buildEXCSAT (DRDAConstants .EXTNAM , // externalName,
67
+ DRDAConnectRequest connectRequest = new DRDAConnectRequest (packet , encoder .connMetadata );
68
+ connectRequest .buildEXCSAT (DRDAConstants .EXTNAM , // externalName,
116
69
0x0A , // targetAgent,
117
70
DRDAConstants .TARGET_SQL_AM , // targetSqlam,
118
71
0x0C , // targetRdb,
@@ -124,11 +77,40 @@ private void sendInitialHandshake() {
124
77
0 , // targetRsyncmgr,
125
78
CCSIDConstants .TARGET_UNICODE_MGR // targetUnicodemgr
126
79
);
127
- cmd .buildACCSEC (TARGET_SECURITY_MEASURE , this .cmd .database (), null );
128
- cmd .completeCommand ();
80
+ connectRequest .buildACCSEC (TARGET_SECURITY_MEASURE , this .cmd .database (), null );
81
+ correlationToken = connectRequest .getCorrelationToken (encoder .socketConnection .socket ().localAddress ().port ());
82
+ connectRequest .buildSECCHK (TARGET_SECURITY_MEASURE , cmd .database (), cmd .username (), cmd .password (), null , // sectkn,
83
+ null ); // sectkn2
84
+ connectRequest .buildACCRDB (cmd .database (), false , // readOnly,
85
+ correlationToken , DRDAConstants .SYSTEM_ASC );
86
+ connectRequest .completeCommand ();
129
87
130
88
int lenOfPayload = packet .writerIndex () - packetStartIdx ;
131
89
sendPacket (packet , lenOfPayload );
132
90
}
133
91
92
+ @ Override
93
+ void decodePayload (ByteBuf payload , int payloadLength ) {
94
+ DRDAConnectResponse response = new DRDAConnectResponse (payload , encoder .connMetadata );
95
+ response .readExchangeServerAttributes ();
96
+ // readAccessSecurity can throw a DB2Exception if there are problems connecting.
97
+ // In that case, we want to catch that exception and
98
+ // make sure to set the status to something other than ST_CONNECTING so we don't
99
+ // try to complete the result twice (when we hit encode)
100
+ try {
101
+ response .readAccessSecurity (TARGET_SECURITY_MEASURE );
102
+ } catch (DB2Exception de ) {
103
+ status = ConnectionState .CONNECT_FAILED ;
104
+ throw de ;
105
+ }
106
+ status = ConnectionState .AUTHENTICATING ;
107
+ response .readSecurityCheck ();
108
+ RDBAccessData accData = response .readAccessDatabase ();
109
+ if (accData .correlationToken != null ) {
110
+ correlationToken = accData .correlationToken ;
111
+ }
112
+ status = ConnectionState .CONNECTED ;
113
+ completionHandler .handle (CommandResponse .success (cmd .connection ()));
114
+ }
115
+
134
116
}
0 commit comments