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.io.Reader;
22  
23  import javax.websocket.OnMessage;
24  
25  import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
26  import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
27  
28  /**
29   * Param handling for static Text @{@link javax.websocket.OnMessage} parameters
30   */
31  public class JsrParamIdText extends JsrParamIdOnMessage implements IJsrParamId
32  {
33      public static final IJsrParamId INSTANCE = new JsrParamIdText();
34  
35      private boolean isMessageRoleAssigned(JsrCallable callable)
36      {
37          if (callable instanceof OnMessageCallable)
38          {
39              OnMessageCallable onmessage = (OnMessageCallable)callable;
40              return onmessage.isMessageRoleAssigned();
41          }
42          return false;
43      }
44  
45      @Override
46      public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
47      {
48          if (super.process(param,callable))
49          {
50              // Found common roles
51              return true;
52          }
53  
54          // String for whole message
55          if (param.type.isAssignableFrom(String.class))
56          {
57              param.bind(Role.MESSAGE_TEXT);
58              callable.setDecodingType(String.class);
59              return true;
60          }
61  
62          // Java primitive or class equivalent to receive the whole message converted to that type
63          if (param.type.isAssignableFrom(Boolean.class))
64          {
65              assertPartialMessageSupportDisabled(param,callable);
66              param.bind(Role.MESSAGE_TEXT);
67              callable.setDecodingType(Boolean.class);
68              return true;
69          }
70          if (param.type.isAssignableFrom(Byte.class) || (param.type == Byte.TYPE))
71          {
72              assertPartialMessageSupportDisabled(param,callable);
73              param.bind(Role.MESSAGE_TEXT);
74              callable.setDecodingType(Byte.class);
75              return true;
76          }
77          if (param.type.isAssignableFrom(Character.class) || (param.type == Character.TYPE))
78          {
79              assertPartialMessageSupportDisabled(param,callable);
80              param.bind(Role.MESSAGE_TEXT);
81              callable.setDecodingType(Character.class);
82              return true;
83          }
84          if (param.type.isAssignableFrom(Double.class) || (param.type == Double.TYPE))
85          {
86              assertPartialMessageSupportDisabled(param,callable);
87              param.bind(Role.MESSAGE_TEXT);
88              callable.setDecodingType(Double.class);
89              return true;
90          }
91          if (param.type.isAssignableFrom(Float.class) || (param.type == Float.TYPE))
92          {
93              assertPartialMessageSupportDisabled(param,callable);
94              param.bind(Role.MESSAGE_TEXT);
95              callable.setDecodingType(Float.class);
96              return true;
97          }
98          if (param.type.isAssignableFrom(Integer.class) || (param.type == Integer.TYPE))
99          {
100             assertPartialMessageSupportDisabled(param,callable);
101             param.bind(Role.MESSAGE_TEXT);
102             callable.setDecodingType(Integer.class);
103             return true;
104         }
105         if (param.type.isAssignableFrom(Long.class) || (param.type == Long.TYPE))
106         {
107             assertPartialMessageSupportDisabled(param,callable);
108             param.bind(Role.MESSAGE_TEXT);
109             callable.setDecodingType(Long.class);
110             return true;
111         }
112         if (param.type.isAssignableFrom(Short.class) || (param.type == Short.TYPE))
113         {
114             assertPartialMessageSupportDisabled(param,callable);
115             param.bind(Role.MESSAGE_TEXT);
116             callable.setDecodingType(Short.class);
117             return true;
118         }
119 
120         // Streaming
121         if (param.type.isAssignableFrom(Reader.class))
122         {
123             assertPartialMessageSupportDisabled(param,callable);
124             param.bind(Role.MESSAGE_TEXT_STREAM);
125             callable.setDecodingType(Reader.class);
126             return true;
127         }
128 
129         /*
130          * boolean primitive.
131          * 
132          * can be used for either: 1) a boolean message type 2) a partial message indicator flag
133          */
134         if (param.type == Boolean.TYPE)
135         {
136             if (isMessageRoleAssigned(callable))
137             {
138                 param.bind(Role.MESSAGE_PARTIAL_FLAG);
139             }
140             else
141             {
142                 param.bind(Role.MESSAGE_TEXT);
143                 callable.setDecodingType(Boolean.class);
144             }
145             return true;
146         }
147 
148         return false;
149     }
150 }