Skip to content

Commit 709d4ba

Browse files
committed
Avoid explicit DecoratedObjectFactory setup in JettyRequestUpgradeStrategy
Issue: SPR-14940
1 parent 8d55c7d commit 709d4ba

File tree

7 files changed

+69
-67
lines changed

7 files changed

+69
-67
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import javax.servlet.http.HttpServletRequest;
2727
import javax.servlet.http.HttpServletResponse;
2828

29-
import org.eclipse.jetty.util.DecoratedObjectFactory;
3029
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
3130
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
3231
import org.eclipse.jetty.websocket.server.HandshakeRFC6455;
@@ -296,7 +295,6 @@ private class ModernJettyWebSocketServerFactoryAdapter extends WebSocketServerFa
296295

297296
@Override
298297
protected WebSocketServerFactory createFactory(WebSocketPolicy policy) throws Exception {
299-
servletContext.setAttribute(DecoratedObjectFactory.ATTR, new DecoratedObjectFactory());
300298
return new WebSocketServerFactory(servletContext, policy);
301299
}
302300

spring-websocket/src/test/java/org/springframework/web/socket/AbstractWebSocketIntegrationTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ public void setup() throws Exception {
8686

8787
this.server.setup();
8888
this.server.deployConfig(this.wac);
89-
// Set ServletContext in WebApplicationContext after deployment but before
90-
// starting the server.
89+
this.server.start();
90+
9191
this.wac.setServletContext(this.server.getServletContext());
9292
this.wac.refresh();
93-
this.server.start();
9493
}
9594

9695
protected abstract Class<?>[] getAnnotatedConfigClasses();

spring-websocket/src/test/java/org/springframework/web/socket/JettyWebSocketTestServer.java

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,11 +52,6 @@ public void setup() {
5252
this.jettyServer = new Server(0);
5353
}
5454

55-
@Override
56-
public int getPort() {
57-
return this.port;
58-
}
59-
6055
@Override
6156
public void deployConfig(WebApplicationContext wac, Filter... filters) {
6257
ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(wac));
@@ -72,11 +67,6 @@ private EnumSet<DispatcherType> getDispatcherTypes() {
7267
return EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC);
7368
}
7469

