Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add binding host support to jetty integration #374

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@Configuration(prefix="governator.jetty.embedded")
public interface Archaius2JettyConfig extends JettyConfig {

String getHost();

@DefaultValue("8080")
int getPort();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
@Singleton
public class DefaultJettyConfig implements JettyConfig {
private int port = 8080;

private String host;

// Where static files live. We pass this to Jetty for class path scanning to find the exact directory.
// The default is to use resources supported by the servlet 3.0 spec.
Expand All @@ -14,6 +16,11 @@ public class DefaultJettyConfig implements JettyConfig {

private String webAppContextPath = "/";

@Override
public String getHost() {
return host;
}

@Override
public int getPort() {
return port;
Expand All @@ -39,6 +46,11 @@ public String getWebAppContextPath() {
return webAppContextPath;
}

public DefaultJettyConfig setHost(String host) {
this.host = host;
return this;
}

public DefaultJettyConfig setPort(int port) {
this.port = port;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public interface JettyConfig {

String getHost();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add javadoc for this, especially since it can be null?


int getPort();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package com.netflix.governator.guice.jetty;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.EnumSet;
import java.util.Set;

Expand Down Expand Up @@ -183,7 +185,12 @@ protected void configure() {
@Singleton
private Server getServer(OptionalJettyConfig optionalConfig, Set<JettyConnectorProvider> jettyConnectors) {
JettyConfig config = optionalConfig.getJettyConfig();
Server server = new Server(config.getPort());
Server server;
if (config.getHost() != null) {
server = new Server(new InetSocketAddress(config.getHost(), config.getPort()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these tabs?

} else {
server = new Server(config.getPort());
}
Resource staticResourceBase = Resource.newClassPathResource(config.getStaticResourceBase());
if (staticResourceBase != null) {
// Set up a full web app since we have static content. We require the app to have its static content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,37 @@ JettyConfig getConfig() {

injector.close();
}

@Test
public void testExplicitHostConnectorBinding() throws Exception {
LifecycleInjector injector = InjectorBuilder.fromModules(
new SampleServletModule(),
new ShutdownHookModule(),
Modules.override(new JettyModule())
.with(new AbstractModule() {
@Override
protected void configure() {
}

@Provides
JettyConfig getConfig() {
// Use ephemeral ports
return new DefaultJettyConfig().setPort(0).setHost("127.0.0.1");
}
})
).createInjector();

Server server = injector.getInstance(Server.class);
Assert.assertEquals(1, server.getConnectors().length);

int port = ((ServerConnector)server.getConnectors()[0]).getLocalPort();

// Do a plaintext GET and verify that the default connector works
String response = doGet(String.format("http://127.0.0.1:%d/", port), null);
Assert.assertTrue(response.startsWith("hello "));

injector.close();
}

private static String doGet(String url, KeyStore sslTrustStore) throws Exception {

Expand Down