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