Skip to content

Commit c76fdb0

Browse files
committed
eliminates deprecations for websocket transport
Signed-off-by: Oleh Dokuka <[email protected]>
1 parent a8df316 commit c76fdb0

File tree

3 files changed

+10
-133
lines changed

3 files changed

+10
-133
lines changed

rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/client/WebsocketClientTransport.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
*/
4444
public final class WebsocketClientTransport implements ClientTransport, TransportHeaderAware {
4545

46-
private static final int DEFAULT_FRAME_SIZE = 65536;
4746
private static final String DEFAULT_PATH = "/";
4847

4948
private final HttpClient client;

rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/server/WebsocketRouteTransport.java

Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,20 @@
1919
import static io.rsocket.frame.FrameLengthFlyweight.FRAME_LENGTH_MASK;
2020

2121
import io.netty.buffer.ByteBufAllocator;
22-
import io.netty.handler.codec.http.HttpMethod;
2322
import io.rsocket.Closeable;
2423
import io.rsocket.DuplexConnection;
2524
import io.rsocket.fragmentation.FragmentationDuplexConnection;
2625
import io.rsocket.transport.ServerTransport;
2726
import io.rsocket.transport.netty.WebsocketDuplexConnection;
28-
import java.util.ArrayList;
29-
import java.util.HashMap;
30-
import java.util.List;
31-
import java.util.Map;
3227
import java.util.Objects;
3328
import java.util.function.BiFunction;
3429
import java.util.function.Consumer;
35-
import java.util.regex.Matcher;
36-
import java.util.regex.Pattern;
3730
import org.reactivestreams.Publisher;
3831
import reactor.core.publisher.Mono;
3932
import reactor.netty.Connection;
4033
import reactor.netty.http.server.HttpServer;
4134
import reactor.netty.http.server.HttpServerRoutes;
35+
import reactor.netty.http.server.WebsocketServerSpec;
4236
import reactor.netty.http.websocket.WebsocketInbound;
4337
import reactor.netty.http.websocket.WebsocketOutbound;
4438

@@ -48,7 +42,7 @@
4842
*/
4943
public final class WebsocketRouteTransport extends BaseWebsocketServerTransport<Closeable> {
5044

51-
private final UriPathTemplate template;
45+
private final String path;
5246

5347
private final Consumer<? super HttpServerRoutes> routesBuilder;
5448

@@ -65,7 +59,7 @@ public WebsocketRouteTransport(
6559
HttpServer server, Consumer<? super HttpServerRoutes> routesBuilder, String path) {
6660
this.server = serverConfigurer.apply(Objects.requireNonNull(server, "server must not be null"));
6761
this.routesBuilder = Objects.requireNonNull(routesBuilder, "routesBuilder must not be null");
68-
this.template = new UriPathTemplate(Objects.requireNonNull(path, "path must not be null"));
62+
this.path = Objects.requireNonNull(path, "path must not be null");
6963
}
7064

7165
@Override
@@ -77,10 +71,9 @@ public Mono<Closeable> start(ConnectionAcceptor acceptor, int mtu) {
7771
routes -> {
7872
routesBuilder.accept(routes);
7973
routes.ws(
80-
hsr -> hsr.method().equals(HttpMethod.GET) && template.matches(hsr.uri()),
74+
path,
8175
newHandler(acceptor, mtu),
82-
null,
83-
FRAME_LENGTH_MASK);
76+
WebsocketServerSpec.builder().maxFramePayloadLength(FRAME_LENGTH_MASK).build());
8477
})
8578
.bind()
8679
.map(CloseableChannel::new);
@@ -118,121 +111,4 @@ public static BiFunction<WebsocketInbound, WebsocketOutbound, Publisher<Void>> n
118111
return acceptor.apply(connection).then(out.neverComplete());
119112
};
120113
}
121-
122-
static final class UriPathTemplate {
123-
124-
private static final Pattern FULL_SPLAT_PATTERN = Pattern.compile("[\\*][\\*]");
125-
private static final String FULL_SPLAT_REPLACEMENT = ".*";
126-
127-
private static final Pattern NAME_SPLAT_PATTERN = Pattern.compile("\\{([^/]+?)\\}[\\*][\\*]");
128-
private static final String NAME_SPLAT_REPLACEMENT = "(?<%NAME%>.*)";
129-
130-
private static final Pattern NAME_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
131-
private static final String NAME_REPLACEMENT = "(?<%NAME%>[^\\/]*)";
132-
133-
private final List<String> pathVariables = new ArrayList<>();
134-
private final HashMap<String, Matcher> matchers = new HashMap<>();
135-
private final HashMap<String, Map<String, String>> vars = new HashMap<>();
136-
137-
private final Pattern uriPattern;
138-
139-
static String filterQueryParams(String uri) {
140-
int hasQuery = uri.lastIndexOf("?");
141-
if (hasQuery != -1) {
142-
return uri.substring(0, hasQuery);
143-
} else {
144-
return uri;
145-
}
146-
}
147-
148-
/**
149-
* Creates a new {@code UriPathTemplate} from the given {@code uriPattern}.
150-
*
151-
* @param uriPattern The pattern to be used by the template
152-
*/
153-
UriPathTemplate(String uriPattern) {
154-
String s = "^" + filterQueryParams(uriPattern);
155-
156-
Matcher m = NAME_SPLAT_PATTERN.matcher(s);
157-
while (m.find()) {
158-
for (int i = 1; i <= m.groupCount(); i++) {
159-
String name = m.group(i);
160-
pathVariables.add(name);
161-
s = m.replaceFirst(NAME_SPLAT_REPLACEMENT.replaceAll("%NAME%", name));
162-
m.reset(s);
163-
}
164-
}
165-
166-
m = NAME_PATTERN.matcher(s);
167-
while (m.find()) {
168-
for (int i = 1; i <= m.groupCount(); i++) {
169-
String name = m.group(i);
170-
pathVariables.add(name);
171-
s = m.replaceFirst(NAME_REPLACEMENT.replaceAll("%NAME%", name));
172-
m.reset(s);
173-
}
174-
}
175-
176-
m = FULL_SPLAT_PATTERN.matcher(s);
177-
while (m.find()) {
178-
s = m.replaceAll(FULL_SPLAT_REPLACEMENT);
179-
m.reset(s);
180-
}
181-
182-
this.uriPattern = Pattern.compile(s + "$");
183-
}
184-
185-
/**
186-
* Tests the given {@code uri} against this template, returning {@code true} if the uri matches
187-
* the template, {@code false} otherwise.
188-
*
189-
* @param uri The uri to match
190-
* @return {@code true} if there's a match, {@code false} otherwise
191-
*/
192-
public boolean matches(String uri) {
193-
return matcher(uri).matches();
194-
}
195-
196-
/**
197-
* Matches the template against the given {@code uri} returning a map of path parameters
198-
* extracted from the uri, keyed by the names in the template. If the uri does not match, or
199-
* there are no path parameters, an empty map is returned.
200-
*
201-
* @param uri The uri to match
202-
* @return the path parameters from the uri. Never {@code null}.
203-
*/
204-
final Map<String, String> match(String uri) {
205-
Map<String, String> pathParameters = vars.get(uri);
206-
if (null != pathParameters) {
207-
return pathParameters;
208-
}
209-
210-
pathParameters = new HashMap<>();
211-
Matcher m = matcher(uri);
212-
if (m.matches()) {
213-
int i = 1;
214-
for (String name : pathVariables) {
215-
String val = m.group(i++);
216-
pathParameters.put(name, val);
217-
}
218-
}
219-
synchronized (vars) {
220-
vars.put(uri, pathParameters);
221-
}
222-
223-
return pathParameters;
224-
}
225-
226-
private Matcher matcher(String uri) {
227-
uri = filterQueryParams(uri);
228-
Matcher m = matchers.get(uri);
229-
if (null == m) {
230-
m = uriPattern.matcher(uri);
231-
synchronized (matchers) {
232-
matchers.put(uri, m);
233-
}
234-
}
235-
return m;
236-
}
237-
}
238114
}

