View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import javax.net.ssl.SSLEngine;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import static io.netty.handler.ssl.ApplicationProtocolUtil.*;
23  import static io.netty.util.internal.ObjectUtil.*;
24  
25  /**
26   * Provides an {@link SSLEngine} agnostic way to configure a {@link ApplicationProtocolNegotiator}.
27   */
28  public final class ApplicationProtocolConfig {
29  
30      /**
31       * The configuration that disables application protocol negotiation.
32       */
33      public static final ApplicationProtocolConfig DISABLED = new ApplicationProtocolConfig();
34  
35      private final List<String> supportedProtocols;
36      private final Protocol protocol;
37      private final SelectorFailureBehavior selectorBehavior;
38      private final SelectedListenerFailureBehavior selectedBehavior;
39  
40      /**
41       * Create a new instance.
42       * @param protocol The application protocol functionality to use.
43       * @param selectorBehavior How the peer selecting the protocol should behave.
44       * @param selectedBehavior How the peer being notified of the selected protocol should behave.
45       * @param supportedProtocols The order of iteration determines the preference of support for protocols.
46       */
47      public ApplicationProtocolConfig(Protocol protocol, SelectorFailureBehavior selectorBehavior,
48              SelectedListenerFailureBehavior selectedBehavior, Iterable<String> supportedProtocols) {
49          this(protocol, selectorBehavior, selectedBehavior, toList(supportedProtocols));
50      }
51  
52      /**
53       * Create a new instance.
54       * @param protocol The application protocol functionality to use.
55       * @param selectorBehavior How the peer selecting the protocol should behave.
56       * @param selectedBehavior How the peer being notified of the selected protocol should behave.
57       * @param supportedProtocols The order of iteration determines the preference of support for protocols.
58       */
59      public ApplicationProtocolConfig(Protocol protocol, SelectorFailureBehavior selectorBehavior,
60              SelectedListenerFailureBehavior selectedBehavior, String... supportedProtocols) {
61          this(protocol, selectorBehavior, selectedBehavior, toList(supportedProtocols));
62      }
63  
64      /**
65       * Create a new instance.
66       * @param protocol The application protocol functionality to use.
67       * @param selectorBehavior How the peer selecting the protocol should behave.
68       * @param selectedBehavior How the peer being notified of the selected protocol should behave.
69       * @param supportedProtocols The order of iteration determines the preference of support for protocols.
70       */
71      private ApplicationProtocolConfig(
72              Protocol protocol, SelectorFailureBehavior selectorBehavior,
73              SelectedListenerFailureBehavior selectedBehavior, List<String> supportedProtocols) {
74          this.supportedProtocols = Collections.unmodifiableList(checkNotNull(supportedProtocols, "supportedProtocols"));
75          this.protocol = checkNotNull(protocol, "protocol");
76          this.selectorBehavior = checkNotNull(selectorBehavior, "selectorBehavior");
77          this.selectedBehavior = checkNotNull(selectedBehavior, "selectedBehavior");
78  
79          if (protocol == Protocol.NONE) {
80              throw new IllegalArgumentException("protocol (" + Protocol.NONE + ") must not be " + Protocol.NONE + '.');
81          }
82      }
83  
84      /**
85       * A special constructor that is used to instantiate {@link #DISABLED}.
86       */
87      private ApplicationProtocolConfig() {
88          supportedProtocols = Collections.emptyList();
89          protocol = Protocol.NONE;
90          selectorBehavior = SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL;
91          selectedBehavior = SelectedListenerFailureBehavior.ACCEPT;
92      }
93  
94      /**
95       * Defines which application level protocol negotiation to use.
96       */
97      public enum Protocol {
98          NONE, NPN, ALPN, NPN_AND_ALPN
99      }
100 
101     /**
102      * Defines the most common behaviors for the peer that selects the application protocol.
103      */
104     public enum SelectorFailureBehavior {
105         FATAL_ALERT, NO_ADVERTISE, CHOOSE_MY_LAST_PROTOCOL
106     }
107 
108     /**
109      * Defines the most common behaviors for the peer which is notified of the selected protocol.
110      */
111     public enum SelectedListenerFailureBehavior {
112         ACCEPT, FATAL_ALERT, CHOOSE_MY_LAST_PROTOCOL
113     }
114 
115     /**
116      * The application level protocols supported.
117      */
118     public List<String> supportedProtocols() {
119         return supportedProtocols;
120     }
121 
122     /**
123      * Get which application level protocol negotiation to use.
124      */
125     public Protocol protocol() {
126         return protocol;
127     }
128 
129     /**
130      * Get the desired behavior for the peer who selects the application protocol.
131      */
132     public SelectorFailureBehavior selectorFailureBehavior() {
133         return selectorBehavior;
134     }
135 
136     /**
137      * Get the desired behavior for the peer who is notified of the selected protocol.
138      */
139     public SelectedListenerFailureBehavior selectedListenerFailureBehavior() {
140         return selectedBehavior;
141     }
142 }