[jbossseam-issues] [JBoss JIRA] Created: (JBSEAM-3988) Support for mapping RESTful XHTML resource representations with Facelets templates

Christian Bauer (JIRA) jira-events at lists.jboss.org
Thu Mar 5 03:00:22 EST 2009


Support for mapping RESTful XHTML resource representations with Facelets templates
----------------------------------------------------------------------------------

                 Key: JBSEAM-3988
                 URL: https://jira.jboss.org/jira/browse/JBSEAM-3988
             Project: Seam
          Issue Type: Feature Request
          Components: WS
            Reporter: Christian Bauer
            Assignee: Jozef Hartinger


What I wrote a while ago about this feature:

I'm thinking about a special provider that can marshal an object graph into an XHTML (not just XML) response body and back from an XHTML request body to an object graph. This is about the "connectedness" of resources, see the O'Reilly REST book. As a developer I should write a Facelets XHTML template that defines the transformation. Imagine that you write it the same way you write a JSF template today, with bidirectional binding using EL expressions, from object getter/setter pair to XML attribute or element value. We can even have built-in Facelets "widgets" that render certain microformats. Seam has some machinery for this already but we might need an extra interceptor on resource methods to trigger the transformation. Or we use Seams pages.xml navigation rules ("when this resource method finishes, render this template").

Later I did some prototype experiments - note that this only considers uni-directional mapping for GET, handling POST/PUT resource representation is more difficult:


package org.jboss.seam.resteasy;

import java.lang.annotation.*;

/**
 * @author Christian Bauer
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface FaceletsXhtmlResponse {
    String template();
}


package org.jboss.seam.resteasy;

import org.jboss.seam.core.Interpolator;
import org.jboss.seam.faces.Renderer;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
 * @author Christian Bauer
 */
@Provider
@ProduceMime("application/xhtml+xml")
public class FaceletsXhtmlProvider implements MessageBodyWriter<Object> {

    Log log = Logging.getLog(FaceletsXhtmlProvider.class);

    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(FaceletsXhtmlResponse.class)) {
                return true;
            }
        }
        return false;
    }

    public long getSize(Object o) {
        return -1;
    }

    public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations,
                        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
            throws IOException {
        log.debug("writing XHTML response for entity type: " + type.getName());
        FaceletsXhtmlResponse respAnnotation = null;
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(FaceletsXhtmlResponse.class))
                respAnnotation = (FaceletsXhtmlResponse) annotation;
        }
        if (respAnnotation == null) throw new IllegalStateException("@FaceletsXhtmlResponse annotation disappeared");
        String template = getTemplatePath(respAnnotation);
        log.debug("rendering XHTML template: " + template);
        String output = Renderer.instance().render(template);
        entityStream.write(output.getBytes());
    }

    protected String getTemplatePath(FaceletsXhtmlResponse annotation) {
        return isTemplatePathInterpolated() 
                ? Interpolator.instance().interpolate(annotation.template())
                : annotation.template();
    }

    protected boolean isTemplatePathInterpolated() {
        return true;
    }
}



@Name("customerResource")
@Path("/customer")
public class CustomerResource {

    @GET
    @ProduceMime("application/xhtml+xml")
    @FaceletsXhtmlResponse(
        template = "#{somePath}/customerTemplate.xhtml"
    )
    @Out(value = "customers", scope = ScopeType.EVENT)
    public List<Customer> getCustomers() {
        return mockCustomers();
    }
}


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:t="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<head>
    <title>Customers</title>
</head>
<body>
<ul>
    <t:repeat var="cust" value="#{customers}">
        <li>
            <h:outputText value="#{cust.name}"/>
        </li>
    </t:repeat>
</ul>
</body>
</html>



-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the seam-issues mailing list