rsocket-transport-netty/src/main/java/io/rsocket/transport/netty/server/WebsocketServerTransport.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import reactor.core.publisher.Mono;
3636
import reactor.netty.Connection;
3737
import reactor.netty.http.server.HttpServer;
38+
import reactor.netty.http.server.WebsocketServerSpec;
3839

3940
/**
4041
* An implementation of {@link ServerTransport} that connects to a {@link ClientTransport} via a
@@ -122,8 +123,6 @@ public Mono<CloseableChannel> start(ConnectionAcceptor acceptor, int mtu) {
122123
(request, response) -> {
123124
transportHeaders.get().forEach(response::addHeader);
124125
return response.sendWebsocket(
125-
null,
126-
FRAME_LENGTH_MASK,
127126
(in, out) -> {
128127
DuplexConnection connection =
129128
new WebsocketDuplexConnection((Connection) in);
@@ -133,7 +132,10 @@ public Mono<CloseableChannel> start(ConnectionAcceptor acceptor, int mtu) {
133132
connection, ByteBufAllocator.DEFAULT, mtu, false, "server");
134133
}
135134
return acceptor.apply(connection).then(out.neverComplete());
136-
});
135+
},
136+
WebsocketServerSpec.builder()
137+
.maxFramePayloadLength(FRAME_LENGTH_MASK)
138+
.build());
137139
})
138140
.bind()
139141
.map(CloseableChannel::new);

0 commit comments

Comments
 (0)