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 javax.websocket.server.ServerEndpoint;
22  import javax.websocket.server.ServerEndpointConfig;
23  
24  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
25  import org.eclipse.jetty.websocket.common.events.EventDriver;
26  import org.eclipse.jetty.websocket.common.events.EventDriverImpl;
27  import org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents;
28  import org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance;
29  import org.eclipse.jetty.websocket.jsr356.endpoints.JsrAnnotatedEventDriver;
30  
31  /**
32   * Event Driver for classes annotated with @{@link ServerEndpoint}
33   */
34  public class JsrServerEndpointImpl implements EventDriverImpl
35  {
36      @Override
37      public EventDriver create(Object websocket, WebSocketPolicy policy) throws Throwable
38      {
39          if (!(websocket instanceof EndpointInstance))
40          {
41              throw new IllegalStateException(String.format("Websocket %s must be an %s",websocket.getClass().getName(),EndpointInstance.class.getName()));
42          }
43  
44          EndpointInstance ei = (EndpointInstance)websocket;
45          AnnotatedServerEndpointMetadata metadata = (AnnotatedServerEndpointMetadata)ei.getMetadata();
46          JsrEvents<ServerEndpoint, ServerEndpointConfig> events = new JsrEvents<>(metadata);
47          JsrAnnotatedEventDriver driver = new JsrAnnotatedEventDriver(policy,ei,events);
48  
49          ServerEndpointConfig config = (ServerEndpointConfig)ei.getConfig();
50          if (config instanceof PathParamServerEndpointConfig)
51          {
52              PathParamServerEndpointConfig ppconfig = (PathParamServerEndpointConfig)config;
53              driver.setPathParameters(ppconfig.getPathParamMap());
54          }
55  
56          return driver;
57      }
58  
59      @Override
60      public String describeRule()
61      {
62          return "class is annotated with @" + ServerEndpoint.class.getName();
63      }
64  
65      @Override
66      public boolean supports(Object websocket)
67      {
68          if (!(websocket instanceof EndpointInstance))
69          {
70              return false;
71          }
72  
73          EndpointInstance ei = (EndpointInstance)websocket;
74          Object endpoint = ei.getEndpoint();
75  
76          ServerEndpoint anno = endpoint.getClass().getAnnotation(ServerEndpoint.class);
77          return (anno != null);
78      }
79  }