1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.websockets.jsr;
20
21 import io.undertow.protocols.ssl.UndertowXnioSsl;
22 import org.xnio.OptionMap;
23 import org.xnio.XnioWorker;
24 import org.xnio.ssl.XnioSsl;
25
26 import javax.net.ssl.SSLContext;
27 import javax.websocket.ClientEndpointConfig;
28 import javax.websocket.Endpoint;
29 import java.net.URI;
30
31 /**
32  * Client SSL provider that gets the SSL context in one of two ways.
33  *
34  * Either the {@link #setSslContext(javax.net.ssl.SSLContext)} method can
35  * be invoked before connecting, and this context will be used for the next
36  * client connection from this thread, or alternatively the
37  * io.undertow.websocket.SSL_CONTEXT property can be set in the user properties
38  * of the ClientEndpointConfig.
39  *
40  * @author Stuart Douglas
41  */

42 public class DefaultWebSocketClientSslProvider implements WebsocketClientSslProvider {
43
44     public static final String SSL_CONTEXT = "io.undertow.websocket.SSL_CONTEXT";
45
46     private static final ThreadLocal<SSLContext> LOCAL_SSL_CONTEXT = new ThreadLocal<>();
47
48     @Override
49     public XnioSsl getSsl(XnioWorker worker, Class<?> annotatedEndpoint, URI uri) {
50         return getThreadLocalSsl(worker);
51     }
52
53     @Override
54     public XnioSsl getSsl(XnioWorker worker, Object annotatedEndpointInstance, URI uri) {
55         return getThreadLocalSsl(worker);
56     }
57
58     @Override
59     public XnioSsl getSsl(XnioWorker worker, Endpoint endpoint, ClientEndpointConfig cec, URI uri) {
60         XnioSsl ssl =  getThreadLocalSsl(worker);
61         if(ssl != null) {
62             return ssl;
63         }
64         //look for some SSL config
65         SSLContext sslContext = (SSLContext) cec.getUserProperties().get(SSL_CONTEXT);
66
67         if (sslContext != null) {
68             return new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, sslContext);
69         }
70         return null;
71     }
72
73     public static void setSslContext(final SSLContext context) {
74         LOCAL_SSL_CONTEXT.set(context);
75     }
76     private XnioSsl getThreadLocalSsl(XnioWorker worker) {
77         SSLContext val = LOCAL_SSL_CONTEXT.get();
78         if (val != null) {
79             LOCAL_SSL_CONTEXT.remove();
80             return new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, val);
81         }
82         return null;
83     }
84
85 }
86