My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Representation  
How to make a Representation, sent through the network
Featured
Updated Oct 4, 2011 by nzo...@gmail.com

Simple creation of a Resource Representation

Let's say we have :

 User johnDoe = new User(1L, "john.doe@robustaweb.com", "John", "Doe");

where User implements the Resource interface.

johnDoe.getRepresentation().toString() will return :

  <user>
    <id>1</id>
    <email>john.doe@robustaweb.com</email>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
  </user>

This is often enough to get informations on the precious concept of User. But you still make easy changes with :

Representation representation = johnDoe.getRepresentation();
representation.set("username", representation.get("email"));
System.out.println( representation );

This adds the email as username in the Xml representation :

  <user>
    <id>1</id>
    <email>john.doe@robustaweb.com</email>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
    <username>john.doe@robustaweb.com</username>
  </user>

Minimal Configuration

A major principle of the Robusta Web Library is that serialization is NOT automatic. You should always know what is going on. The simplest serializer is one that read all fields and returns the ( fieldName, fieldValue.toString() ) couple.

MyRobusta.setDefaultRepresentationFactory(JdomRepresentation.getRepresentationManager());
  //Representations will be based on a JDOM document ; jdom.jar must be in the classpath

Let's describe the User class

public class User implements Resource<Long>{//IdType is a Long

    long id;
    String email;
    String firstname;
    String lastname;

    public User(long id, String email, String firstname, String lastname) {
        this.id = id;
        this.email = email;
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public Representation getRepresentation() throws RepresentationException {
        //Using default RepresentationManager
        return MyRobusta.getDefaultSerializer().getRepresentation(this);
        //Or using a specific one
        //return new UserRepresentationManager().getRepresentation(this);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }    
    
    public String getRelativeURI() {
        return "user/"+id;
    }
}

Customization of a Resource Representation

Let's describe a custom Representation of a User

public class UserRepresentationManager implements RepresentationManager<User>{

   public Representation getRepresentation(User user){
      //fieldsAndValues looks like ["id", 2, "firstname", "John", "lastname", "Doe"]
      Object[] fieldsAndValues = ReflectionUtilsJava.getFieldValueCouples(user).flat();
      //We build an Xml representation, choosing 'user' as nodeName
      return new JdomRepresentation("user", fieldsAndValues);
   }

   public User getResource (Representation rp){
      //throw new UnsupportedOperation("not supported yet"); //This method is facultative
      return new User(
         rp.getNumber("id"), rp.get("email"), rp.get("firstname"), rp.get("lastname");
     );
   }
}


Sign in to add a comment
Powered by Google Project Hosting