View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.endpoints;
20  
21  import java.util.Map;
22  
23  import javax.websocket.CloseReason;
24  import javax.websocket.CloseReason.CloseCode;
25  import javax.websocket.CloseReason.CloseCodes;
26  import javax.websocket.EndpointConfig;
27  import javax.websocket.Session;
28  
29  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
30  import org.eclipse.jetty.websocket.api.extensions.Frame;
31  import org.eclipse.jetty.websocket.common.CloseInfo;
32  import org.eclipse.jetty.websocket.common.WebSocketSession;
33  import org.eclipse.jetty.websocket.common.events.AbstractEventDriver;
34  import org.eclipse.jetty.websocket.jsr356.JsrSession;
35  import org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata;
36  
37  public abstract class AbstractJsrEventDriver extends AbstractEventDriver
38  {
39      protected final EndpointMetadata metadata;
40      protected final EndpointConfig config;
41      protected JsrSession jsrsession;
42      private boolean hasCloseBeenCalled = false;
43  
44      public AbstractJsrEventDriver(WebSocketPolicy policy, EndpointInstance endpointInstance)
45      {
46          super(policy,endpointInstance.getEndpoint());
47          this.config = endpointInstance.getConfig();
48          this.metadata = endpointInstance.getMetadata();
49      }
50  
51      public EndpointConfig getConfig()
52      {
53          return config;
54      }
55  
56      public Session getJsrSession()
57      {
58          return this.jsrsession;
59      }
60  
61      public EndpointMetadata getMetadata()
62      {
63          return metadata;
64      }
65  
66      public abstract void init(JsrSession jsrsession);
67  
68      @Override
69      public final void onClose(CloseInfo close)
70      {
71          if (hasCloseBeenCalled)
72          {
73              // avoid duplicate close events (possible when using harsh Session.disconnect())
74              return;
75          }
76          hasCloseBeenCalled = true;
77  
78          CloseCode closecode = CloseCodes.getCloseCode(close.getStatusCode());
79          CloseReason closereason = new CloseReason(closecode,close.getReason());
80          onClose(closereason);
81      }
82  
83      protected abstract void onClose(CloseReason closereason);
84  
85      @Override
86      public void onFrame(Frame frame)
87      {
88          /* Ignored, not supported by JSR-356 */
89      }
90  
91      @Override
92      public final void openSession(WebSocketSession session)
93      {
94          // Cast should be safe, as it was created by JsrSessionFactory
95          this.jsrsession = (JsrSession)session;
96  
97          // Allow jsr session to init
98          this.jsrsession.init(config);
99  
100         // Allow event driver to init itself
101         init(jsrsession);
102 
103         // Allow end-user socket to adjust configuration
104         super.openSession(session);
105     }
106 
107     public void setEndpointconfig(EndpointConfig endpointconfig)
108     {
109         throw new RuntimeException("Why are you reconfiguring the endpoint?");
110         // this.config = endpointconfig;
111     }
112 
113     public abstract void setPathParameters(Map<String, String> pathParameters);
114 }