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.annotations;
20  
21  import javax.websocket.Decoder;
22  import javax.websocket.OnMessage;
23  
24  import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
25  import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
26  import org.eclipse.jetty.websocket.jsr356.metadata.DecoderMetadata;
27  
28  /**
29   * Param handling for Text or Binary @{@link OnMessage} parameters declared as {@link Decoder}s
30   */
31  public class JsrParamIdDecoder extends JsrParamIdOnMessage implements IJsrParamId
32  {
33      private final DecoderMetadata metadata;
34  
35      public JsrParamIdDecoder(DecoderMetadata metadata)
36      {
37          this.metadata = metadata;
38      }
39  
40      @Override
41      public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
42      {
43          if (param.type.isAssignableFrom(metadata.getObjectType()))
44          {
45              assertPartialMessageSupportDisabled(param,callable);
46  
47              switch (metadata.getMessageType())
48              {
49                  case TEXT:
50                      if (metadata.isStreamed())
51                      {
52                          param.bind(Role.MESSAGE_TEXT_STREAM);
53                      }
54                      else
55                      {
56                          param.bind(Role.MESSAGE_TEXT);
57                      }
58                      break;
59                  case BINARY:
60                      if (metadata.isStreamed())
61                      {
62                          param.bind(Role.MESSAGE_BINARY_STREAM);
63                      }
64                      else
65                      {
66                          param.bind(Role.MESSAGE_BINARY);
67                      }
68                      break;
69                  case PONG:
70                      param.bind(Role.MESSAGE_PONG);
71                      break;
72              }
73              callable.setDecoderClass(metadata.getCoderClass());
74              return true;
75          }
76          return false;
77      }
78  }