Chapter 3. Resources

Table of Contents

3.1. HomePageResource
3.2. Services Resource
3.3. Object Resource
3.3.1. Objects
3.3.2. Properties
3.3.3. Collections
3.3.4. Actions
3.4. Metamodel (specs) Resource
3.4.1. All Classes
3.4.2. Class (NakedObjectSpecification)
3.4.3. Class Members (NakedObjectMember)
3.4.4. Facets
3.5. Security (user) Resource

Understanding how to interact with resources and how to interpret their representations is central to the RESTful approach. This chapter describes in detail the resources that are available; in effect, the API for using domain objects exposed using Restful Objects.

In order to invoke a HTTP method on a resource, the URL must be constructed correctly. After that, the client code then needs to handle the response.

Each of the resources is defined as interfaces annotated using javax.ws.rs (JAX-RS) annotations. JAX-RS is the Java API for Restful Web Services, part of Java EE 6. Its annotations are used by JAX-RS libraries such as RestEasy, or Jersey (the latter is the reference implementation). These libraries expose the resources as endpoints and route requests through to server-side methods (implemented by Restful Objects).

As such, we can use these interfaces as a way of describing the resources provided by Restful Objects. Let's go through each in turn.

3.1. HomePageResource

To start with, we have HomePageResource:

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

public interface HomePageResource {

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

Because there is no @javax.ws.rs.Path annotation, this in effect says that the root URL "/" (eg http://localhost:8080/) is a valid resource. What you'll get back is an XHTML page that identifies the current user (in exploration mode this is always hard-coded), and then lists the resources available: the services (ie repositories), the specifications (metamodel), and details on the current user.

These blocks are always present on every page.

Meanwhile the raw XHTML - what your client code must parse - looks something like:

<?xml version="1.0"?>
<html>
  <head><title>Home Page</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">Resources</p>
      <ul class="nof-resources">
        <li>
          <a href="/services" rel="services" rev="resources" class="nof-resource">
            Services
          </a>
        </li>
        <li>
          <a href="/specs" rel="specs" rev="resources" class="nof-resource">
            Specifications (MetaModel)
          </a>
        </li>
        <li>
          <a href="/user" rel="user" rev="resources" class="nof-resource">
            User (Security)
          </a>
        </li>
      </ul>
    </div>
  </body>
</html>

Note the use of class attributes to distinguish the different types of properties. For example, the XPath expression //a[@class='nof-resource']/@href will return the links to all resources available. One of these - /services - is the list of services, so lets look at that next.

Note

Depending on feedback, the format of the XHTML may evolve in the future, eg to add in further class attributes.