|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.websocket.client.jetty; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.concurrent.Future; |
| 21 | + |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.eclipse.jetty.websocket.api.Session; |
| 25 | +import org.springframework.context.SmartLifecycle; |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.web.util.UriComponentsBuilder; |
| 28 | +import org.springframework.websocket.WebSocketHandler; |
| 29 | +import org.springframework.websocket.WebSocketSession; |
| 30 | +import org.springframework.websocket.adapter.JettyWebSocketListenerAdapter; |
| 31 | +import org.springframework.websocket.adapter.JettyWebSocketSessionAdapter; |
| 32 | +import org.springframework.websocket.client.WebSocketClient; |
| 33 | +import org.springframework.websocket.client.WebSocketConnectFailureException; |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Rossen Stoyanchev |
| 38 | + * @since 4.0 |
| 39 | + */ |
| 40 | +public class JettyWebSocketClient implements WebSocketClient, SmartLifecycle { |
| 41 | + |
| 42 | + private static final Log logger = LogFactory.getLog(JettyWebSocketClient.class); |
| 43 | + |
| 44 | + private final org.eclipse.jetty.websocket.client.WebSocketClient client; |
| 45 | + |
| 46 | + private boolean autoStartup = true; |
| 47 | + |
| 48 | + private int phase = Integer.MAX_VALUE; |
| 49 | + |
| 50 | + private final Object lifecycleMonitor = new Object(); |
| 51 | + |
| 52 | + |
| 53 | + public JettyWebSocketClient() { |
| 54 | + this.client = new org.eclipse.jetty.websocket.client.WebSocketClient(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + // TODO: configure Jetty WebSocketClient properties |
| 59 | + |
| 60 | + public void setAutoStartup(boolean autoStartup) { |
| 61 | + this.autoStartup = autoStartup; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean isAutoStartup() { |
| 66 | + return this.autoStartup; |
| 67 | + } |
| 68 | + |
| 69 | + public void setPhase(int phase) { |
| 70 | + this.phase = phase; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int getPhase() { |
| 75 | + return this.phase; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean isRunning() { |
| 80 | + synchronized (this.lifecycleMonitor) { |
| 81 | + return this.client.isStarted(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void start() { |
| 87 | + synchronized (this.lifecycleMonitor) { |
| 88 | + if (!isRunning()) { |
| 89 | + try { |
| 90 | + if (logger.isDebugEnabled()) { |
| 91 | + logger.debug("Starting Jetty WebSocketClient"); |
| 92 | + } |
| 93 | + this.client.start(); |
| 94 | + } |
| 95 | + catch (Exception e) { |
| 96 | + throw new IllegalStateException("Failed to start Jetty client", e); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void stop() { |
| 104 | + synchronized (this.lifecycleMonitor) { |
| 105 | + if (isRunning()) { |
| 106 | + try { |
| 107 | + if (logger.isDebugEnabled()) { |
| 108 | + logger.debug("Stopping Jetty WebSocketClient"); |
| 109 | + } |
| 110 | + this.client.stop(); |
| 111 | + } |
| 112 | + catch (Exception e) { |
| 113 | + logger.error("Error stopping Jetty WebSocketClient", e); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void stop(Runnable callback) { |
| 121 | + this.stop(); |
| 122 | + callback.run(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVariables) |
| 127 | + throws WebSocketConnectFailureException { |
| 128 | + |
| 129 | + URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVariables).encode().toUri(); |
| 130 | + return doHandshake(webSocketHandler, null, uri); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri) |
| 135 | + throws WebSocketConnectFailureException { |
| 136 | + |
| 137 | + JettyWebSocketListenerAdapter listener = new JettyWebSocketListenerAdapter(webSocketHandler); |
| 138 | + |
| 139 | + try { |
| 140 | + // block for now |
| 141 | + Future<org.eclipse.jetty.websocket.api.Session> future = this.client.connect(listener, uri); |
| 142 | + Session session = future.get(); |
| 143 | + return new JettyWebSocketSessionAdapter(session); |
| 144 | + } |
| 145 | + catch (Exception e) { |
| 146 | + throw new WebSocketConnectFailureException("Failed to connect to " + uri, e); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments