1
1
/*******************************************************************************
2
- * Copyright (c) 2016, 2023 Contributors to the Eclipse Foundation
2
+ * Copyright (c) 2023 Contributors to the Eclipse Foundation
3
3
*
4
4
* See the NOTICE file(s) distributed with this work for additional
5
5
* information regarding copyright ownership.
41
41
import org .eclipse .hono .adapter .AbstractProtocolAdapterBase ;
42
42
import org .eclipse .hono .adapter .AdapterConnectionsExceededException ;
43
43
import org .eclipse .hono .adapter .AuthorizationException ;
44
+ import org .eclipse .hono .adapter .ConnectionDurationExceededException ;
45
+ import org .eclipse .hono .adapter .DataVolumeExceededException ;
46
+ import org .eclipse .hono .adapter .TenantConnectionsExceededException ;
44
47
import org .eclipse .hono .adapter .auth .device .AuthHandler ;
45
48
import org .eclipse .hono .adapter .auth .device .ChainAuthHandler ;
46
49
import org .eclipse .hono .adapter .auth .device .CredentialsApiAuthProvider ;
90
93
import io .netty .handler .codec .mqtt .MqttConnectReturnCode ;
91
94
import io .netty .handler .codec .mqtt .MqttProperties ;
92
95
import io .netty .handler .codec .mqtt .MqttQoS ;
96
+ import io .netty .handler .codec .mqtt .MqttVersion ;
93
97
import io .opentracing .Span ;
94
98
import io .opentracing .SpanContext ;
95
99
import io .opentracing .log .Fields ;
@@ -518,7 +522,8 @@ final void handleEndpointConnection(final MqttEndpoint endpoint) {
518
522
log .debug ("rejecting connection request from client [clientId: {}], cause:" ,
519
523
endpoint .clientIdentifier (), t );
520
524
521
- final MqttConnectReturnCode code = getConnectReturnCode (t );
525
+ final boolean isPreMqtt5 = ((int ) MqttVersion .MQTT_5 .protocolLevel ()) > endpoint .protocolVersion ();
526
+ final var code = isPreMqtt5 ? getMqtt3ConnackReturnCode (t ) : getMqtt5ConnackReasonCode (t );
522
527
rejectConnectionRequest (endpoint , code , span );
523
528
TracingHelper .logError (span , t );
524
529
}
@@ -1106,18 +1111,18 @@ final MqttDeviceEndpoint createMqttDeviceEndpoint(
1106
1111
return mqttDeviceEndpoint ;
1107
1112
}
1108
1113
1109
- private static MqttConnectReturnCode getConnectReturnCode (final Throwable e ) {
1110
-
1111
- if ( e instanceof MqttConnectionException ) {
1112
- return (( MqttConnectionException ) e ). code ();
1113
- } else if (e instanceof AuthorizationException ) {
1114
- if ( e instanceof AdapterConnectionsExceededException ) {
1115
- return MqttConnectReturnCode . CONNECTION_REFUSED_SERVER_UNAVAILABLE ;
1116
- } else {
1117
- return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED ;
1118
- }
1119
- } else if (e instanceof ServiceInvocationException ) {
1120
- switch ((( ServiceInvocationException ) e ) .getErrorCode ()) {
1114
+ private static MqttConnectReturnCode getMqtt3ConnackReturnCode (final Throwable e ) {
1115
+ if ( e instanceof MqttConnectionException connectionException ) {
1116
+ return connectionException . code ();
1117
+ }
1118
+ if (e instanceof AdapterConnectionsExceededException ) {
1119
+ return MqttConnectReturnCode . CONNECTION_REFUSED_SERVER_UNAVAILABLE ;
1120
+ }
1121
+ if ( e instanceof AuthorizationException ) {
1122
+ return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED ;
1123
+ }
1124
+ if (e instanceof ServiceInvocationException exception ) {
1125
+ switch (exception .getErrorCode ()) {
1121
1126
case HttpURLConnection .HTTP_UNAUTHORIZED :
1122
1127
case HttpURLConnection .HTTP_NOT_FOUND :
1123
1128
return MqttConnectReturnCode .CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD ;
@@ -1126,9 +1131,39 @@ private static MqttConnectReturnCode getConnectReturnCode(final Throwable e) {
1126
1131
default :
1127
1132
return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED ;
1128
1133
}
1129
- } else {
1130
- return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED ;
1131
1134
}
1135
+
1136
+ return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED ;
1137
+ }
1138
+
1139
+ private static MqttConnectReturnCode getMqtt5ConnackReasonCode (final Throwable e ) {
1140
+
1141
+ if (e instanceof MqttConnectionException connectionException ) {
1142
+ return connectionException .code ();
1143
+ }
1144
+ if (e instanceof AdapterConnectionsExceededException ) {
1145
+ return MqttConnectReturnCode .CONNECTION_REFUSED_USE_ANOTHER_SERVER ;
1146
+ }
1147
+ if (e instanceof TenantConnectionsExceededException
1148
+ || e instanceof DataVolumeExceededException
1149
+ || e instanceof ConnectionDurationExceededException ) {
1150
+ return MqttConnectReturnCode .CONNECTION_REFUSED_QUOTA_EXCEEDED ;
1151
+ }
1152
+ if (e instanceof AuthorizationException ) {
1153
+ return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED_5 ;
1154
+ }
1155
+ if (e instanceof ServiceInvocationException exception ) {
1156
+ switch (exception .getErrorCode ()) {
1157
+ case HttpURLConnection .HTTP_UNAUTHORIZED :
1158
+ case HttpURLConnection .HTTP_NOT_FOUND :
1159
+ return MqttConnectReturnCode .CONNECTION_REFUSED_BAD_USERNAME_OR_PASSWORD ;
1160
+ case HttpURLConnection .HTTP_UNAVAILABLE :
1161
+ return MqttConnectReturnCode .CONNECTION_REFUSED_SERVER_UNAVAILABLE_5 ;
1162
+ default :
1163
+ return MqttConnectReturnCode .CONNECTION_REFUSED_NOT_AUTHORIZED_5 ;
1164
+ }
1165
+ }
1166
+ return MqttConnectReturnCode .CONNECTION_REFUSED_UNSPECIFIED_ERROR ;
1132
1167
}
1133
1168
1134
1169
/**
0 commit comments