1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48
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 }