How to do it...

You need to perform the following steps to complete this recipe:

  1. We are going to build a JAX-RS-based application, so we will start by preparing the application:
@ApplicationPath("webresources")
public class Application extends javax.ws.rs.core.Application {
}
  1. Then, we will create a User class as our main object:
public class User implements Serializable {

private String name;
private String email;

//DO NOT FORGET TO ADD THE GETTERS AND SETTERS
}
  1. Our User class doesn't have a default constructor, so Jakarta CDI doesn't know how to construct the class when it tries to inject it. Therefore, we need to create a factory class and use the @Produces annotation over its methods:
public class UserFactory implements Serializable{

@Produces
public User getUser() {
return new User("Elder Moraes", "[email protected]");
}

}

  1. Let's create an enumeration so that we can list our profile types:
public enum ProfileType {
ADMIN, OPERATOR;
}
  1. Next, we will create a custom annotation:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface Profile {
ProfileType value();
}
  1. Add this to an interface in order to prototype the user profile behavior:
public interface UserProfile {
ProfileType type();
}
  1. Now that we have defined the profile list and its behavior with respect to the user, we can give it a proper implementation for an admin profile:
@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is admin");
return ProfileType.ADMIN;
}
}
  1. The same can be done for an operator profile:
@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is operator");
return ProfileType.OPERATOR;
}
}
  1. Then, we need to create a REST endpoint by injecting all the objects that we are going to use into it:
@Path("userservice/")
@RequestScoped
public class UserService {

@Inject
private User user;

@Inject
@Profile(ProfileType.ADMIN)
private UserProfile userProfileAdmin;

@Inject
@Profile(ProfileType.OPERATOR)
private UserProfile userProfileOperator;

@Inject
private UserProfile userProfileDefault;

@Inject
private Event<User> userEvent;

...
  1. This method injects the user through Jakarta CDI and sends them to the result page:
    @GET
@Path("getUser")
public Response getUser(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result", user);
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. The following code does the same but with an admin profile:
    @GET
@Path("getProfileAdmin")
public Response getProfileAdmin(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileAdmin.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}

  1. The following code does the same but with an operator profile:
    @GET
@Path("getProfileOperator")
public Response getProfileOperator(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileOperator.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. Finally, we send the default profile to the result page:
   @GET
@Path("getProfileDefault")
public Response getProfileDefault(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileDefault.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. We use the fireUserEvents method to fire an event and async events over a previously injected User object:
    private ProfileType fireUserEvents(ProfileType type){
userEvent.fire(user);
userEvent.fireAsync(user);
return type;
}

public void sendUserNotification(@Observes User user){
System.out.println("sendUserNotification: " + user);
}

public void sendUserNotificationAsync(@ObservesAsync User user){
System.out.println("sendUserNotificationAsync: " + user);
}
  1. Now, we need to build a page to call each endpoint method:
<body>
<a href="http://localhost:8080/ch02-
cdi/webresources/userservice/getUser">getUser</a>
<br>
<a href="http://localhost:8080/ch02-
cdi/webresources/userservice/getProfileAdmin">getProfileAdmin</a>
<br>
<a href="http://localhost:8080/ch02-
cdi/webresources/userservice/getProfileOperator">getProfileOperator</a>
<br>
<a href="http://localhost:8080/ch02-
cdi/webresources/userservice/getProfileDefault">getProfileDefault</a>
</body>
  1. Finally, we use an expression language to print the result on the result page:
<body>
<h1>${result}</h1>
<a href="javascript:window.history.back();">Back</a>
</body>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.159.10