1

My Problem scenario: I have a single JEE7 web application WAR having RestEasy JAX-RS webservices available (for different type of clients) on two distinctly separate application paths. I am using WildFly-8.2.0.Final for deployment. Everything is fine.

The Problem: I want to deploy the two distinctly separate type of JAX-RS web services on two mutually exclusive ports of single instance of WildFly server, let's say TYPE_A_WEB_SERVICES on port 9555 and TYPE_B_WEB_SEREVICES on port 10888 while the web application being deployed on port 80 (or may on be 8080). I do not want both TYPE_A_WEB_SERVICES & TYPE_B_WEB_SERVICES be available web application's port 80 (or may on be 8080) either. Is this configuration possible on single instance of WildFly?

yayayokoho3
  • 1,209
  • 2
  • 16
  • 40

1 Answers1

2

Yes, it is possible and it includes configuration of WildFly to listen on two http ports and a bit of Java EE programming.

Wildfly configuration

  1. Create socket for alternative port

    <socket-binding-group ...>
        ...
        <socket-binding name="http" port="${jboss.http.port:8080}"/>
        <!-- add this one -->
        <socket-binding name="http-alternative" port="${jboss.http.port:8888}"/>
        ...
    </socket-binding-group>
    
  2. Define http-listener on alternative port

    <subsystem xmlns="urn:jboss:domain:undertow:1.2">
             ...
             <server name="default-server">
                 <http-listener name="default" socket-binding="http"/>
                 <!-- add this one -->
                 <http-listener name="http-alt" socket-binding="http-alternative"/>
                 ...
             </server>
    </subsystem>
    

Check ports in JAX-RS

For this I used Java EE @Interceptors. I defined 2 endpoints (Endpoint1, Endpoint2) within one application App. Every invocation of methods of endpoints is intercepted by PortDetectionInterceptor which checks if the endpoint is called from predefined ports.

App.java

 package net.stankay.test;

 import javax.ws.rs.ApplicationPath;
 import javax.ws.rs.core.Application;

 @ApplicationPath("/rest")
 public class App extends Application {}

Endpoint1.java

package net.stankay.test;

import javax.interceptor.Interceptors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Interceptors(PortDetectionInterceptor.class)
@Path("/endpoint1")
public class Endpoint1 {

    @GET
    public String hi() {
        return "hi";
    }
}

Endpoint2.java

 package net.stankay.test;

 import javax.interceptor.Interceptors;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;

 @Interceptors(PortDetectionInterceptor.class)
 @Path("/endpoint2")
 public class Endpoint2 {

    @GET
    public String hello() {
        return "hello";
    }
 }

PortDetectionInterceptor.java

 package net.stankay.test;

 import javax.inject.Inject;
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.InvocationContext;
 import javax.servlet.http.HttpServletRequest;

 public class PortDetectionInterceptor {

    @Inject 
    private HttpServletRequest httpRequest;

    @AroundInvoke
    public Object detectPort(InvocationContext ctx) throws Exception {
        try {
            String restEndpoint = ctx.getTarget().getClass().getName();
            int restPort = httpRequest.getLocalPort();

            if (restEndpoint.contains("Endpoint1") && restPort != 8080) {
                throw new RuntimeException("Please access Endpoint1 only via port 8080");
            }

            if (restEndpoint.contains("Endpoint2") && restPort != 8888) {
                throw new RuntimeException("Please access Endpoint2 only via port 8888");
            }

            return ctx.proceed();
        } catch (Exception e) {
            return String.format("{ \"error\" : \"%s\" }", e.getMessage());
        }
    }
 }
Mišo Stankay
  • 339
  • 1
  • 8