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.annotations;
20  
21  import java.lang.reflect.Method;
22  
23  import javax.websocket.CloseReason;
24  import javax.websocket.CloseReason.CloseCodes;
25  import javax.websocket.Decoder;
26  import javax.websocket.OnClose;
27  
28  import org.eclipse.jetty.websocket.common.CloseInfo;
29  import org.eclipse.jetty.websocket.jsr356.JsrSession;
30  import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
31  
32  /**
33   * Callable for {@link OnClose} annotated methods
34   */
35  public class OnCloseCallable extends JsrCallable
36  {
37      private int idxCloseReason = -1;
38  
39      public OnCloseCallable(Class<?> pojo, Method method)
40      {
41          super(pojo,method);
42      }
43  
44      public OnCloseCallable(OnCloseCallable copy)
45      {
46          super(copy);
47          this.idxCloseReason = copy.idxCloseReason;
48      }
49  
50      public void call(Object endpoint, CloseInfo close)
51      {
52          this.call(endpoint,close.getStatusCode(),close.getReason());
53      }
54  
55      public void call(Object endpoint, CloseReason closeReason)
56      {
57          // Close Reason is an optional parameter
58          if (idxCloseReason >= 0)
59          {
60              // convert to javax.websocket.CloseReason
61              super.args[idxCloseReason] = closeReason;
62          }
63          super.call(endpoint,super.args);
64      }
65  
66      public void call(Object endpoint, int statusCode, String reason)
67      {
68          // Close Reason is an optional parameter
69          if (idxCloseReason >= 0)
70          {
71              // convert to javax.websocket.CloseReason
72              CloseReason jsrclose = new CloseReason(CloseCodes.getCloseCode(statusCode),reason);
73              super.args[idxCloseReason] = jsrclose;
74          }
75          super.call(endpoint,super.args);
76      }
77  
78      @Override
79      public void init(JsrSession session)
80      {
81          idxCloseReason = findIndexForRole(Role.CLOSE_REASON);
82          super.init(session);
83      }
84  
85      @Override
86      public void setDecoderClass(Class<? extends Decoder> decoderClass)
87      {
88          /* ignore, not relevant for onClose */
89      }
90  }