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