View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  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              // nothing requested, don't return anything
72              return NO_SUBPROTOCOL;
73          }
74  
75          // Nothing specifically called out as being supported by the endpoint
76          if ((supported == null) || (supported.isEmpty()))
77          {
78              // Just return the first hit in this case
79              LOG.warn("Client requested Subprotocols on endpoint with none supported: {}",QuoteUtil.join(requested,","));
80              return NO_SUBPROTOCOL;
81          }
82  
83          // Return the first matching hit from the list of supported protocols.
84          for (String possible : requested)
85          {
86              if (possible == null)
87              {
88                  // skip null
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         /* do nothing */
107     }
108 }