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