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.List;
22 import javax.websocket.Extension;
23 import javax.websocket.HandshakeResponse;
24 import javax.websocket.server.HandshakeRequest;
25 import javax.websocket.server.ServerEndpointConfig;
26
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
29 import org.eclipse.jetty.websocket.api.util.QuoteUtil;
30
31 public class BasicServerEndpointConfigurator extends ServerEndpointConfig.Configurator
32 {
33 private static final Logger LOG = Log.getLogger(BasicServerEndpointConfigurator.class);
34 private static final String NO_SUBPROTOCOL = "";
35 public static final ServerEndpointConfig.Configurator INSTANCE = new BasicServerEndpointConfigurator();
36
37 @Override
38 public boolean checkOrigin(String originHeaderValue)
39 {
40 return true;
41 }
42
43 @Override
44 public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException
45 {
46 if (LOG.isDebugEnabled())
47 {
48 LOG.debug(".getEndpointInstance({})",endpointClass);
49 }
50 try
51 {
52 return endpointClass.newInstance();
53 }
54 catch (IllegalAccessException e)
55 {
56 throw new InstantiationException(String.format("%s: %s",e.getClass().getName(),e.getMessage()));
57 }
58 }
59
60 @Override
61 public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested)
62 {
63 return requested;
64 }
65
66 @Override
67 public String getNegotiatedSubprotocol(List<String> supported, List<String> requested)
68 {
69 if ((requested == null) || (requested.size() == 0))
70 {
71
72 return NO_SUBPROTOCOL;
73 }
74
75
76 if ((supported == null) || (supported.isEmpty()))
77 {
78
79 LOG.warn("Client requested Subprotocols on endpoint with none supported: {}",QuoteUtil.join(requested,","));
80 return NO_SUBPROTOCOL;
81 }
82
83
84 for (String possible : requested)
85 {
86 if (possible == null)
87 {
88
89 continue;
90 }
91
92 if (supported.contains(possible))
93 {
94 return possible;
95 }
96 }
97
98 LOG.warn("Client requested subprotocols {} do not match any endpoint supported subprotocols {}",QuoteUtil.join(requested,","),
99 QuoteUtil.join(supported,","));
100 return NO_SUBPROTOCOL;
101 }
102
103 @Override
104 public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response)
105 {
106
107 }
108 }