75-
@Override
76-
public ServletContext getServletContext() {
77-
return this.contextHandler.getServletContext();
78-
}
79-
8070
@Override
8171
public void undeployConfig() {
8272
// Stopping jetty will undeploy the servlet
@@ -85,6 +75,7 @@ public void undeployConfig() {
8575
@Override
8676
public void start() throws Exception {
8777
this.jettyServer.start();
78+
this.contextHandler.start();
8879

8980
Connector[] connectors = jettyServer.getConnectors();
9081
NetworkConnector connector = (NetworkConnector) connectors[0];
@@ -93,10 +84,27 @@ public void start() throws Exception {
9384

9485
@Override
9586
public void stop() throws Exception {
96-
if (this.jettyServer.isRunning()) {
97-
this.jettyServer.setStopTimeout(5000);
98-
this.jettyServer.stop();
87+
try {
88+
if (this.contextHandler.isRunning()) {
89+
this.contextHandler.stop();
90+
}
91+
}
92+
finally {
93+
if (this.jettyServer.isRunning()) {
94+
this.jettyServer.setStopTimeout(5000);
95+
this.jettyServer.stop();
96+
}
9997
}
10098
}
10199

100+
@Override
101+
public int getPort() {
102+
return this.port;
103+
}
104+
105+
@Override
106+
public ServletContext getServletContext() {
107+
return this.contextHandler.getServletContext();
108+
}
109+
102110
}

spring-websocket/src/test/java/org/springframework/web/socket/TomcatWebSocketTestServer.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21-
2221
import javax.servlet.Filter;
2322
import javax.servlet.ServletContext;
2423

@@ -82,15 +81,10 @@ private File createTempDir(String prefix) {
8281
return tempFolder;
8382
}
8483
catch (IOException ex) {
85-
throw new RuntimeException("Unable to create temp directory", ex);
84+
throw new IllegalStateException("Unable to create temp directory", ex);
8685
}
8786
}
8887

89-
@Override
90-
public int getPort() {
91-
return this.port;
92-
}
93-
9488
@Override
9589
public void deployConfig(WebApplicationContext wac, Filter... filters) {
9690
Assert.state(this.port != -1, "setup() was never called.");
@@ -112,11 +106,6 @@ public void deployConfig(WebApplicationContext wac, Filter... filters) {
112106
}
113107
}
114108

115-
@Override
116-
public ServletContext getServletContext() {
117-
return this.context.getServletContext();
118-
}
119-
120109
@Override
121110
public void undeployConfig() {
122111
if (this.context != null) {
@@ -143,4 +132,14 @@ public void stop() throws Exception {
143132
this.tomcatServer.stop();
144133
}
145134

135+
@Override
136+
public int getPort() {
137+
return this.port;
138+
}
139+
140+
@Override
141+
public ServletContext getServletContext() {
142+
return this.context.getServletContext();
143+
}
144+
146145
}

spring-websocket/src/test/java/org/springframework/web/socket/UndertowTestServer.java

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,13 @@
1616

1717
package org.springframework.web.socket;
1818

19+
import java.io.IOException;
20+
import javax.servlet.DispatcherType;
21+
import javax.servlet.Filter;
22+
import javax.servlet.Servlet;
23+
import javax.servlet.ServletContext;
24+
import javax.servlet.ServletException;
25+
1926
import io.undertow.Undertow;
2027
import io.undertow.server.HttpHandler;
2128
import io.undertow.servlet.api.DeploymentInfo;
@@ -24,23 +31,14 @@
2431
import io.undertow.servlet.api.InstanceFactory;
2532
import io.undertow.servlet.api.InstanceHandle;
2633
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
27-
28-
import java.io.IOException;
29-
30-
import javax.servlet.DispatcherType;
31-
import javax.servlet.Filter;
32-
import javax.servlet.Servlet;
33-
import javax.servlet.ServletContext;
34-
import javax.servlet.ServletException;
34+
import org.xnio.OptionMap;
35+
import org.xnio.Xnio;
3536

3637
import org.springframework.util.Assert;
3738
import org.springframework.util.SocketUtils;
3839
import org.springframework.web.context.WebApplicationContext;
3940
import org.springframework.web.servlet.DispatcherServlet;
4041

41-
import org.xnio.OptionMap;
42-
import org.xnio.Xnio;
43-
4442
import static io.undertow.servlet.Servlets.*;
4543

4644
/**
@@ -63,11 +61,6 @@ public void setup() {
6361
this.port = SocketUtils.findAvailableTcpPort();
6462
}
6563

66-
@Override
67-
public int getPort() {
68-
return this.port;
69-
}
70-
7164
@Override
7265
@SuppressWarnings("deprecation")
7366
public void deployConfig(WebApplicationContext wac, Filter... filters) {
@@ -108,11 +101,6 @@ public void deployConfig(WebApplicationContext wac, Filter... filters) {
108101
}
109102
}
110103

111-
@Override
112-
public ServletContext getServletContext() {
113-
return this.manager.getDeployment().getServletContext();
114-
}
115-
116104
@Override
117105
public void undeployConfig() {
118106
this.manager.undeploy();
@@ -128,6 +116,16 @@ public void stop() throws Exception {
128116
this.server.stop();
129117
}
130118

119+
@Override
120+
public int getPort() {
121+
return this.port;
122+
}
123+
124+
@Override
125+
public ServletContext getServletContext() {
126+
return this.manager.getDeployment().getServletContext();
127+
}
128+
131129

132130
private static class DispatcherServletInstanceFactory implements InstanceFactory<Servlet> {
133131

@@ -151,6 +149,7 @@ public void release() {
151149
}
152150
}
153151

152+
154153
private static class FilterInstanceFactory implements InstanceFactory<Filter> {
155154

156155
private final Filter filter;

spring-websocket/src/test/java/org/springframework/web/socket/WebSocketTestServer.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,26 +29,24 @@
2929
*/
3030
public interface WebSocketTestServer {
3131

32-
int getPort();
33-
3432
void setup();
3533

3634
void deployConfig(WebApplicationContext cxt, Filter... filters);
3735

36+
void undeployConfig();
37+
38+
void start() throws Exception;
39+
40+
void stop() throws Exception;
41+
42+
int getPort();
43+
3844
/**
3945
* Get the {@link ServletContext} created by the underlying server.
40-
*
4146
* <p>The {@code ServletContext} is only guaranteed to be available
4247
* after {@link #deployConfig} has been invoked.
43-
*
4448
* @since 4.2
4549
*/
4650
ServletContext getServletContext();
4751

48-
void undeployConfig();
49-
50-
void start() throws Exception;
51-
52-
void stop() throws Exception;
53-
54-
}
52+
}

spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ static class TestConfig implements WebSocketConfigurer {
9797
@Override
9898
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
9999
registry.addHandler(serverHandler(), "/ws")
100-
.setHandshakeHandler(this.handshakeHandler);
100+
.setHandshakeHandler(this.handshakeHandler);
101101
registry.addHandler(serverHandler(), "/sockjs").withSockJS()
102-
.setTransportHandlerOverrides(new WebSocketTransportHandler(this.handshakeHandler));
102+
.setTransportHandlerOverrides(new WebSocketTransportHandler(this.handshakeHandler));
103103
}
104104

105105
@Bean
@@ -108,6 +108,7 @@ public TestHandler serverHandler() {
108108
}
109109
}
110110

111+
111112
private static class TestHandler extends AbstractWebSocketHandler {
112113

113114
private CountDownLatch connectLatch = new CountDownLatch(1);

0 commit comments

Comments
 (0)