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.core.extensions;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.eclipse.jetty.io.ByteBufferPool;
28  import org.eclipse.jetty.util.StringUtil;
29  import org.eclipse.jetty.websocket.core.api.Extension;
30  import org.eclipse.jetty.websocket.core.api.ExtensionRegistry;
31  import org.eclipse.jetty.websocket.core.api.WebSocketException;
32  import org.eclipse.jetty.websocket.core.api.WebSocketPolicy;
33  import org.eclipse.jetty.websocket.core.extensions.compress.PerMessageCompressionExtension;
34  import org.eclipse.jetty.websocket.core.extensions.compress.WebkitDeflateFrameExtension;
35  import org.eclipse.jetty.websocket.core.extensions.fragment.FragmentExtension;
36  import org.eclipse.jetty.websocket.core.extensions.identity.IdentityExtension;
37  import org.eclipse.jetty.websocket.core.protocol.ExtensionConfig;
38  
39  public class WebSocketExtensionRegistry implements ExtensionRegistry
40  {
41      private Map<String, Class<? extends Extension>> registry;
42      private WebSocketPolicy policy;
43      private ByteBufferPool bufferPool;
44  
45      public WebSocketExtensionRegistry(WebSocketPolicy policy, ByteBufferPool bufferPool)
46      {
47          this.policy = policy;
48          this.bufferPool = bufferPool;
49  
50          this.registry = new HashMap<>();
51  
52          this.registry.put("identity",IdentityExtension.class);
53          this.registry.put("fragment",FragmentExtension.class);
54          this.registry.put("x-webkit-deflate-frame",WebkitDeflateFrameExtension.class);
55          this.registry.put("permessage-compress",PerMessageCompressionExtension.class);
56      }
57  
58      @Override
59      public boolean isAvailable(String name)
60      {
61          synchronized (registry)
62          {
63              return registry.containsKey(name);
64          }
65      }
66  
67      @Override
68      public Iterator<Class<? extends Extension>> iterator()
69      {
70          List<Class<? extends Extension>> coll = new ArrayList<>();
71          synchronized (registry)
72          {
73              coll.addAll(registry.values());
74              return coll.iterator();
75          }
76      }
77  
78      public Extension newInstance(ExtensionConfig config)
79      {
80          if (config == null)
81          {
82              return null;
83          }
84          String name = config.getName();
85          if (StringUtil.isBlank(name))
86          {
87              return null;
88          }
89          Class<? extends Extension> extClass = registry.get(name);
90          if (extClass == null)
91          {
92              return null;
93          }
94  
95          try
96          {
97              Extension ext = extClass.newInstance();
98              ext.setConfig(config);
99              ext.setPolicy(policy);
100             ext.setBufferPool(bufferPool);
101             return ext;
102         }
103         catch (InstantiationException | IllegalAccessException e)
104         {
105             throw new WebSocketException("Cannot instantiate extension: " + extClass,e);
106         }
107     }
108 
109     @Override
110     public void register(String name, Class<? extends Extension> extension)
111     {
112         synchronized (registry)
113         {
114             registry.put(name,extension);
115         }
116     }
117 
118     @Override
119     public void unregister(String name)
120     {
121         synchronized (registry)
122         {
123             registry.remove(name);
124         }
125     }
126 }