xref: /unit/src/java/javax/websocket/ClientEndpointConfig.java (revision 1157:7ae152bda303)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package javax.websocket;
18 
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Map;
22 
23 public interface ClientEndpointConfig extends EndpointConfig {
24 
getPreferredSubprotocols()25     List<String> getPreferredSubprotocols();
26 
getExtensions()27     List<Extension> getExtensions();
28 
getConfigurator()29     public Configurator getConfigurator();
30 
31     public final class Builder {
32 
33         private static final Configurator DEFAULT_CONFIGURATOR =
34                 new Configurator() {};
35 
36 
create()37         public static Builder create() {
38             return new Builder();
39         }
40 
41 
Builder()42         private Builder() {
43             // Hide default constructor
44         }
45 
46         private Configurator configurator = DEFAULT_CONFIGURATOR;
47         private List<String> preferredSubprotocols = Collections.emptyList();
48         private List<Extension> extensions = Collections.emptyList();
49         private List<Class<? extends Encoder>> encoders =
50                 Collections.emptyList();
51         private List<Class<? extends Decoder>> decoders =
52                 Collections.emptyList();
53 
54 
build()55         public ClientEndpointConfig build() {
56             return new DefaultClientEndpointConfig(preferredSubprotocols,
57                     extensions, encoders, decoders, configurator);
58         }
59 
60 
configurator(Configurator configurator)61         public Builder configurator(Configurator configurator) {
62             if (configurator == null) {
63                 this.configurator = DEFAULT_CONFIGURATOR;
64             } else {
65                 this.configurator = configurator;
66             }
67             return this;
68         }
69 
70 
preferredSubprotocols( List<String> preferredSubprotocols)71         public Builder preferredSubprotocols(
72                 List<String> preferredSubprotocols) {
73             if (preferredSubprotocols == null ||
74                     preferredSubprotocols.size() == 0) {
75                 this.preferredSubprotocols = Collections.emptyList();
76             } else {
77                 this.preferredSubprotocols =
78                         Collections.unmodifiableList(preferredSubprotocols);
79             }
80             return this;
81         }
82 
83 
extensions( List<Extension> extensions)84         public Builder extensions(
85                 List<Extension> extensions) {
86             if (extensions == null || extensions.size() == 0) {
87                 this.extensions = Collections.emptyList();
88             } else {
89                 this.extensions = Collections.unmodifiableList(extensions);
90             }
91             return this;
92         }
93 
94 
encoders(List<Class<? extends Encoder>> encoders)95         public Builder encoders(List<Class<? extends Encoder>> encoders) {
96             if (encoders == null || encoders.size() == 0) {
97                 this.encoders = Collections.emptyList();
98             } else {
99                 this.encoders = Collections.unmodifiableList(encoders);
100             }
101             return this;
102         }
103 
104 
decoders(List<Class<? extends Decoder>> decoders)105         public Builder decoders(List<Class<? extends Decoder>> decoders) {
106             if (decoders == null || decoders.size() == 0) {
107                 this.decoders = Collections.emptyList();
108             } else {
109                 this.decoders = Collections.unmodifiableList(decoders);
110             }
111             return this;
112         }
113     }
114 
115 
116     public class Configurator {
117 
118         /**
119          * Provides the client with a mechanism to inspect and/or modify the headers
120          * that are sent to the server to start the WebSocket handshake.
121          *
122          * @param headers   The HTTP headers
123          */
beforeRequest(Map<String, List<String>> headers)124         public void beforeRequest(Map<String, List<String>> headers) {
125             // NO-OP
126         }
127 
128         /**
129          * Provides the client with a mechanism to inspect the handshake response
130          * that is returned from the server.
131          *
132          * @param handshakeResponse The response
133          */
afterResponse(HandshakeResponse handshakeResponse)134         public void afterResponse(HandshakeResponse handshakeResponse) {
135             // NO-OP
136         }
137     }
138 }
139