3.5. Security (user) Resource

To finish up, the /user link indicated in Section 3.1, “HomePageResource” corresponds to the UserResource:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

public interface UserResource {

  @GET
  @Produces( { "application/xhtml+xml", "text/html" })
  public String user();
}

The implementation of this interface in Restful Objects viewer (UserResourceImpl) also defines a @Path("/user") for the class as a whole. Taken together this therefore defines a URL in the form /user supporting the GET method.

Note

I believe that the @Path("/user") annotation should reside on the interface, not the implementation. This seems to be a limitation with RestEasy 1.0.2, the underlying library used by Restful Objects.

Here's the resource that's returned, as shown in a browser:

The first section (as ever) lists the current user, while the final section lists out the roles for the current user. The XHTML for this is:

<?xml version="1.0"?>
<html>
  <head><title>User</title></head>
  <body id="body">
    <div>
      <p>Logged in as</p>
      <ul class="nof-session">
        <li>
          <a href="/user" rel="user" rev="resource" class="nof-user">sven</a>
        </li>
      </ul>
    </div>
    ...
    <div class="nof-section">
      <p class="nof-section">Roles</p>
      <ul class="nof-roles">
        <li>
          <p class="nof-role">role1</p>
        </li>
      </ul>
    </div>
  </body>
</html>

Again, we can use XPath to pull back the resources: