0

I have a scenario where we support 2 different types of authenticated users (UserTypeA, UserTypeB), but they will never be used in the same server environment. Right now, we use 2 different url paths /path/usertypea/list vs /path/usertypeb/list. We would like to make them use the same path if possible, for example /path/list, and have an environment variable be the condition to know where to route the traffic. The parameters for each user type aren't exactly the same, there are some differences in how the data is organized. We're using Jersey.

I've tried a few things like Singleton classes: https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/user-guide.html#d0e2650 / https://stackoverflow.com/a/33585724/12183373 but it never routes the value, it just returns the name of the class instead of the JSON payload I'm expecting.

Here's some of the code:

@Path("/list")
public class GlobalSegmentServiceRouter {
    @GET
    @Produces("application/json")
    public Class<?> findAll() {
        boolean isUserTypeA = false;
        if (isUserTypeA) {
            return UserTypeAService.class;
        } else {
            return UserTypeBService.class;
        }
    }
}

Then I have 2 separate class files for the following:

@Singleton
public class UserTypeAService {
    public List<String> findAll(/*Parameters for A*/) {
        // Do work here for User Type A
    }
}
@Singleton
public class UserTypeBService {
    public List<String> findAll(/*Parameters for B*/) {
        // Do work here for User Type B
    }
}

When I try and hit the endpoint, this is the response I get:

"com.test.services.UserTypeAService"

Any suggestions on how to accomplish this?

  • Even though you mentioned what you've tried is not working, if you could post a code sample of that then it would be helpful to get your issue resolved. Perhaps your solution is close, but just needs a few tweaks. – CEH Oct 08 '19 at 16:27
  • Done! Sorry, should have done that at the beginning. – Jason Parrish Oct 08 '19 at 16:35
  • No problem, you posted good samples and these add a lot of clarity to your question. – CEH Oct 08 '19 at 16:37

1 Answers1

0

add some flag for checking which kind of user is logged in to a custom principal impl. Then you can inject the current user and then call UserTypeAService.findAll or UserTypeBService.findAll in your method.

@GET
@Path("/path/list")
public String yourMethod(@Context SecurityContext securityContext)
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • If the parameters for each user type were the same this would be a very good option. But the parameter lists are a little different. Is there a good way to handle that? Sorry, very new to Jersey here. – Jason Parrish Oct 08 '19 at 16:42
  • you could use a post request and pass the params in a pojo with optional fields or a map. Or use optional query params... – Marc Stroebel Oct 08 '19 at 16:47