View Javadoc

1   /*
2    * Copyright 2012 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 org.jboss.netty.channel.socket.http;
17  
18  import org.jboss.netty.buffer.ChannelBufferFactory;
19  import org.jboss.netty.channel.ChannelConfig;
20  import org.jboss.netty.channel.ChannelPipelineFactory;
21  import org.jboss.netty.channel.socket.SocketChannel;
22  import org.jboss.netty.channel.socket.SocketChannelConfig;
23  import org.jboss.netty.util.internal.ConversionUtil;
24  
25  import javax.net.ssl.SSLContext;
26  import javax.net.ssl.SSLEngine;
27  import javax.net.ssl.SSLSession;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  
31  /**
32   * The {@link ChannelConfig} of a client-side HTTP tunneling
33   * {@link SocketChannel}.  A {@link SocketChannel} created by
34   * {@link HttpTunnelingClientSocketChannelFactory} will return an instance of
35   * this configuration type for {@link SocketChannel#getConfig()}.
36   *
37   * <h3>Available options</h3>
38   *
39   * In addition to the options provided by {@link SocketChannelConfig},
40   * {@link HttpTunnelingSocketChannelConfig} allows the following options in
41   * the option map:
42   *
43   * <table border="1" cellspacing="0" cellpadding="6">
44   * <tr>
45   * <th>Name</th><th>Associated setter method</th>
46   * </tr><tr>
47   * <td>{@code "sslContext"}</td><td>{@link #setSslContext(SSLContext)}</td>
48   * </tr><tr>
49   * <td>{@code "enabledSslCiperSuites"}</td><td>{@link #setEnabledSslCipherSuites(String[])}</td>
50   * </tr><tr>
51   * <td>{@code "enabledSslProtocols"}</td><td>{@link #setEnabledSslProtocols(String[])}</td>
52   * </tr><tr>
53   * <td>{@code "enableSslSessionCreation"}</td><td>{@link #setEnableSslSessionCreation(boolean)}</td>
54   * </tr>
55   * </table>
56   * @apiviz.landmark
57   */
58  public final class HttpTunnelingSocketChannelConfig implements SocketChannelConfig {
59  
60      private final HttpTunnelingClientSocketChannel channel;
61      private volatile String serverName;
62      private volatile String serverPath = "/netty-tunnel";
63      private volatile SSLContext sslContext;
64      private volatile String[] enabledSslCipherSuites;
65      private volatile String[] enabledSslProtocols;
66      private volatile boolean enableSslSessionCreation = true;
67  
68      /**
69       * Creates a new instance.
70       */
71      HttpTunnelingSocketChannelConfig(HttpTunnelingClientSocketChannel channel) {
72          this.channel = channel;
73      }
74  
75      /**
76       * Returns the host name of the HTTP server.  If {@code null}, the
77       * {@code "Host"} header is not sent by the HTTP tunneling client.
78       */
79      public String getServerName() {
80          return serverName;
81      }
82  
83      /**
84       * Sets the host name of the HTTP server.  If {@code null}, the
85       * {@code "Host"} header is not sent by the HTTP tunneling client.
86       */
87      public void setServerName(String serverName) {
88          this.serverName = serverName;
89      }
90  
91      /**
92       * Returns the path where the {@link HttpTunnelingServlet} is mapped to.
93       * The default value is {@code "/netty-tunnel"}.
94       */
95      public String getServerPath() {
96          return serverPath;
97      }
98  
99      /**
100      * Sets the path where the {@link HttpTunnelingServlet} is mapped to.
101      * The default value is {@code "/netty-tunnel"}.
102      */
103     public void setServerPath(String serverPath) {
104         if (serverPath == null) {
105             throw new NullPointerException("serverPath");
106         }
107         this.serverPath = serverPath;
108     }
109 
110     /**
111      * Returns the {@link SSLContext} which is used to establish an HTTPS
112      * connection.  If {@code null}, a plain-text HTTP connection is established.
113      */
114     public SSLContext getSslContext() {
115         return sslContext;
116     }
117 
118     /**
119      * Sets the {@link SSLContext} which is used to establish an HTTPS connection.
120      * If {@code null}, a plain-text HTTP connection is established.
121      */
122     public void setSslContext(SSLContext sslContext) {
123         this.sslContext = sslContext;
124     }
125 
126     /**
127      * Returns the cipher suites enabled for use on an {@link SSLEngine}.
128      * If {@code null}, the default value will be used.
129      *
130      * @see SSLEngine#getEnabledCipherSuites()
131      */
132     public String[] getEnabledSslCipherSuites() {
133         String[] suites = enabledSslCipherSuites;
134         if (suites == null) {
135             return null;
136         } else {
137             return suites.clone();
138         }
139     }
140 
141     /**
142      * Sets the cipher suites enabled for use on an {@link SSLEngine}.
143      * If {@code null}, the default value will be used.
144      *
145      * @see SSLEngine#setEnabledCipherSuites(String[])
146      */
147     public void setEnabledSslCipherSuites(String[] suites) {
148         if (suites == null) {
149             enabledSslCipherSuites = null;
150         } else {
151             enabledSslCipherSuites = suites.clone();
152         }
153     }
154 
155     /**
156      * Returns the protocol versions enabled for use on an {@link SSLEngine}.
157      *
158      * @see SSLEngine#getEnabledProtocols()
159      */
160     public String[] getEnabledSslProtocols() {
161         String[] protocols = enabledSslProtocols;
162         if (protocols == null) {
163             return null;
164         } else {
165             return protocols.clone();
166         }
167     }
168 
169     /**
170      * Sets the protocol versions enabled for use on an {@link SSLEngine}.
171      *
172      * @see SSLEngine#setEnabledProtocols(String[])
173      */
174     public void setEnabledSslProtocols(String[] protocols) {
175         if (protocols == null) {
176             enabledSslProtocols = null;
177         } else {
178             enabledSslProtocols = protocols.clone();
179         }
180     }
181 
182     /**
183      * Returns {@code true} if new {@link SSLSession}s may be established by
184      * an {@link SSLEngine}.
185      *
186      * @see SSLEngine#getEnableSessionCreation()
187      */
188     public boolean isEnableSslSessionCreation() {
189         return enableSslSessionCreation;
190     }
191 
192     /**
193      * Sets whether new {@link SSLSession}s may be established by an
194      * {@link SSLEngine}.
195      *
196      * @see SSLEngine#setEnableSessionCreation(boolean)
197      */
198     public void setEnableSslSessionCreation(boolean flag) {
199         enableSslSessionCreation = flag;
200     }
201 
202     public void setOptions(Map<String, Object> options) {
203         for (Entry<String, Object> e: options.entrySet()) {
204             setOption(e.getKey(), e.getValue());
205         }
206     }
207 
208     public boolean setOption(String key, Object value) {
209         if (channel.realChannel.getConfig().setOption(key, value)) {
210             return true;
211         }
212 
213         if ("serverName".equals(key)) {
214             setServerName(String.valueOf(value));
215         } else if ("serverPath".equals(key)) {
216             setServerPath(String.valueOf(value));
217         } else if ("sslContext".equals(key)) {
218             setSslContext((SSLContext) value);
219         } else if ("enabledSslCipherSuites".equals(key)) {
220             setEnabledSslCipherSuites(ConversionUtil.toStringArray(value));
221         } else if ("enabledSslProtocols".equals(key)) {
222             setEnabledSslProtocols(ConversionUtil.toStringArray(value));
223         } else if ("enableSslSessionCreation".equals(key)) {
224             setEnableSslSessionCreation(ConversionUtil.toBoolean(value));
225         } else {
226             return false;
227         }
228 
229         return true;
230     }
231 
232     public int getReceiveBufferSize() {
233         return channel.realChannel.getConfig().getReceiveBufferSize();
234     }
235 
236     public int getSendBufferSize() {
237         return channel.realChannel.getConfig().getSendBufferSize();
238     }
239 
240     public int getSoLinger() {
241         return channel.realChannel.getConfig().getSoLinger();
242     }
243 
244     public int getTrafficClass() {
245         return channel.realChannel.getConfig().getTrafficClass();
246     }
247 
248     public boolean isKeepAlive() {
249         return channel.realChannel.getConfig().isKeepAlive();
250     }
251 
252     public boolean isReuseAddress() {
253         return channel.realChannel.getConfig().isReuseAddress();
254     }
255 
256     public boolean isTcpNoDelay() {
257         return channel.realChannel.getConfig().isTcpNoDelay();
258     }
259 
260     public void setKeepAlive(boolean keepAlive) {
261         channel.realChannel.getConfig().setKeepAlive(keepAlive);
262     }
263 
264     public void setPerformancePreferences(
265           int connectionTime, int latency, int bandwidth) {
266         channel.realChannel.getConfig().setPerformancePreferences(connectionTime, latency, bandwidth);
267     }
268 
269     public void setReceiveBufferSize(int receiveBufferSize) {
270         channel.realChannel.getConfig().setReceiveBufferSize(receiveBufferSize);
271     }
272 
273     public void setReuseAddress(boolean reuseAddress) {
274         channel.realChannel.getConfig().setReuseAddress(reuseAddress);
275     }
276 
277     public void setSendBufferSize(int sendBufferSize) {
278         channel.realChannel.getConfig().setSendBufferSize(sendBufferSize);
279     }
280 
281     public void setSoLinger(int soLinger) {
282         channel.realChannel.getConfig().setSoLinger(soLinger);
283     }
284 
285     public void setTcpNoDelay(boolean tcpNoDelay) {
286         channel.realChannel.getConfig().setTcpNoDelay(tcpNoDelay);
287     }
288 
289     public void setTrafficClass(int trafficClass) {
290         channel.realChannel.getConfig().setTrafficClass(trafficClass);
291     }
292 
293     public ChannelBufferFactory getBufferFactory() {
294         return channel.realChannel.getConfig().getBufferFactory();
295     }
296 
297     public int getConnectTimeoutMillis() {
298         return channel.realChannel.getConfig().getConnectTimeoutMillis();
299     }
300 
301     public ChannelPipelineFactory getPipelineFactory() {
302         return channel.realChannel.getConfig().getPipelineFactory();
303     }
304 
305     public void setBufferFactory(ChannelBufferFactory bufferFactory) {
306         channel.realChannel.getConfig().setBufferFactory(bufferFactory);
307     }
308 
309     public void setConnectTimeoutMillis(int connectTimeoutMillis) {
310         channel.realChannel.getConfig().setConnectTimeoutMillis(connectTimeoutMillis);
311     }
312 
313     public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
314         channel.realChannel.getConfig().setPipelineFactory(pipelineFactory);
315     }
316 }