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