• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package software.amazon.awssdk.crt.mqtt5;
2 
3 import java.util.Map;
4 import java.util.function.Function;
5 import java.util.stream.Collectors;
6 import java.util.stream.Stream;
7 
8 /**
9  * Configuration for all client topic aliasing behavior.
10  */
11 public class TopicAliasingOptions {
12 
13     private OutboundTopicAliasBehaviorType outboundBehavior;
14     private Integer outboundCacheMaxSize;
15     private InboundTopicAliasBehaviorType inboundBehavior;
16     private Integer inboundCacheMaxSize;
17 
18     /**
19      * Default constructor
20      */
TopicAliasingOptions()21     public TopicAliasingOptions() {
22         this.outboundBehavior = OutboundTopicAliasBehaviorType.Default;
23         this.outboundCacheMaxSize = 0;
24         this.inboundBehavior = InboundTopicAliasBehaviorType.Default;
25         this.inboundCacheMaxSize = 0;
26     }
27 
28     /**
29      * Controls what kind of outbound topic aliasing behavior the client should attempt to use.
30      *
31      * If topic aliasing is not supported by the server, this setting has no effect and any attempts to directly
32      * manipulate the topic alias id in outbound publishes will be ignored.
33      *
34      * By default, outbound topic aliasing is disabled.
35      *
36      * @param behavior outbound topic alias behavior to use
37      *
38      * @return the topic aliasing options object
39      */
withOutboundBehavior(OutboundTopicAliasBehaviorType behavior)40     public TopicAliasingOptions withOutboundBehavior(OutboundTopicAliasBehaviorType behavior) {
41         this.outboundBehavior = behavior;
42         return this;
43     }
44 
45     /**
46      * If outbound topic aliasing is set to LRU, this controls the maximum size of the cache.  If outbound topic
47      * aliasing is set to LRU and this is zero or undefined, a sensible default is used (25).  If outbound topic
48      * aliasing is not set to LRU, then this setting has no effect.
49      *
50      * The final size of the cache is determined by the minimum of this setting and the value of the
51      * topic_alias_maximum property of the received CONNACK.  If the received CONNACK does not have an explicit
52      * positive value for that field, outbound topic aliasing is disabled for the duration of that connection.
53      *
54      * @param size maximum size to use for the outbound alias cache
55      *
56      * @return the topic aliasing options object
57      */
withOutboundCacheMaxSize(int size)58     public TopicAliasingOptions withOutboundCacheMaxSize(int size) {
59         this.outboundCacheMaxSize = size;
60         return this;
61     }
62 
63     /**
64      * Controls whether or not the client allows the broker to use topic aliasing when sending publishes.  Even if
65      * inbound topic aliasing is enabled, it is up to the server to choose whether or not to use it.
66      *
67      * If left undefined, then inbound topic aliasing is disabled.
68      *
69      * @param behavior inbound topic alias behavior to use
70      *
71      * @return the topic aliasing options object
72      */
withInboundBehavior(InboundTopicAliasBehaviorType behavior)73     public TopicAliasingOptions withInboundBehavior(InboundTopicAliasBehaviorType behavior) {
74         this.inboundBehavior = behavior;
75         return this;
76     }
77 
78     /**
79      * If inbound topic aliasing is enabled, this will control the size of the inbound alias cache.  If inbound
80      * aliases are enabled and this is zero or undefined, then a sensible default will be used (25).  If inbound
81      * aliases are disabled, this setting has no effect.
82      *
83      * Behaviorally, this value overrides anything present in the topic_alias_maximum field of
84      * the CONNECT packet options.
85      *
86      * @param size maximum size to use for the inbound alias cache
87      *
88      * @return the topic aliasing options object
89      */
withInboundCacheMaxSize(int size)90     public TopicAliasingOptions withInboundCacheMaxSize(int size) {
91         this.inboundCacheMaxSize = size;
92         return this;
93     }
94 
95     /**
96      * An enumeration that controls how the client applies topic aliasing to outbound publish packets.
97      *
98      * Topic alias behavior is described in <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113">MQTT5 Topic Aliasing</a>
99      */
100     public enum OutboundTopicAliasBehaviorType {
101 
102         /**
103          * Maps to Disabled.  This keeps the client from being broken (by default) if the broker
104          * topic aliasing implementation has a problem.
105          */
106         Default(0),
107 
108         /**
109          * Outbound aliasing is the user's responsibility.  Client will cache and use
110          * previously-established aliases if they fall within the negotiated limits of the connection.
111          *
112          * The user must still always submit a full topic in their publishes because disconnections disrupt
113          * topic alias mappings unpredictably.  The client will properly use a requested alias when the most-recently-seen
114          * binding for a topic alias value matches the alias and topic in the publish packet.
115          */
116         Manual(1),
117 
118         /**
119          * (Recommended) The client will ignore any user-specified topic aliasing and instead use an LRU cache to drive
120          * alias usage.
121          */
122         LRU(2),
123 
124         /**
125          * Completely disable outbound topic aliasing.
126          */
127         Disabled(3);
128 
129         private int value;
130 
OutboundTopicAliasBehaviorType(int value)131         private OutboundTopicAliasBehaviorType(int value) {
132             this.value = value;
133         }
134 
135         /**
136          * @return The native enum integer value associated with this Java enum value
137          */
getValue()138         public int getValue() {
139             return this.value;
140         }
141 
142         /**
143          * Creates a Java OutboundTopicAliasBehaviorType enum value from a native integer value.
144          *
145          * @param value native integer value for the OutboundTopicAliasBehaviorType value
146          * @return a new OutboundTopicAliasBehaviorType value
147          */
getEnumValueFromInteger(int value)148         public static OutboundTopicAliasBehaviorType getEnumValueFromInteger(int value) {
149             OutboundTopicAliasBehaviorType enumValue = enumMapping.get(value);
150             if (enumValue != null) {
151                 return enumValue;
152             }
153             throw new RuntimeException("Illegal OutboundTopicAliasBehaviorType");
154         }
155 
buildEnumMapping()156         private static Map<Integer, OutboundTopicAliasBehaviorType> buildEnumMapping() {
157             return Stream.of(OutboundTopicAliasBehaviorType.values())
158                     .collect(Collectors.toMap(OutboundTopicAliasBehaviorType::getValue, Function.identity()));
159         }
160 
161         private static Map<Integer, OutboundTopicAliasBehaviorType> enumMapping = buildEnumMapping();
162     }
163 
164     /**
165      * An enumeration that controls whether or not the client allows the broker to send publishes that use topic
166      * aliasing.
167      *
168      * Topic alias behavior is described in https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113
169      */
170     public enum InboundTopicAliasBehaviorType {
171 
172         /**
173          * Maps to Disabled.  This keeps the client from being broken (by default) if the broker
174          * topic aliasing implementation has a problem.
175          */
176         Default(0),
177 
178         /**
179          * Allow the server to send PUBLISH packets to the client that use topic aliasing
180          */
181         Enabled(1),
182 
183         /**
184          * Forbid the server from sending PUBLISH packets to the client that use topic aliasing
185          */
186         Disabled(2);
187 
188         private int value;
189 
InboundTopicAliasBehaviorType(int value)190         private InboundTopicAliasBehaviorType(int value) {
191             this.value = value;
192         }
193 
194         /**
195          * @return The native enum integer value associated with this Java enum value
196          */
getValue()197         public int getValue() {
198             return this.value;
199         }
200 
201         /**
202          * Creates a Java InboundTopicAliasBehaviorType enum value from a native integer value.
203          *
204          * @param value native integer value for the InboundTopicAliasBehaviorType value
205          * @return a new InboundTopicAliasBehaviorType value
206          */
getEnumValueFromInteger(int value)207         public static InboundTopicAliasBehaviorType getEnumValueFromInteger(int value) {
208             InboundTopicAliasBehaviorType enumValue = enumMapping.get(value);
209             if (enumValue != null) {
210                 return enumValue;
211             }
212             throw new RuntimeException("Illegal InboundTopicAliasBehaviorType");
213         }
214 
buildEnumMapping()215         private static Map<Integer, InboundTopicAliasBehaviorType> buildEnumMapping() {
216             return Stream.of(InboundTopicAliasBehaviorType.values())
217                     .collect(Collectors.toMap(InboundTopicAliasBehaviorType::getValue, Function.identity()));
218         }
219 
220         private static Map<Integer, InboundTopicAliasBehaviorType> enumMapping = buildEnumMapping();
221     }
222 }