Skip to content

Commit

Permalink
Merge pull request #560 from joschi/resteasy-netty-hostname
Browse files Browse the repository at this point in the history
Allow NettyJaxrsServer to bind to a specific hostname
  • Loading branch information
patriot1burke committed Sep 11, 2014
2 parents 82029e6 + 8a1ab36 commit 81b12aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -31,6 +31,7 @@ public class NettyJaxrsServer implements EmbeddedJaxrsServer
{
protected ServerBootstrap bootstrap;
protected Channel channel;
protected String hostname = null;
protected int port = 8080;
protected ResteasyDeployment deployment = new ResteasyDeployment();
protected String root = "";
Expand Down Expand Up @@ -85,7 +86,15 @@ public void setKeepAlive(boolean isKeepAlive)
{
this.isKeepAlive = isKeepAlive;
}


public String getHostname() {
return hostname;
}

public void setHostname(String hostname) {
this.hostname = hostname;
}

public int getPort()
{
return port;
Expand Down Expand Up @@ -144,7 +153,14 @@ public void start()
bootstrap.setPipelineFactory(factory);

// Bind and start to accept incoming connections.
channel = bootstrap.bind(new InetSocketAddress(port));
final InetSocketAddress socketAddress;
if(null == hostname || hostname.isEmpty()) {
socketAddress = new InetSocketAddress(port);
} else {
socketAddress = new InetSocketAddress(hostname, port);
}

channel = bootstrap.bind(socketAddress);
allChannels.add(channel);
}

Expand Down
Expand Up @@ -19,6 +19,7 @@

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.net.InetSocketAddress;

/**
* An HTTP server that sends back the content of the received HTTP request
Expand All @@ -33,6 +34,7 @@
public class NettyJaxrsServer implements EmbeddedJaxrsServer
{
protected ServerBootstrap bootstrap = new ServerBootstrap();
protected String hostname = null;
protected int port = 8080;
protected ResteasyDeployment deployment = new ResteasyDeployment();
protected String root = "";
Expand Down Expand Up @@ -82,6 +84,14 @@ public void setMaxRequestSize(int maxRequestSize)
this.maxRequestSize = maxRequestSize;
}

public String getHostname() {
return hostname;
}

public void setHostname(String hostname) {
this.hostname = hostname;
}

public int getPort()
{
return port;
Expand Down Expand Up @@ -174,7 +184,14 @@ public void initChannel(SocketChannel ch) throws Exception {
.childOption(ChannelOption.SO_KEEPALIVE, true);
}

bootstrap.bind(port).syncUninterruptibly();
final InetSocketAddress socketAddress;
if(null == hostname || hostname.isEmpty()) {
socketAddress = new InetSocketAddress(port);
} else {
socketAddress = new InetSocketAddress(hostname, port);
}

bootstrap.bind(socketAddress).syncUninterruptibly();
}

@Override
Expand Down

0 comments on commit 81b12aa

Please sign in to comment.