Returning Objects in Metamug Plugins

The process() method of the Processable classes can return Java objects. The Java classes need to be created using JAXB annotations. This helps Metamug framework to convert the returned objects to JSON/XML.

Let us consider the following Java classes

Customer.java

@XmlRootElement
public class Customer {

    private String name;
    @XmlElement(name = "contact")
    private Contact contact;

    public void setName(String n) {
        name = n;
    }

    public void setContact(String phone, String email) {
        contact = new Contact();
        contact.setEmail(email);
        contact.setPhone(phone);
    }

    public String getName() {
        return name;
    }

    public Contact getContact() {
        return contact;
    }    
 }

Contact.java

public class Contact {
    private String phone;
    private String email;

    public void setPhone(String ph) {
        phone = ph;
    }

    public void setEmail(String e) {
        email = e;
    }

    public String getPhone() {
        return phone;
    }

    public String getEmail() {
        return email;
    }
}

Rules for creating classes

Returning Objects

Objects of the JAXB annotated classes can be returned in the following manner

public Object process(Map<String, String> params, DataSource ds, Map<String, String> requestHeaders) {

    Customer customer = new Customer();
    customer.setName("John");
    customer.setContact("8080808080", "john@example.com");

    return customer;

}

The returned object will get converted to JSON/XML depending upon the value of the Accept header.

JSON response (Accept: application/json)

{
    "customer": {
        "contact": {
            "phone": "8080808080",
            "email": "john@example.com"
        },
        "name": "John"
    }
}

XML response (Accept: application/xml)

<response>
    <customer>
        <contact>
            <email>john@example.com</email>
            <phone>8080808080</phone>
        </contact>
        <name>John</name>
    </customer>
</response>

Using Persist for Returned Objects

The values of returned object can be accessed in subsequent tags using dot-notation by setting the persist attribute as true. The persist feature is available only for JSON response.

<Request method="GET">
    <Execute classname="com.metamug.RequestExample" persist="true"/>
    <Query verbose="true"> SELECT $customer.name </Query>
</Request>

Returning List of Objects

List of Objects can be returned in the following manner

public Object process(Map<String, String> params, DataSource ds, Map<String, String> requestHeaders) {

    Customer customer = new Customer();
    customer.setName("John");
    customer.setContact("8080808080", "john@example.com");

    Customer customer2 = new Customer();
    customer2.setName("Jane");
    customer2.setContact("1212121212", "jane@example.com");

    List<Customer> customers = new ArrayList<>();
    customers.add(customer);
    customers.add(customer2);

    return customers;
}

The corresponding HTTP response will be as follows

JSON response (Accept: application/json)

[
    {
        "customer": {
            "contact": {
                "phone": "8080808080",
                "email": "john@example.com"
            },
            "name": "John"
        }
    },
    {
        "customer": {
            "contact": {
                "phone": "1212121212",
                "email": "jane@example.com"
            },
            "name": "Jane"
        }
    }
]

XML response (Accept: application/xml)

<response>
    <customer>
        <contact>
            <email>john@example.com</email>
            <phone>8080808080</phone>
        </contact>
        <name>John</name>
    </customer>
    <customer>
        <contact>
            <email>jane@example.com</email>
            <phone>1212121212</phone>
        </contact>
        <name>Jane</name>
    </customer>
</response>