Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 38c023c

Browse files
author
Michal Gajdos
committed
JERSEY-2501: Jersey 2.8 breaks JerseyTest unit tests under TestNG
JERSEY-2502: JdkHttpServerFactory does not return HttpsServer when https:// URI is used. - ability to run TestNG tests in parallel (see JerseyTestNg and TestNgState) - find first available port for container if 0 is passed as the port - added some parallel TestNG tests - updated documentation Change-Id: I6be235f40b01a055d64ed1ac6279e98f431d085d Signed-off-by: Michal Gajdos <[email protected]>
1 parent 5f23719 commit 38c023c

File tree

37 files changed

+2320
-262
lines changed

37 files changed

+2320
-262
lines changed

containers/jdk-http/src/main/java/org/glassfish/jersey/jdkhttp/JdkHttpServerFactory.java

Lines changed: 104 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.sun.net.httpserver.HttpContext;
5454
import com.sun.net.httpserver.HttpHandler;
5555
import com.sun.net.httpserver.HttpServer;
56+
import com.sun.net.httpserver.HttpsConfigurator;
5657
import com.sun.net.httpserver.HttpsServer;
5758

5859
/**
@@ -100,7 +101,7 @@ public static HttpServer createHttpServer(final URI uri, final ResourceConfig co
100101
return createHttpServer(uri, new JdkHttpHandlerContainer(configuration), start);
101102
}
102103

103-
private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler, boolean start) {
104+
private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerContainer handler, final boolean start) {
104105

105106
if (uri == null) {
106107
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_NULL());
@@ -120,79 +121,156 @@ private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerCo
120121
throw new IllegalArgumentException(LocalizationMessages.ERROR_CONTAINER_URI_PATH_START(uri));
121122
}
122123

123-
final int port = (uri.getPort() == -1) ? 80 : uri.getPort();
124+
final boolean isHttp = scheme.equalsIgnoreCase("http");
125+
final int port = (uri.getPort() == -1)
126+
? (isHttp ? 80 : 143)
127+
: uri.getPort();
128+
124129
final HttpServer server;
125130
try {
126-
server = (scheme.equalsIgnoreCase("http"))
131+
server = isHttp
127132
? HttpServer.create(new InetSocketAddress(port), 0)
128133
: HttpsServer.create(new InetSocketAddress(port), 0);
129-
} catch (IOException ioe) {
134+
} catch (final IOException ioe) {
130135
throw new ProcessingException(LocalizationMessages.ERROR_CONTAINER_EXCEPTION_IO(), ioe);
131136
}
132137

133138
server.setExecutor(Executors.newCachedThreadPool());
134139
server.createContext(path, handler);
135140

136-
final HttpServer wrapper = new HttpServer() {
141+
final HttpServer wrapper = isHttp
142+
? createHttpServerWrapper(server, handler)
143+
: createHttpsServerWrapper((HttpsServer) server, handler);
144+
145+
if (start) {
146+
wrapper.start();
147+
}
148+
149+
return wrapper;
150+
}
151+
152+
private static HttpServer createHttpsServerWrapper(final HttpsServer delegate, final JdkHttpHandlerContainer handler) {
153+
return new HttpsServer() {
154+
155+
@Override
156+
public void setHttpsConfigurator(final HttpsConfigurator httpsConfigurator) {
157+
delegate.setHttpsConfigurator(httpsConfigurator);
158+
}
159+
160+
@Override
161+
public HttpsConfigurator getHttpsConfigurator() {
162+
return delegate.getHttpsConfigurator();
163+
}
137164

138165
@Override
139-
public void bind(InetSocketAddress inetSocketAddress, int i) throws IOException {
140-
server.bind(inetSocketAddress, i);
166+
public void bind(final InetSocketAddress inetSocketAddress, final int i) throws IOException {
167+
delegate.bind(inetSocketAddress, i);
141168
}
142169

143170
@Override
144171
public void start() {
145-
server.start();
172+
delegate.start();
146173
handler.onServerStart();
147174
}
148175

149176
@Override
150-
public void setExecutor(Executor executor) {
151-
server.setExecutor(executor);
177+
public void setExecutor(final Executor executor) {
178+
delegate.setExecutor(executor);
152179
}
153180

154181
@Override
155182
public Executor getExecutor() {
156-
return server.getExecutor();
183+
return delegate.getExecutor();
157184
}
158185

159186
@Override
160-
public void stop(int i) {
187+
public void stop(final int i) {
161188
handler.onServerStop();
162-
server.stop(i);
189+
delegate.stop(i);
163190
}
164191

165192
@Override
166-
public HttpContext createContext(String s, HttpHandler httpHandler) {
167-
return server.createContext(s, httpHandler);
193+
public HttpContext createContext(final String s, final HttpHandler httpHandler) {
194+
return delegate.createContext(s, httpHandler);
168195
}
169196

170197
@Override
171-
public HttpContext createContext(String s) {
172-
return server.createContext(s);
198+
public HttpContext createContext(final String s) {
199+
return delegate.createContext(s);
173200
}
174201

175202
@Override
176-
public void removeContext(String s) throws IllegalArgumentException {
177-
server.removeContext(s);
203+
public void removeContext(final String s) throws IllegalArgumentException {
204+
delegate.removeContext(s);
178205
}
179206

180207
@Override
181-
public void removeContext(HttpContext httpContext) {
182-
server.removeContext(httpContext);
208+
public void removeContext(final HttpContext httpContext) {
209+
delegate.removeContext(httpContext);
183210
}
184211

185212
@Override
186213
public InetSocketAddress getAddress() {
187-
return server.getAddress();
214+
return delegate.getAddress();
188215
}
189216
};
217+
}
190218

191-
if (start) {
192-
wrapper.start();
193-
}
219+
private static HttpServer createHttpServerWrapper(final HttpServer delegate, final JdkHttpHandlerContainer handler) {
220+
return new HttpServer() {
194221

195-
return wrapper;
222+
@Override
223+
public void bind(final InetSocketAddress inetSocketAddress, final int i) throws IOException {
224+
delegate.bind(inetSocketAddress, i);
225+
}
226+
227+
@Override
228+
public void start() {
229+
delegate.start();
230+
handler.onServerStart();
231+
}
232+
233+
@Override
234+
public void setExecutor(final Executor executor) {
235+
delegate.setExecutor(executor);
236+
}
237+
238+
@Override
239+
public Executor getExecutor() {
240+
return delegate.getExecutor();
241+
}
242+
243+
@Override
244+
public void stop(final int i) {
245+
handler.onServerStop();
246+
delegate.stop(i);
247+
}
248+
249+
@Override
250+
public HttpContext createContext(final String s, final HttpHandler httpHandler) {
251+
return delegate.createContext(s, httpHandler);
252+
}
253+
254+
@Override
255+
public HttpContext createContext(final String s) {
256+
return delegate.createContext(s);
257+
}
258+
259+
@Override
260+
public void removeContext(final String s) throws IllegalArgumentException {
261+
delegate.removeContext(s);
262+
}
263+
264+
@Override
265+
public void removeContext(final HttpContext httpContext) {
266+
delegate.removeContext(httpContext);
267+
}
268+
269+
@Override
270+
public InetSocketAddress getAddress() {
271+
return delegate.getAddress();
272+
}
273+
};
196274
}
197275

198276
/**
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.jdkhttp;
42+
43+
import javax.ws.rs.GET;
44+
import javax.ws.rs.Path;
45+
import javax.ws.rs.core.UriBuilder;
46+
47+
import org.glassfish.jersey.server.ResourceConfig;
48+
49+
import org.junit.After;
50+
import org.junit.Test;
51+
import static org.hamcrest.CoreMatchers.instanceOf;
52+
import static org.hamcrest.CoreMatchers.not;
53+
import static org.hamcrest.MatcherAssert.assertThat;
54+
55+
import com.sun.net.httpserver.HttpServer;
56+
import com.sun.net.httpserver.HttpsServer;
57+
58+
/**
59+
* Jdk Http Server basic tests.
60+
*
61+
* @author Michal Gajdos (michal.gajdos at oracle.com)
62+
*/
63+
public class BasicJdkHttpServerTest extends AbstractJdkHttpServerTester {
64+
65+
private HttpServer server;
66+
67+
@Path("/test")
68+
public static class TestResource {
69+
70+
@GET
71+
public String get() {
72+
return "test";
73+
}
74+
}
75+
76+
@Test
77+
public void testCreateHttpServer() throws Exception {
78+
server = JdkHttpServerFactory.createHttpServer(
79+
UriBuilder.fromUri("http://localhost/").port(getPort()).build(), new ResourceConfig(TestResource.class));
80+
81+
assertThat(server, instanceOf(HttpServer.class));
82+
assertThat(server, not(instanceOf(HttpsServer.class)));
83+
}
84+
85+
@Test
86+
public void testCreateHttpsServer() throws Exception {
87+
server = JdkHttpServerFactory.createHttpServer(
88+
UriBuilder.fromUri("https://localhost/").port(getPort()).build(), new ResourceConfig(TestResource.class));
89+
90+
assertThat(server, instanceOf(HttpsServer.class));
91+
}
92+
93+
@After
94+
public void tearDown() {
95+
if (server != null) {
96+
server.stop(3);
97+
server = null;
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)