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