View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.websocket.common.extensions;
20  
21  import org.eclipse.jetty.io.ByteBufferPool;
22  import org.eclipse.jetty.util.StringUtil;
23  import org.eclipse.jetty.websocket.api.WebSocketException;
24  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
25  import org.eclipse.jetty.websocket.api.extensions.Extension;
26  import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
27  import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
28  import org.eclipse.jetty.websocket.common.extensions.compress.FrameCompressionExtension;
29  import org.eclipse.jetty.websocket.common.extensions.compress.MessageCompressionExtension;
30  import org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension;
31  import org.eclipse.jetty.websocket.common.extensions.identity.IdentityExtension;
32  
33  public class WebSocketExtensionFactory extends ExtensionFactory
34  {
35      private WebSocketPolicy policy;
36      private ByteBufferPool bufferPool;
37  
38      public WebSocketExtensionFactory(WebSocketPolicy policy, ByteBufferPool bufferPool)
39      {
40          super();
41          this.policy = policy;
42          this.bufferPool = bufferPool;
43  
44          register("identity",IdentityExtension.class);
45          register("fragment",FragmentExtension.class);
46          register("x-webkit-deflate-frame",FrameCompressionExtension.class);
47          register("permessage-compress",MessageCompressionExtension.class);
48      }
49  
50      @Override
51      public Extension newInstance(ExtensionConfig config)
52      {
53          if (config == null)
54          {
55              return null;
56          }
57  
58          String name = config.getName();
59          if (StringUtil.isBlank(name))
60          {
61              return null;
62          }
63  
64          Class<? extends Extension> extClass = getExtension(name);
65          if (extClass == null)
66          {
67              return null;
68          }
69  
70          try
71          {
72              Extension ext = extClass.newInstance();
73              if (ext instanceof AbstractExtension)
74              {
75                  AbstractExtension aext = (AbstractExtension)ext;
76                  aext.setConfig(config);
77                  aext.setPolicy(policy);
78                  aext.setBufferPool(bufferPool);
79              }
80              return ext;
81          }
82          catch (InstantiationException | IllegalAccessException e)
83          {
84              throw new WebSocketException("Cannot instantiate extension: " + extClass,e);
85          }
86      }
87  }