Skip to content

Commit

Permalink
Port Mqtt3ConnectRestrictions from master
Browse files Browse the repository at this point in the history
with the following changes:
- new package names
- replace annotations DoNotImplement and CheckReturnValue
- add missing Range annotations
- rename fluent restrictions() -> restrictionsWith()
  • Loading branch information
SgtSilvio committed Nov 19, 2023
1 parent 0b0ba62 commit ef530d6
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.hivemq.mqtt.client2.internal.datatypes.MqttVariableByteInteger;
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3ConnectRestrictions;
import com.hivemq.mqtt.client2.mqtt5.message.connect.Mqtt5ConnectRestrictions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,7 +29,7 @@
* @author Silvio Giebl
*/
@Unmodifiable
public class MqttConnectRestrictions implements Mqtt5ConnectRestrictions {
public class MqttConnectRestrictions implements Mqtt5ConnectRestrictions, Mqtt3ConnectRestrictions {

public static final @NotNull MqttConnectRestrictions DEFAULT =
new MqttConnectRestrictions(DEFAULT_RECEIVE_MAXIMUM, DEFAULT_SEND_MAXIMUM, DEFAULT_MAXIMUM_PACKET_SIZE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hivemq.mqtt.client2.internal.util.Checks;
import com.hivemq.mqtt.client2.internal.util.MqttChecks;
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3ConnectRestrictionsBuilder;
import com.hivemq.mqtt.client2.mqtt5.message.connect.Mqtt5ConnectRestrictionsBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
Expand Down Expand Up @@ -107,7 +108,7 @@ public abstract class MqttConnectRestrictionsBuilder<B extends MqttConnectRestri
}

public static class Default extends MqttConnectRestrictionsBuilder<Default>
implements Mqtt5ConnectRestrictionsBuilder {
implements Mqtt5ConnectRestrictionsBuilder, Mqtt3ConnectRestrictionsBuilder {

public Default() {}

Expand All @@ -122,9 +123,9 @@ public Default() {}
}

public static class Nested<P> extends MqttConnectRestrictionsBuilder<Nested<P>>
implements Mqtt5ConnectRestrictionsBuilder.Nested<P> {
implements Mqtt5ConnectRestrictionsBuilder.Nested<P>, Mqtt3ConnectRestrictionsBuilder.Nested<P> {

Nested(
public Nested(
final @NotNull MqttConnectRestrictions restrictions,
final @NotNull Function<? super MqttConnectRestrictions, P> parentConsumer) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import com.hivemq.mqtt.client2.mqtt3.message.auth.Mqtt3SimpleAuth;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3Connect;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3ConnectRestrictions;
import com.hivemq.mqtt.client2.mqtt3.message.publish.Mqtt3Publish;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -40,26 +41,26 @@
@Unmodifiable
public class Mqtt3ConnectView implements Mqtt3Connect {

public static final @NotNull Mqtt3ConnectView DEFAULT = of(DEFAULT_KEEP_ALIVE, DEFAULT_CLEAN_SESSION, null, null);
public static final @NotNull Mqtt3ConnectView DEFAULT =
of(DEFAULT_KEEP_ALIVE, DEFAULT_CLEAN_SESSION, MqttConnectRestrictions.DEFAULT, null, null);

private static @NotNull MqttConnect delegate(
final @Range(from = 0, to = UnsignedDataTypes.UNSIGNED_SHORT_MAX_VALUE) int keepAlive,
final boolean cleanSession,
final boolean cleanSession, final @NotNull MqttConnectRestrictions restrictions,
final @Nullable MqttSimpleAuth simpleAuth,
final @Nullable MqttWillPublish willPublish) {

return new MqttConnect(keepAlive, cleanSession, cleanSession ? 0 : MqttConnect.NO_SESSION_EXPIRY,
MqttConnectRestrictions.DEFAULT, simpleAuth, null, willPublish,
MqttUserPropertiesImpl.NO_USER_PROPERTIES);
return new MqttConnect(keepAlive, cleanSession, cleanSession ? 0 : MqttConnect.NO_SESSION_EXPIRY, restrictions,
simpleAuth, null, willPublish, MqttUserPropertiesImpl.NO_USER_PROPERTIES);
}

static @NotNull Mqtt3ConnectView of(
final @Range(from = 0, to = UnsignedDataTypes.UNSIGNED_SHORT_MAX_VALUE) int keepAlive,
final boolean cleanSession,
final boolean cleanSession, final @NotNull MqttConnectRestrictions restrictions,
final @Nullable MqttSimpleAuth simpleAuth,
final @Nullable MqttWillPublish willPublish) {

return new Mqtt3ConnectView(delegate(keepAlive, cleanSession, simpleAuth, willPublish));
return new Mqtt3ConnectView(delegate(keepAlive, cleanSession, restrictions, simpleAuth, willPublish));
}

public static @NotNull Mqtt3ConnectView of(final @NotNull MqttConnect delegate) {
Expand All @@ -82,6 +83,11 @@ public boolean isCleanSession() {
return delegate.isCleanStart();
}

@Override
public @NotNull Mqtt3ConnectRestrictions getRestrictions() {
return delegate.getRestrictions();
}

@Override
public @NotNull Optional<Mqtt3SimpleAuth> getSimpleAuth() {
return Optional.ofNullable(getRawSimpleAuth());
Expand Down Expand Up @@ -114,7 +120,8 @@ public boolean isCleanSession() {
private @NotNull String toAttributeString() {
final Mqtt3SimpleAuth simpleAuth = getRawSimpleAuth();
final Mqtt3Publish willPublish = getRawWillPublish();
return "keepAlive=" + getKeepAlive() + ", cleanSession=" + isCleanSession() +
final Mqtt3ConnectRestrictions restrictions = getRestrictions();
return "keepAlive=" + getKeepAlive() + ", cleanSession=" + isCleanSession() + ", restrictions=" + restrictions +
((simpleAuth == null) ? "" : ", simpleAuth=" + simpleAuth) +
((willPublish == null) ? "" : ", willPublish=" + willPublish);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import com.hivemq.mqtt.client2.internal.message.auth.mqtt3.Mqtt3SimpleAuthView;
import com.hivemq.mqtt.client2.internal.message.auth.mqtt3.Mqtt3SimpleAuthViewBuilder;
import com.hivemq.mqtt.client2.internal.message.connect.MqttConnect;
import com.hivemq.mqtt.client2.internal.message.connect.MqttConnectRestrictions;
import com.hivemq.mqtt.client2.internal.message.connect.MqttConnectRestrictionsBuilder;
import com.hivemq.mqtt.client2.internal.message.publish.MqttWillPublish;
import com.hivemq.mqtt.client2.internal.message.publish.mqtt3.Mqtt3PublishView;
import com.hivemq.mqtt.client2.internal.message.publish.mqtt3.Mqtt3PublishViewBuilder;
import com.hivemq.mqtt.client2.internal.util.Checks;
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import com.hivemq.mqtt.client2.mqtt3.message.auth.Mqtt3SimpleAuth;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3ConnectBuilder;
import com.hivemq.mqtt.client2.mqtt3.message.connect.Mqtt3ConnectRestrictions;
import com.hivemq.mqtt.client2.mqtt3.message.publish.Mqtt3Publish;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -42,6 +45,7 @@ public abstract class Mqtt3ConnectViewBuilder<B extends Mqtt3ConnectViewBuilder<
private @Range(from = 0, to = UnsignedDataTypes.UNSIGNED_SHORT_MAX_VALUE) int keepAliveSeconds =
Mqtt3ConnectView.DEFAULT_KEEP_ALIVE;
private boolean cleanSession = Mqtt3ConnectView.DEFAULT_CLEAN_SESSION;
private @NotNull MqttConnectRestrictions restrictions = MqttConnectRestrictions.DEFAULT;
private @Nullable MqttSimpleAuth simpleAuth;
private @Nullable MqttWillPublish willPublish;

Expand All @@ -51,6 +55,7 @@ public abstract class Mqtt3ConnectViewBuilder<B extends Mqtt3ConnectViewBuilder<
final MqttConnect delegate = connect.getDelegate();
keepAliveSeconds = delegate.getKeepAlive();
cleanSession = delegate.isCleanStart();
restrictions = delegate.getRestrictions();
simpleAuth = delegate.getRawSimpleAuth();
willPublish = delegate.getRawWillPublish();
}
Expand All @@ -72,6 +77,15 @@ public abstract class Mqtt3ConnectViewBuilder<B extends Mqtt3ConnectViewBuilder<
return self();
}

public @NotNull B restrictions(final @Nullable Mqtt3ConnectRestrictions restrictions) {
this.restrictions = Checks.notImplemented(restrictions, MqttConnectRestrictions.class, "Connect restrictions");
return self();
}

public MqttConnectRestrictionsBuilder.@NotNull Nested<B> restrictionsWith() {
return new MqttConnectRestrictionsBuilder.Nested<>(restrictions, this::restrictions);
}

public @NotNull B simpleAuth(final @Nullable Mqtt3SimpleAuth simpleAuth) {
this.simpleAuth = (simpleAuth == null) ? null :
Checks.notImplemented(simpleAuth, Mqtt3SimpleAuthView.class, "Simple auth").getDelegate();
Expand All @@ -93,7 +107,7 @@ public abstract class Mqtt3ConnectViewBuilder<B extends Mqtt3ConnectViewBuilder<
}

public @NotNull Mqtt3ConnectView build() {
return Mqtt3ConnectView.of(keepAliveSeconds, cleanSession, simpleAuth, willPublish);
return Mqtt3ConnectView.of(keepAliveSeconds, cleanSession, restrictions, simpleAuth, willPublish);
}

public static class Default extends Mqtt3ConnectViewBuilder<Default> implements Mqtt3ConnectBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public interface Mqtt3Connect extends Mqtt3Message {
*/
boolean isCleanSession();

/**
* @return the restrictions set from the client.
* @since 1.3
*/
@NotNull Mqtt3ConnectRestrictions getRestrictions();

/**
* @return the optional simple authentication and/or authorization related data of this Connect message.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ public interface Mqtt3ConnectBuilderBase<B extends Mqtt3ConnectBuilderBase<B>> {
@CheckReturnValue
@NotNull B cleanSession(boolean cleanSession);

/**
* Sets the {@link Mqtt3Connect#getRestrictions() restrictions} from the client.
*
* @param restrictions the restrictions from the client.
* @return the builder.
* @since 1.3
*/
@CheckReturnValue
@NotNull B restrictions(@NotNull Mqtt3ConnectRestrictions restrictions);

/**
* Fluent counterpart of {@link #restrictions(Mqtt3ConnectRestrictions)}.
* <p>
* Calling {@link Mqtt3ConnectRestrictionsBuilder.Nested#applyRestrictions()} on the returned builder has the effect
* of {@link Mqtt3ConnectRestrictions#extend() extending} the current restrictions.
*
* @return the fluent builder for the restrictions.
* @see #restrictions(Mqtt3ConnectRestrictions)
* @since 1.3
*/
@CheckReturnValue
Mqtt3ConnectRestrictionsBuilder.@NotNull Nested<? extends B> restrictionsWith();

/**
* Sets the optional {@link Mqtt3Connect#getSimpleAuth() simple authentication and/or authorization related data}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2018-present HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hivemq.mqtt.client2.mqtt3.message.connect;

import com.hivemq.mqtt.client2.internal.datatypes.MqttVariableByteInteger;
import com.hivemq.mqtt.client2.internal.message.connect.MqttConnectRestrictionsBuilder;
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;

/**
* Restrictions applied by the client in an {@link Mqtt3Connect MQTT 3 Connect message}.
*
* @author Yannick Weber
* @since 1.3
*/
@ApiStatus.NonExtendable
public interface Mqtt3ConnectRestrictions {

/**
* Creates a builder for Connect restrictions.
*
* @return the created builder.
*/
static @NotNull Mqtt3ConnectRestrictionsBuilder builder() {
return new MqttConnectRestrictionsBuilder.Default();
}

/**
* Returns the maximum amount of not acknowledged publishes with QoS 1 or 2 the client sends to the server
* concurrently. The default is
* {@link com.hivemq.mqtt.client2.mqtt5.message.connect.Mqtt5ConnectRestrictions#DEFAULT_SEND_MAXIMUM
* DEFAULT_SEND_MAXIMUM}.
*
* @return the maximum amount of not acknowledged publishes with QoS 1 or 2 the client sends to the server
* concurrently.
*/
@Range(from = 1, to = UnsignedDataTypes.UNSIGNED_SHORT_MAX_VALUE) int getSendMaximum();

/**
* Returns the maximum packet size the client sends to the server. The default is
* {@link com.hivemq.mqtt.client2.mqtt5.message.connect.Mqtt5ConnectRestrictions#DEFAULT_SEND_MAXIMUM_PACKET_SIZE
* DEFAULT_SEND_MAXIMUM_PACKET_SIZE}.
*
* @return the maximum packet size the client sends to the server.
*/
@Range(from = 1, to = MqttVariableByteInteger.MAXIMUM_PACKET_SIZE_LIMIT) int getSendMaximumPacketSize();

/**
* Creates a builder for extending this Connect restrictions.
*
* @return the created builder.
*/
@NotNull Mqtt3ConnectRestrictionsBuilder extend();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2018-present HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hivemq.mqtt.client2.mqtt3.message.connect;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.CheckReturnValue;
import org.jetbrains.annotations.NotNull;

/**
* Builder for {@link Mqtt3ConnectRestrictions}.
*
* @author Yannick Weber
* @since 1.3
*/
@ApiStatus.NonExtendable
public interface Mqtt3ConnectRestrictionsBuilder
extends Mqtt3ConnectRestrictionsBuilderBase<Mqtt3ConnectRestrictionsBuilder> {

/**
* Builds the {@link Mqtt3ConnectRestrictions}.
*
* @return the built {@link Mqtt3ConnectRestrictions}.
*/
@CheckReturnValue
@NotNull Mqtt3ConnectRestrictions build();

/**
* Builder for {@link Mqtt3ConnectRestrictions} that are applied to a parent.
*
* @param <P> the type of the result when the built {@link Mqtt3ConnectRestrictions} are applied to the parent.
*/
@ApiStatus.NonExtendable
interface Nested<P> extends Mqtt3ConnectRestrictionsBuilderBase<Nested<P>> {

/**
* Builds the {@link Mqtt3ConnectRestrictions} and applies them to the parent.
*
* @return the result when the built {@link Mqtt3ConnectRestrictions} are applied to the parent.
*/
@NotNull P applyRestrictions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2018-present HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hivemq.mqtt.client2.mqtt3.message.connect;

import com.hivemq.mqtt.client2.internal.datatypes.MqttVariableByteInteger;
import com.hivemq.mqtt.client2.internal.util.UnsignedDataTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.CheckReturnValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;

/**
* Builder base for {@link Mqtt3ConnectRestrictions}.
*
* @param <B> the type of the builder.
* @author Yannick Weber
* @since 1.3
*/
@ApiStatus.NonExtendable
public interface Mqtt3ConnectRestrictionsBuilderBase<B extends Mqtt3ConnectRestrictionsBuilderBase<B>> {

/**
* Sets the {@link Mqtt3ConnectRestrictions#getSendMaximum() send maximum}.
* <p>
* The value must not be zero and must be in the range of an unsigned short: [1, 65_535].
*
* @param receiveMaximum the send maximum.
* @return the builder.
*/
@CheckReturnValue
@NotNull B sendMaximum(@Range(from = 1, to = UnsignedDataTypes.UNSIGNED_SHORT_MAX_VALUE) int receiveMaximum);

/**
* Sets the {@link Mqtt3ConnectRestrictions#getSendMaximumPacketSize() maximum packet size for sending}.
* <p>
* The value must not be zero and in the range: [1, 268_435_460].
*
* @param maximumPacketSize the maximum packet size for sending.
* @return the builder.
*/
@CheckReturnValue
@NotNull B sendMaximumPacketSize(
@Range(from = 1, to = MqttVariableByteInteger.MAXIMUM_PACKET_SIZE_LIMIT) int maximumPacketSize);
}

0 comments on commit ef530d6

Please sign in to comment.