1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.websocket.jsr356.server;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.websocket.Decoder;
29 import javax.websocket.DeploymentException;
30 import javax.websocket.Encoder;
31 import javax.websocket.Extension;
32 import javax.websocket.server.ServerEndpoint;
33 import javax.websocket.server.ServerEndpointConfig;
34
35 import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
36
37 public class AnnotatedServerEndpointConfig implements ServerEndpointConfig
38 {
39 private final Class<?> endpointClass;
40 private final String path;
41 private final List<Class<? extends Decoder>> decoders;
42 private final List<Class<? extends Encoder>> encoders;
43 private final ServerEndpointConfig.Configurator configurator;
44 private final List<String> subprotocols;
45
46 private Map<String, Object> userProperties;
47 private List<Extension> extensions;
48
49 public AnnotatedServerEndpointConfig(WebSocketContainerScope containerScope, Class<?> endpointClass, ServerEndpoint anno) throws DeploymentException
50 {
51 this(containerScope,endpointClass,anno,null);
52 }
53
54 public AnnotatedServerEndpointConfig(WebSocketContainerScope containerScope, Class<?> endpointClass, ServerEndpoint anno, ServerEndpointConfig baseConfig) throws DeploymentException
55 {
56 ServerEndpointConfig.Configurator configr = null;
57
58
59 if (baseConfig != null)
60 {
61 configr = baseConfig.getConfigurator();
62 }
63
64
65 if (baseConfig != null && baseConfig.getDecoders() != null && baseConfig.getDecoders().size() > 0)
66 {
67 this.decoders = Collections.unmodifiableList(baseConfig.getDecoders());
68 }
69 else
70 {
71 this.decoders = Collections.unmodifiableList(Arrays.asList(anno.decoders()));
72 }
73
74
75 if (baseConfig != null && baseConfig.getEncoders() != null && baseConfig.getEncoders().size() > 0)
76 {
77 this.encoders = Collections.unmodifiableList(baseConfig.getEncoders());
78 }
79 else
80 {
81 this.encoders = Collections.unmodifiableList(Arrays.asList(anno.encoders()));
82 }
83
84
85 if (baseConfig != null && baseConfig.getSubprotocols() != null && baseConfig.getSubprotocols().size() > 0)
86 {
87 this.subprotocols = Collections.unmodifiableList(baseConfig.getSubprotocols());
88 }
89 else
90 {
91 this.subprotocols = Collections.unmodifiableList(Arrays.asList(anno.subprotocols()));
92 }
93
94
95 if (baseConfig != null && baseConfig.getPath() != null && baseConfig.getPath().length() > 0)
96 {
97 this.path = baseConfig.getPath();
98 }
99 else
100 {
101 this.path = anno.value();
102 }
103
104
105 this.extensions = new ArrayList<>();
106
107 this.endpointClass = endpointClass;
108
109 this.userProperties = new HashMap<>();
110 if (baseConfig != null && baseConfig.getUserProperties() != null && baseConfig.getUserProperties().size() > 0)
111 {
112 userProperties.putAll(baseConfig.getUserProperties());
113 }
114
115 ServerEndpointConfig.Configurator cfgr;
116
117 if (anno.configurator() == ServerEndpointConfig.Configurator.class)
118 {
119 if (configr != null)
120 {
121 cfgr = configr;
122 }
123 else
124 {
125 cfgr = new ContainerDefaultConfigurator();
126 }
127 }
128 else
129 {
130 try
131 {
132 cfgr = anno.configurator().newInstance();
133 }
134 catch (InstantiationException | IllegalAccessException e)
135 {
136 StringBuilder err = new StringBuilder();
137 err.append("Unable to instantiate ClientEndpoint.configurator() of ");
138 err.append(anno.configurator().getName());
139 err.append(" defined as annotation in ");
140 err.append(anno.getClass().getName());
141 throw new DeploymentException(err.toString(),e);
142 }
143 }
144
145
146 this.configurator = containerScope.getObjectFactory().decorate(cfgr);
147 }
148
149 @Override
150 public ServerEndpointConfig.Configurator getConfigurator()
151 {
152 return configurator;
153 }
154
155 @Override
156 public List<Class<? extends Decoder>> getDecoders()
157 {
158 return decoders;
159 }
160
161 @Override
162 public List<Class<? extends Encoder>> getEncoders()
163 {
164 return encoders;
165 }
166
167 @Override
168 public Class<?> getEndpointClass()
169 {
170 return endpointClass;
171 }
172
173 @Override
174 public List<Extension> getExtensions()
175 {
176 return extensions;
177 }
178
179 @Override
180 public String getPath()
181 {
182 return path;
183 }
184
185 @Override
186 public List<String> getSubprotocols()
187 {
188 return subprotocols;
189 }
190
191 @Override
192 public Map<String, Object> getUserProperties()
193 {
194 return userProperties;
195 }
196
197 @Override
198 public String toString()
199 {
200 StringBuilder builder = new StringBuilder();
201 builder.append("AnnotatedServerEndpointConfig[endpointClass=");
202 builder.append(endpointClass);
203 builder.append(",path=");
204 builder.append(path);
205 builder.append(",decoders=");
206 builder.append(decoders);
207 builder.append(",encoders=");
208 builder.append(encoders);
209 builder.append(",subprotocols=");
210 builder.append(subprotocols);
211 builder.append(",extensions=");
212 builder.append(extensions);
213 builder.append("]");
214 return builder.toString();
215 }
216 }