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  
20  /**
21   * The {@link JdkApplicationProtocolNegotiator} to use if you need NPN and are using {@link SslProvider#JDK}.
22   */
23  public final class JdkNpnApplicationProtocolNegotiator extends JdkBaseApplicationProtocolNegotiator {
24      private static final SslEngineWrapperFactory NPN_WRAPPER = new SslEngineWrapperFactory() {
25          {
26              if (!JdkNpnSslEngine.isAvailable()) {
27                  throw new RuntimeException("NPN unsupported. Is your classpatch configured correctly?"
28                          + " See http://www.eclipse.org/jetty/documentation/current/npn-chapter.html#npn-starting");
29              }
30          }
31  
32          @Override
33          public SSLEngine wrapSslEngine(SSLEngine engine, JdkApplicationProtocolNegotiator applicationNegotiator,
34                  boolean isServer) {
35              return new JdkNpnSslEngine(engine, applicationNegotiator, isServer);
36          }
37      };
38  
39      /**
40       * Create a new instance.
41       * @param protocols The order of iteration determines the preference of support for protocols.
42       */
43      public JdkNpnApplicationProtocolNegotiator(Iterable<String> protocols) {
44          this(false, protocols);
45      }
46  
47      /**
48       * Create a new instance.
49       * @param protocols The order of iteration determines the preference of support for protocols.
50       */
51      public JdkNpnApplicationProtocolNegotiator(String... protocols) {
52          this(false, protocols);
53      }
54  
55      /**
56       * Create a new instance.
57       * @param failIfNoCommonProtocols Fail with a fatal alert if not common protocols are detected.
58       * @param protocols The order of iteration determines the preference of support for protocols.
59       */
60      public JdkNpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, Iterable<String> protocols) {
61          this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
62      }
63  
64      /**
65       * Create a new instance.
66       * @param failIfNoCommonProtocols Fail with a fatal alert if not common protocols are detected.
67       * @param protocols The order of iteration determines the preference of support for protocols.
68       */
69      public JdkNpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, String... protocols) {
70          this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
71      }
72  
73      /**
74       * Create a new instance.
75       * @param clientFailIfNoCommonProtocols Client side fail with a fatal alert if not common protocols are detected.
76       * @param serverFailIfNoCommonProtocols Server side fail with a fatal alert if not common protocols are detected.
77       * @param protocols The order of iteration determines the preference of support for protocols.
78       */
79      public JdkNpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
80              boolean serverFailIfNoCommonProtocols, Iterable<String> protocols) {
81          this(clientFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
82                  serverFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
83                  protocols);
84      }
85  
86      /**
87       * Create a new instance.
88       * @param clientFailIfNoCommonProtocols Client side fail with a fatal alert if not common protocols are detected.
89       * @param serverFailIfNoCommonProtocols Server side fail with a fatal alert if not common protocols are detected.
90       * @param protocols The order of iteration determines the preference of support for protocols.
91       */
92      public JdkNpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
93              boolean serverFailIfNoCommonProtocols, String... protocols) {
94          this(clientFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
95                  serverFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
96                  protocols);
97      }
98  
99      /**
100      * Create a new instance.
101      * @param selectorFactory The factory which provides classes responsible for selecting the protocol.
102      * @param listenerFactory The factory which provides to be notified of which protocol was selected.
103      * @param protocols The order of iteration determines the preference of support for protocols.
104      */
105     public JdkNpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
106             ProtocolSelectionListenerFactory listenerFactory, Iterable<String> protocols) {
107         super(NPN_WRAPPER, selectorFactory, listenerFactory, protocols);
108     }
109 
110     /**
111      * Create a new instance.
112      * @param selectorFactory The factory which provides classes responsible for selecting the protocol.
113      * @param listenerFactory The factory which provides to be notified of which protocol was selected.
114      * @param protocols The order of iteration determines the preference of support for protocols.
115      */
116     public JdkNpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
117             ProtocolSelectionListenerFactory listenerFactory, String... protocols) {
118         super(NPN_WRAPPER, selectorFactory, listenerFactory, protocols);
119     }
120 }