Implementing the HelloWorld class

The HelloWorld class is a JAX-RS resource that responds to requests sent to the /helloworld path. This class has two methods with the respective helloWorldWithDeploymentDescriptor() and helloWorldWithProgrammatically() signatures. In helloWorldWithDeploymentDescriptor(), the authentication validation is oriented by the deployment descriptor configuration, and in helloWorldWithProgrammatically(), the authentication validation is oriented by codes written by the developer:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;


@Path("helloworld")
public class HelloWorld {

@Context
private SecurityContext securityContext;

@GET
@Path("/deploymentdescriptor")
public Response helloWorldWithDeploymentDescriptor(){

return Response
.ok("Hello World. Welcome to App with validation by deployment descriptor!")
.build();

}

@GET
@Path("/programmatically")
public Response helloWorldWithProgrammatically() {

if (!securityContext.isUserInRole("user")) {
return Response.status(401).header("WWW-Authenticate", "Basic").build();
}

return Response
.ok("Hello World. Welcome to App with validation by programmatically.")
.build();
}

}

The preceding code includes the helloWorldWithDeploymentDescriptor() method, which contains the code that only responds to the client because the validation of authentication is done by the Java EE context without using any code together with the core business logic. In the following code, we have a snippet of the HelloWorld class that includes the helloWorldWithDeploymentDescriptor() method:

    @GET
@Path("/deploymentdescriptor")
public Response helloWorldWithDeploymentDescriptor(){

return Response
.ok("Hello World. Welcome to App with validation by deployment descriptor!")
.build();

}

In the following code, we have a snippet of the HelloWorld class that includes a helloWorldWithProgrammatically() method that uses a code to validate the role associated with each user that sent a request:

@GET
@Path("/programmatically")
public Response helloWorldWithProgrammatically() {

if (!securityContext.isUserInRole("user")) {
return Response.status(401).header("WWW-Authenticate", "Basic").build();
}

return Response
.ok("Hello World. Welcome to App with validation by programmatically.")
.build();
}

To show the dialog box in the browser in a programmatic manner, use the following code:

 if (!securityContext.isUserInRole("user")) {
return Response.status(401).header("WWW-Authenticate",
"Basic").build();
}
..................Content has been hidden....................

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