View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.annotation.Annotation;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.eclipse.jetty.websocket.common.util.ReflectUtils;
26  
27  public class Param
28  {
29      /**
30       * The various roles of the known parameters.
31       */
32      public static enum Role
33      {
34          SESSION,
35          ENDPOINT_CONFIG,
36          CLOSE_REASON,
37          ERROR_CAUSE,
38          MESSAGE_TEXT,
39          MESSAGE_TEXT_STREAM,
40          MESSAGE_BINARY,
41          MESSAGE_BINARY_STREAM,
42          MESSAGE_PONG,
43          MESSAGE_PARTIAL_FLAG,
44          PATH_PARAM;
45  
46          private static Role[] messageRoles;
47  
48          static
49          {
50              messageRoles = new Role[]
51              { MESSAGE_TEXT, MESSAGE_TEXT_STREAM, MESSAGE_BINARY, MESSAGE_BINARY_STREAM, MESSAGE_PONG, };
52          }
53  
54          public static Role[] getMessageRoles()
55          {
56              return messageRoles;
57          }
58      }
59  
60      public int index;
61      public Class<?> type;
62      private transient Map<Class<? extends Annotation>, Annotation> annotations;
63  
64      /*
65       * The bound role for this parameter.
66       */
67      public Role role = null;
68      private String pathParamName = null;
69  
70      public Param(int idx, Class<?> type, Annotation[] annos)
71      {
72          this.index = idx;
73          this.type = type;
74          if (annos != null)
75          {
76              this.annotations = new HashMap<>();
77              for (Annotation anno : annos)
78              {
79                  this.annotations.put(anno.annotationType(),anno);
80              }
81          }
82      }
83  
84      public void bind(Role role)
85      {
86          this.role = role;
87      }
88  
89      @SuppressWarnings("unchecked")
90      public <A extends Annotation> A getAnnotation(Class<A> annotationClass)
91      {
92          if (this.annotations == null)
93          {
94              return null;
95          }
96  
97          return (A)this.annotations.get(annotationClass);
98      }
99  
100     public String getPathParamName()
101     {
102         return this.pathParamName;
103     }
104 
105     public boolean isValid()
106     {
107         return this.role != null;
108     }
109 
110     public void setPathParamName(String name)
111     {
112         this.pathParamName = name;
113     }
114 
115     @Override
116     public String toString()
117     {
118         StringBuilder str = new StringBuilder();
119         str.append("Param[");
120         str.append("index=").append(index);
121         str.append(",type=").append(ReflectUtils.toShortName(type));
122         str.append(",role=").append(role);
123         if (pathParamName != null)
124         {
125             str.append(",pathParamName=").append(pathParamName);
126         }
127         str.append(']');
128         return str.toString();
129     }
130 
131     public void unbind()
132     {
133         this.role = null;
134     }
135 }