Skip to content

Commit

Permalink
Merge "J-230: Default port for HTTPS is wrong."
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Lindenthal authored and Gerrit Code Review committed Jul 31, 2014
2 parents 680be0b + f22253f commit c48401b
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import org.glassfish.jersey.jdkhttp.internal.LocalizationMessages;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.internal.ConfigHelper;

import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
Expand Down Expand Up @@ -123,7 +124,7 @@ private static HttpServer createHttpServer(final URI uri, final JdkHttpHandlerCo

final boolean isHttp = scheme.equalsIgnoreCase("http");
final int port = (uri.getPort() == -1)
? (isHttp ? 80 : 143)
? (isHttp ? ConfigHelper.DEFAULT_HTTP_PORT : ConfigHelper.DEFAULT_HTTPS_PORT)
: uri.getPort();

final HttpServer server;
Expand Down
5 changes: 0 additions & 5 deletions containers/jersey-servlet-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@

import javax.ws.rs.ProcessingException;

import org.glassfish.jersey.server.ApplicationHandler;
import org.glassfish.jersey.jetty.internal.LocalizationMessages;
import org.glassfish.jersey.server.ContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.internal.ConfigHelper;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
Expand Down Expand Up @@ -76,8 +77,8 @@ private JettyHttpContainerFactory() {
/**
* Creates a {@link Server} instance that registers an {@link org.eclipse.jetty.server.Handler}.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used as
* context path, the rest will be ignored.
* @param uri uri on which the {@link org.glassfish.jersey.server.ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @return newly created {@link Server}.
*
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
Expand All @@ -90,8 +91,8 @@ public static Server createServer(final URI uri) throws ProcessingException {
/**
* Creates a {@link Server} instance that registers an {@link org.eclipse.jetty.server.Handler}.
*
* @param uri uri on which the {@link ApplicationHandler} will be deployed. Only first path segment will be used
* as context path, the rest will be ignored.
* @param uri uri on which the {@link org.glassfish.jersey.server.ApplicationHandler} will be deployed. Only first path
* segment will be used as context path, the rest will be ignored.
* @param start if set to false, server will not get started, which allows to configure the underlying transport
* layer, see above for details.
* @return newly created {@link Server}.
Expand Down Expand Up @@ -148,7 +149,8 @@ public static Server createServer(final URI uri, final ResourceConfig config)
* @throws ProcessingException in case of any failure when creating a new Jetty {@code Server} instance.
* @throws IllegalArgumentException if {@code uri} is {@code null}.
*/
public static Server createServer(final URI uri, final ResourceConfig configuration, final boolean start) throws ProcessingException {
public static Server createServer(final URI uri, final ResourceConfig configuration, final boolean start)
throws ProcessingException {
return createServer(uri, null, ContainerFactory.createContainer(JettyHttpContainer.class, configuration), start);
}

Expand All @@ -163,8 +165,9 @@ public static Server createServer(final URI uri, final ResourceConfig configurat
* for creating an Container that manages the root resources.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param sslContextFactory this is the SSL context factory used to configure SSL connector
* @param config the resource configuration.
Expand All @@ -185,8 +188,9 @@ public static Server createServer(final URI uri, final SslContextFactory sslCont
* classes referenced in the java classpath.
*
* @param uri the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param sslContextFactory this is the SSL context factory used to configure SSL connector
* @param handler the container that handles all HTTP requests
Expand All @@ -203,38 +207,38 @@ public static Server createServer(final URI uri,
final JettyHttpContainer handler,
final boolean start) {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
}
String scheme = uri.getScheme();
int defaultPort = 80;
final String scheme = uri.getScheme();
int defaultPort = ConfigHelper.DEFAULT_HTTP_PORT;

if (sslContextFactory == null) {
if (!scheme.equalsIgnoreCase("http")) {
throw new IllegalArgumentException("The URI scheme should be 'http' when not using SSL");
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
}
} else {
if (!scheme.equalsIgnoreCase("https")) {
throw new IllegalArgumentException("The URI scheme should be 'https' when using SSL");
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
}
defaultPort = 143; // default HTTPS port
defaultPort = ConfigHelper.DEFAULT_HTTPS_PORT;
}
final int port = (uri.getPort() == -1) ? defaultPort : uri.getPort();

Server server = new Server();
HttpConfiguration config = new HttpConfiguration();
final Server server = new Server();
final HttpConfiguration config = new HttpConfiguration();
if (sslContextFactory != null) {
config.setSecureScheme("https");
config.setSecurePort(port);
config.addCustomizer(new SecureRequestCustomizer());

ServerConnector https = new ServerConnector(server,
final ServerConnector https = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(config));
https.setPort(port);
server.setConnectors(new Connector[]{https});

} else {
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
final ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
http.setPort(port);
server.setConnectors(new Connector[]{http});
}
Expand All @@ -246,8 +250,8 @@ public static Server createServer(final URI uri,
try {
// Start the server.
server.start();
} catch (Exception e) {
throw new ProcessingException("Exception thrown when trying to create jetty server", e);
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_WHEN_CREATING_SERVER(), e);
}
}
return server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
* @author Arul Dhesiaseelan ([email protected])
* @author Marek Potociar (marek.potociar at oracle.com)
*/
public class JettyHttpContainerProvider implements ContainerProvider {
public final class JettyHttpContainerProvider implements ContainerProvider {

@Override
public <T> T createContainer(Class<T> type, Application application) throws ProcessingException {
public <T> T createContainer(final Class<T> type, final Application application) throws ProcessingException {
if (Handler.class == type || JettyHttpContainer.class == type) {
return type.cast(new JettyHttpContainer(application));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
#
# The contents of this file are subject to the terms of either the GNU
# General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -38,6 +38,10 @@
# holder.
#

unable.to.close.response=Unable to close response output
# {0} - status code; {1} - status reason message
exception.sending.error.response=I/O exception occurred while sending "{0}/{1}" error response.
error.when.creating.server=Exception thrown when trying to create jetty server.
unable.to.close.response=Unable to close response output.
uri.cannot.be.null=The URI must not be null.
wrong.scheme.when.using.http=The URI scheme should be 'http' when not using SSL.
wrong.scheme.when.using.https=The URI scheme should be 'https' when using SSL.
10 changes: 10 additions & 0 deletions containers/simple-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@

<build>
<plugins>
<plugin>
<groupId>com.sun.istack</groupId>
<artifactId>maven-istack-commons-plugin</artifactId>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<inherited>true</inherited>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

import org.glassfish.jersey.internal.util.collection.UnsafeValue;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.internal.ConfigHelper;
import org.glassfish.jersey.simple.internal.LocalizationMessages;

import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerServer;
Expand Down Expand Up @@ -120,8 +122,9 @@ public static SimpleServer create(final URI address, final ResourceConfig config
* resource configuration.
*
* @param address the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param context this is the SSL context used for SSL connections.
* @param config the resource configuration.
Expand All @@ -139,8 +142,9 @@ public static SimpleServer create(final URI address, final SSLContext context, f
* resource configuration.
*
* @param address the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param context this is the SSL context used for SSL connections.
* @param config the resource configuration.
Expand All @@ -150,7 +154,8 @@ public static SimpleServer create(final URI address, final SSLContext context, f
* @throws ProcessingException thrown when problems during server creation.
* @throws IllegalArgumentException if {@code address} is {@code null}.
*/
public static SimpleServer create(final URI address, final SSLContext context, final ResourceConfig config, final int count, final int select) {
public static SimpleServer create(final URI address, final SSLContext context,
final ResourceConfig config, final int count, final int select) {
return create(address, context, new SimpleContainer(config), count, select);
}

Expand All @@ -160,8 +165,9 @@ public static SimpleServer create(final URI address, final SSLContext context, f
* classes referenced in the java classpath.
*
* @param address the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param context this is the SSL context used for SSL connections.
* @param container the container that handles all HTTP requests.
Expand All @@ -170,9 +176,8 @@ public static SimpleServer create(final URI address, final SSLContext context, f
* @throws IllegalArgumentException if {@code address} is {@code null}.
*/
public static SimpleServer create(final URI address,
final SSLContext context,
final SimpleContainer container) {

final SSLContext context,
final SimpleContainer container) {
return _create(address, context, container, new UnsafeValue<Server, IOException>() {
@Override
public Server get() throws IOException {
Expand All @@ -187,8 +192,9 @@ public Server get() throws IOException {
* classes referenced in the java classpath.
*
* @param address the URI to create the http server. The URI scheme must be
* equal to "https". The URI user information and host
* are ignored If the URI port is not present then port 143 will be
* equal to {@code https}. The URI user information and host
* are ignored. If the URI port is not present then port
* {@value org.glassfish.jersey.server.internal.ConfigHelper#DEFAULT_HTTPS_PORT} will be
* used. The URI path, query and fragment components are ignored.
* @param context this is the SSL context used for SSL connections.
* @param container the container that handles all HTTP requests.
Expand Down Expand Up @@ -217,20 +223,20 @@ private static SimpleServer _create(final URI address,
final SimpleContainer container,
final UnsafeValue<Server, IOException> serverProvider) throws ProcessingException {
if (address == null) {
throw new IllegalArgumentException("The URI must not be null");
throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
}
final String scheme = address.getScheme();
int defaultPort = 80;
int defaultPort = ConfigHelper.DEFAULT_HTTP_PORT;

if (context == null) {
if (!scheme.equalsIgnoreCase("http")) {
throw new IllegalArgumentException("The URI scheme should be 'http' when not using SSL");
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTP());
}
} else {
if (!scheme.equalsIgnoreCase("https")) {
throw new IllegalArgumentException("The URI scheme should be 'https' when using SSL");
throw new IllegalArgumentException(LocalizationMessages.WRONG_SCHEME_WHEN_USING_HTTPS());
}
defaultPort = 143; // default HTTPS port
defaultPort = ConfigHelper.DEFAULT_HTTPS_PORT;
}
int port = address.getPort();

Expand All @@ -256,11 +262,11 @@ public void close() throws IOException {

@Override
public int getPort() {
return ((InetSocketAddress)socketAddr).getPort();
return ((InetSocketAddress) socketAddr).getPort();
}
};
} catch (final IOException ex) {
throw new ProcessingException("IOException thrown when trying to create simple server", ex);
throw new ProcessingException(LocalizationMessages.ERROR_WHEN_CREATING_SERVER(), ex);
}
}
}
Loading

0 comments on commit c48401b

Please sign in to comment.