View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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   *
    58   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    59   * @author Andy Taylor (andy.taylor@jboss.org)
    60   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    61   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    62   *
    63   * @apiviz.landmark
    64   */
    65  public final class HttpTunnelingSocketChannelConfig implements SocketChannelConfig {
    66
    67      private final HttpTunnelingClientSocketChannel channel;
    68      private volatile String serverName;
    69      private volatile String serverPath = "/netty-tunnel";
    70      private volatile SSLContext sslContext;
    71      private volatile String[] enabledSslCipherSuites;
    72      private volatile String[] enabledSslProtocols;
    73      private volatile boolean enableSslSessionCreation = true;
    74
    75      /**
    76       * Creates a new instance.
    77       */
    78      HttpTunnelingSocketChannelConfig(HttpTunnelingClientSocketChannel channel) {
    79          this.channel = channel;
    80      }
    81
    82      /**
    83       * Returns the host name of the HTTP server.  If {@code null}, the
    84       * {@code "Host"} header is not sent by the HTTP tunneling client.
    85       */
    86      public String getServerName() {
    87          return serverName;
    88      }
    89
    90      /**
    91       * Sets the host name of the HTTP server.  If {@code null}, the
    92       * {@code "Host"} header is not sent by the HTTP tunneling client.
    93       */
    94      public void setServerName(String serverName) {
    95          this.serverName = serverName;
    96      }
    97
    98      /**
    99       * Returns the path where the {@link HttpTunnelingServlet} is mapped to.
    100      * The default value is {@code "/netty-tunnel"}.
    101      */
    102     public String getServerPath() {
    103         return serverPath;
    104     }
    105
    106     /**
    107      * Sets the path where the {@link HttpTunnelingServlet} is mapped to.
    108      * The default value is {@code "/netty-tunnel"}.
    109      */
    110     public void setServerPath(String serverPath) {
    111         if (serverPath == null) {
    112             throw new NullPointerException("serverPath");
    113         }
    114         this.serverPath = serverPath;
    115     }
    116
    117     /**
    118      * Returns the {@link SSLContext} which is used to establish an HTTPS
    119      * connection.  If {@code null}, a plain-text HTTP connection is established.
    120      */
    121     public SSLContext getSslContext() {
    122         return sslContext;
    123     }
    124
    125     /**
    126      * Sets the {@link SSLContext} which is used to establish an HTTPS connection.
    127      * If {@code null}, a plain-text HTTP connection is established.
    128      */
    129     public void setSslContext(SSLContext sslContext) {
    130         this.sslContext = sslContext;
    131     }
    132
    133     /**
    134      * Returns the cipher suites enabled for use on an {@link SSLEngine}.
    135      * If {@code null}, the default value will be used.
    136      *
    137      * @see SSLEngine#getEnabledCipherSuites()
    138      */
    139     public String[] getEnabledSslCipherSuites() {
    140         String[] suites = enabledSslCipherSuites;
    141         if (suites == null) {
    142             return null;
    143         } else {
    144             return suites.clone();
    145         }
    146     }
    147
    148     /**
    149      * Sets the cipher suites enabled for use on an {@link SSLEngine}.
    150      * If {@code null}, the default value will be used.
    151      *
    152      * @see SSLEngine#setEnabledCipherSuites(String[])
    153      */
    154     public void setEnabledSslCipherSuites(String[] suites) {
    155         if (suites == null) {
    156             enabledSslCipherSuites = null;
    157         } else {
    158             enabledSslCipherSuites = suites.clone();
    159         }
    160     }
    161
    162     /**
    163      * Returns the protocol versions enabled for use on an {@link SSLEngine}.
    164      *
    165      * @see SSLEngine#getEnabledProtocols()
    166      */
    167     public String[] getEnabledSslProtocols() {
    168         String[] protocols = enabledSslProtocols;
    169         if (protocols == null) {
    170             return null;
    171         } else {
    172             return protocols.clone();
    173         }
    174     }
    175
    176     /**
    177      * Sets the protocol versions enabled for use on an {@link SSLEngine}.
    178      *
    179      * @see SSLEngine#setEnabledProtocols(String[])
    180      */
    181     public void setEnabledSslProtocols(String[] protocols) {
    182         if (protocols == null) {
    183             enabledSslProtocols = null;
    184         } else {
    185             enabledSslProtocols = protocols.clone();
    186         }
    187     }
    188
    189     /**
    190      * Returns {@code true} if new {@link SSLSession}s may be established by
    191      * an {@link SSLEngine}.
    192      *
    193      * @see SSLEngine#getEnableSessionCreation()
    194      */
    195     public boolean isEnableSslSessionCreation() {
    196         return enableSslSessionCreation;
    197     }
    198
    199     /**
    200      * Sets whether new {@link SSLSession}s may be established by an
    201      * {@link SSLEngine}.
    202      *
    203      * @see SSLEngine#setEnableSessionCreation(boolean)
    204      */
    205     public void setEnableSslSessionCreation(boolean flag) {
    206         enableSslSessionCreation = flag;
    207     }
    208
    209     public void setOptions(Map<String, Object> options) {
    210         for (Entry<String, Object> e: options.entrySet()) {
    211             setOption(e.getKey(), e.getValue());
    212         }
    213     }
    214
    215     public boolean setOption(String key, Object value) {
    216         if (channel.realChannel.getConfig().setOption(key, value)) {
    217             return true;
    218         }
    219
    220         if (key.equals("serverName")){
    221             setServerName(String.valueOf(value));
    222         } else if (key.equals("serverPath")){
    223             setServerPath(String.valueOf(value));
    224         } else if (key.equals("sslContext")) {
    225             setSslContext((SSLContext) value);
    226         } else if (key.equals("enabledSslCipherSuites")){
    227             setEnabledSslCipherSuites(ConversionUtil.toStringArray(value));
    228         } else if (key.equals("enabledSslProtocols")){
    229             setEnabledSslProtocols(ConversionUtil.toStringArray(value));
    230         } else if (key.equals("enableSslSessionCreation")){
    231             setEnableSslSessionCreation(ConversionUtil.toBoolean(value));
    232         } else {
    233             return false;
    234         }
    235
    236         return true;
    237     }
    238
    239     public int getReceiveBufferSize() {
    240         return channel.realChannel.getConfig().getReceiveBufferSize();
    241     }
    242
    243     public int getSendBufferSize() {
    244         return channel.realChannel.getConfig().getSendBufferSize();
    245     }
    246
    247     public int getSoLinger() {
    248         return channel.realChannel.getConfig().getSoLinger();
    249     }
    250
    251     public int getTrafficClass() {
    252         return channel.realChannel.getConfig().getTrafficClass();
    253     }
    254
    255     public boolean isKeepAlive() {
    256         return channel.realChannel.getConfig().isKeepAlive();
    257     }
    258
    259     public boolean isReuseAddress() {
    260         return channel.realChannel.getConfig().isReuseAddress();
    261     }
    262
    263     public boolean isTcpNoDelay() {
    264         return channel.realChannel.getConfig().isTcpNoDelay();
    265     }
    266
    267     public void setKeepAlive(boolean keepAlive) {
    268         channel.realChannel.getConfig().setKeepAlive(keepAlive);
    269     }
    270
    271     public void setPerformancePreferences(
    272           int connectionTime, int latency, int bandwidth) {
    273         channel.realChannel.getConfig().setPerformancePreferences(connectionTime, latency, bandwidth);
    274     }
    275
    276     public void setReceiveBufferSize(int receiveBufferSize) {
    277         channel.realChannel.getConfig().setReceiveBufferSize(receiveBufferSize);
    278     }
    279
    280     public void setReuseAddress(boolean reuseAddress) {
    281         channel.realChannel.getConfig().setReuseAddress(reuseAddress);
    282     }
    283
    284     public void setSendBufferSize(int sendBufferSize) {
    285         channel.realChannel.getConfig().setSendBufferSize(sendBufferSize);
    286
    287     }
    288
    289     public void setSoLinger(int soLinger) {
    290         channel.realChannel.getConfig().setSoLinger(soLinger);
    291     }
    292
    293     public void setTcpNoDelay(boolean tcpNoDelay) {
    294         channel.realChannel.getConfig().setTcpNoDelay(tcpNoDelay);
    295     }
    296
    297     public void setTrafficClass(int trafficClass) {
    298         channel.realChannel.getConfig().setTrafficClass(trafficClass);
    299     }
    300
    301     public ChannelBufferFactory getBufferFactory() {
    302         return channel.realChannel.getConfig().getBufferFactory();
    303     }
    304
    305     public int getConnectTimeoutMillis() {
    306         return channel.realChannel.getConfig().getConnectTimeoutMillis();
    307     }
    308
    309     public ChannelPipelineFactory getPipelineFactory() {
    310         return channel.realChannel.getConfig().getPipelineFactory();
    311     }
    312
    313     public void setBufferFactory(ChannelBufferFactory bufferFactory) {
    314         channel.realChannel.getConfig().setBufferFactory(bufferFactory);
    315     }
    316
    317     public void setConnectTimeoutMillis(int connectTimeoutMillis) {
    318         channel.realChannel.getConfig().setConnectTimeoutMillis(connectTimeoutMillis);
    319     }
    320
    321     public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) {
    322         channel.realChannel.getConfig().setPipelineFactory(pipelineFactory);
    323     }
    324 }