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.server;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.websocket.Decoder;
29  import javax.websocket.DeploymentException;
30  import javax.websocket.Encoder;
31  import javax.websocket.Extension;
32  import javax.websocket.server.ServerEndpoint;
33  import javax.websocket.server.ServerEndpointConfig;
34  
35  public class AnnotatedServerEndpointConfig implements ServerEndpointConfig
36  {
37      private final Class<?> endpointClass;
38      private final String path;
39      private final List<Class<? extends Decoder>> decoders;
40      private final List<Class<? extends Encoder>> encoders;
41      private final Configurator configurator;
42      private final List<String> subprotocols;
43  
44      private Map<String, Object> userProperties;
45      private List<Extension> extensions;
46  
47      public AnnotatedServerEndpointConfig(Class<?> endpointClass, ServerEndpoint anno) throws DeploymentException
48      {
49          this(endpointClass,anno,null);
50      }
51  
52      public AnnotatedServerEndpointConfig(Class<?> endpointClass, ServerEndpoint anno, ServerEndpointConfig baseConfig) throws DeploymentException
53      {
54          Configurator configr = null;
55  
56          // Copy from base config
57          if (baseConfig != null)
58          {
59              configr = baseConfig.getConfigurator();
60          }
61  
62          // Decoders (favor provided config over annotation)
63          if (baseConfig != null && baseConfig.getDecoders() != null && baseConfig.getDecoders().size() > 0)
64          {
65              this.decoders = Collections.unmodifiableList(baseConfig.getDecoders());
66          }
67          else
68          {
69              this.decoders = Collections.unmodifiableList(Arrays.asList(anno.decoders()));
70          }
71  
72          // Encoders (favor provided config over annotation)
73          if (baseConfig != null && baseConfig.getEncoders() != null && baseConfig.getEncoders().size() > 0)
74          {
75              this.encoders = Collections.unmodifiableList(baseConfig.getEncoders());
76          }
77          else
78          {
79              this.encoders = Collections.unmodifiableList(Arrays.asList(anno.encoders()));
80          }
81  
82          // Sub Protocols (favor provided config over annotation)
83          if (baseConfig != null && baseConfig.getSubprotocols() != null && baseConfig.getSubprotocols().size() > 0)
84          {
85              this.subprotocols = Collections.unmodifiableList(baseConfig.getSubprotocols());
86          }
87          else
88          {
89              this.subprotocols = Collections.unmodifiableList(Arrays.asList(anno.subprotocols()));
90          }
91  
92          // Path (favor provided config over annotation)
93          if (baseConfig != null && baseConfig.getPath() != null && baseConfig.getPath().length() > 0)
94          {
95              this.path = baseConfig.getPath();
96          }
97          else
98          {
99              this.path = anno.value();
100         }
101 
102         // supplied by init lifecycle
103         this.extensions = new ArrayList<>();
104         // always what is passed in
105         this.endpointClass = endpointClass;
106         // UserProperties in annotation
107         this.userProperties = new HashMap<>();
108         if (baseConfig != null && baseConfig.getUserProperties() != null && baseConfig.getUserProperties().size() > 0)
109         {
110             userProperties.putAll(baseConfig.getUserProperties());
111         }
112 
113         if (anno.configurator() == ServerEndpointConfig.Configurator.class)
114         {
115             if (configr != null)
116             {
117                 this.configurator = configr;
118             }
119             else
120             {
121                 this.configurator = BasicServerEndpointConfigurator.INSTANCE;
122             }
123         }
124         else
125         {
126             try
127             {
128                 this.configurator = anno.configurator().newInstance();
129             }
130             catch (InstantiationException | IllegalAccessException e)
131             {
132                 StringBuilder err = new StringBuilder();
133                 err.append("Unable to instantiate ClientEndpoint.configurator() of ");
134                 err.append(anno.configurator().getName());
135                 err.append(" defined as annotation in ");
136                 err.append(anno.getClass().getName());
137                 throw new DeploymentException(err.toString(),e);
138             }
139         }
140     }
141 
142     @Override
143     public Configurator getConfigurator()
144     {
145         return configurator;
146     }
147 
148     @Override
149     public List<Class<? extends Decoder>> getDecoders()
150     {
151         return decoders;
152     }
153 
154     @Override
155     public List<Class<? extends Encoder>> getEncoders()
156     {
157         return encoders;
158     }
159 
160     @Override
161     public Class<?> getEndpointClass()
162     {
163         return endpointClass;
164     }
165 
166     @Override
167     public List<Extension> getExtensions()
168     {
169         return extensions;
170     }
171 
172     @Override
173     public String getPath()
174     {
175         return path;
176     }
177 
178     @Override
179     public List<String> getSubprotocols()
180     {
181         return subprotocols;
182     }
183 
184     @Override
185     public Map<String, Object> getUserProperties()
186     {
187         return userProperties;
188     }
189 
190     @Override
191     public String toString()
192     {
193         StringBuilder builder = new StringBuilder();
194         builder.append("AnnotatedServerEndpointConfig[endpointClass=");
195         builder.append(endpointClass);
196         builder.append(",path=");
197         builder.append(path);
198         builder.append(",decoders=");
199         builder.append(decoders);
200         builder.append(",encoders=");
201         builder.append(encoders);
202         builder.append(",subprotocols=");
203         builder.append(subprotocols);
204         builder.append(",extensions=");
205         builder.append(extensions);
206         builder.append("]");
207         return builder.toString();
208     }
209 }