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.client;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.websocket.ClientEndpoint;
28  import javax.websocket.ClientEndpointConfig;
29  import javax.websocket.Decoder;
30  import javax.websocket.Encoder;
31  import javax.websocket.Extension;
32  
33  import org.eclipse.jetty.websocket.api.InvalidWebSocketException;
34  
35  public class AnnotatedClientEndpointConfig implements ClientEndpointConfig
36  {
37      private final List<Class<? extends Decoder>> decoders;
38      private final List<Class<? extends Encoder>> encoders;
39      private final List<Extension> extensions;
40      private final List<String> preferredSubprotocols;
41      private final Configurator configurator;
42      private Map<String, Object> userProperties;
43  
44      public AnnotatedClientEndpointConfig(ClientEndpoint anno)
45      {
46          this.decoders = Collections.unmodifiableList(Arrays.asList(anno.decoders()));
47          this.encoders = Collections.unmodifiableList(Arrays.asList(anno.encoders()));
48          this.preferredSubprotocols = Collections.unmodifiableList(Arrays.asList(anno.subprotocols()));
49  
50          // no extensions declared in annotation
51          this.extensions = Collections.emptyList();
52          // no userProperties in annotation
53          this.userProperties = new HashMap<>();
54  
55          if (anno.configurator() == null)
56          {
57              this.configurator = EmptyConfigurator.INSTANCE;
58          }
59          else
60          {
61              try
62              {
63                  this.configurator = anno.configurator().newInstance();
64              }
65              catch (InstantiationException | IllegalAccessException e)
66              {
67                  StringBuilder err = new StringBuilder();
68                  err.append("Unable to instantiate ClientEndpoint.configurator() of ");
69                  err.append(anno.configurator().getName());
70                  err.append(" defined as annotation in ");
71                  err.append(anno.getClass().getName());
72                  throw new InvalidWebSocketException(err.toString(),e);
73              }
74          }
75      }
76  
77      @Override
78      public Configurator getConfigurator()
79      {
80          return configurator;
81      }
82  
83      @Override
84      public List<Class<? extends Decoder>> getDecoders()
85      {
86          return decoders;
87      }
88  
89      @Override
90      public List<Class<? extends Encoder>> getEncoders()
91      {
92          return encoders;
93      }
94  
95      @Override
96      public List<Extension> getExtensions()
97      {
98          return extensions;
99      }
100 
101     @Override
102     public List<String> getPreferredSubprotocols()
103     {
104         return preferredSubprotocols;
105     }
106 
107     @Override
108     public Map<String, Object> getUserProperties()
109     {
110         return userProperties;
111     }
112 }