View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server;
20  
21  import org.eclipse.jetty.io.AbstractConnection;
22  import org.eclipse.jetty.io.Connection;
23  import org.eclipse.jetty.io.EndPoint;
24  import org.eclipse.jetty.util.ArrayUtil;
25  import org.eclipse.jetty.util.component.ContainerLifeCycle;
26  import org.eclipse.jetty.util.ssl.SslContextFactory;
27  
28  public abstract class AbstractConnectionFactory extends ContainerLifeCycle implements ConnectionFactory
29  {
30      private final String _protocol;
31      private int _inputbufferSize = 8192;
32  
33      protected AbstractConnectionFactory(String protocol)
34      {
35          _protocol=protocol;
36      }
37  
38      @Override
39      public String getProtocol()
40      {
41          return _protocol;
42      }
43  
44      public int getInputBufferSize()
45      {
46          return _inputbufferSize;
47      }
48  
49      public void setInputBufferSize(int size)
50      {
51          _inputbufferSize=size;
52      }
53  
54      protected AbstractConnection configure(AbstractConnection connection, Connector connector, EndPoint endPoint)
55      {
56          connection.setInputBufferSize(getInputBufferSize());
57  
58          if (connector instanceof ContainerLifeCycle)
59          {
60              ContainerLifeCycle aggregate = (ContainerLifeCycle)connector;
61              for (Connection.Listener listener : aggregate.getBeans(Connection.Listener.class))
62                  connection.addListener(listener);
63          }
64          return connection;
65      }
66  
67      @Override
68      public String toString()
69      {
70          return String.format("%s@%x{%s}",this.getClass().getSimpleName(),hashCode(),getProtocol());
71      }
72  
73      public static ConnectionFactory[] getFactories(SslContextFactory sslContextFactory, ConnectionFactory... factories)
74      {
75          factories=ArrayUtil.removeNulls(factories);
76  
77          if (sslContextFactory==null)
78              return factories;
79  
80          for (ConnectionFactory factory : factories)
81          {
82              if (factory instanceof HttpConfiguration.ConnectionFactory)
83              {
84                  HttpConfiguration config = ((HttpConfiguration.ConnectionFactory)factory).getHttpConfiguration();
85                  if (config.getCustomizer(SecureRequestCustomizer.class)==null)
86                      config.addCustomizer(new SecureRequestCustomizer());
87              }
88          }
89          return ArrayUtil.prependToArray(new SslConnectionFactory(sslContextFactory,factories[0].getProtocol()),factories,ConnectionFactory.class);
90  
91      }
92  }