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.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          /* FIXME: Disabled due to bug report - http://bugs.eclipse.org/395444" target="alexandria_uri">http://bugs.eclipse.org/395444 
47           * register("x-webkit-deflate-frame",FrameCompressionExtension.class);
48           * register("permessage-compress",MessageCompressionExtension.class);
49           */
50      }
51  
52      @Override
53      public Extension newInstance(ExtensionConfig config)
54      {
55          if (config == null)
56          {
57              return null;
58          }
59  
60          String name = config.getName();
61          if (StringUtil.isBlank(name))
62          {
63              return null;
64          }
65  
66          Class<? extends Extension> extClass = getExtension(name);
67          if (extClass == null)
68          {
69              return null;
70          }
71  
72          try
73          {
74              Extension ext = extClass.newInstance();
75              if (ext instanceof AbstractExtension)
76              {
77                  AbstractExtension aext = (AbstractExtension)ext;
78                  aext.setConfig(config);
79                  aext.setPolicy(policy);
80                  aext.setBufferPool(bufferPool);
81              }
82              return ext;
83          }
84          catch (InstantiationException | IllegalAccessException e)
85          {
86              throw new WebSocketException("Cannot instantiate extension: " + extClass,e);
87          }
88      }
89  }