View Javadoc

1   /*
2    *   @(#) $Id: Main.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.examples.httpserver;
20  
21  import org.apache.mina.common.TransportType;
22  import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
23  import org.apache.mina.io.IoAcceptor;
24  import org.apache.mina.io.filter.SSLFilter;
25  import org.apache.mina.registry.Service;
26  import org.apache.mina.registry.ServiceRegistry;
27  import org.apache.mina.registry.SimpleServiceRegistry;
28  
29  /***
30   * (<b>Entry point</b>) HTTP server
31   * 
32   * @author The Apache Directory Project (dev@directory.apache.org)
33   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
34   */
35  public class Main
36  {
37      /*** Choose your favorite port number. */
38      private static final int PORT = 8081;
39      
40      private static final boolean USE_SSL = true;
41  
42      public static void main( String[] args ) throws Exception
43      {
44          ServiceRegistry registry = new SimpleServiceRegistry();
45          
46          // Add SSL filter if SSL is enabled.
47          if( USE_SSL )
48          {
49              addSSLSupport( registry );
50          }
51  
52          // Bind
53          Service service = new Service( "http", TransportType.SOCKET, PORT );
54          registry.bind( service, new HttpProtocolHandler() );
55  
56          System.out.println( "Listening on port " + PORT );
57      }
58  
59  
60      private static void addSSLSupport( ServiceRegistry registry )
61          throws Exception
62      {
63          System.out.println( "SSL is enabled." );
64          SSLFilter sslFilter =
65              new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
66          IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
67          acceptor.getFilterChain().addLast( "sslFilter", sslFilter );
68      }
69  }