1

I've so far been successful in setting up a basic web service using Apache Wink. This includes returning Atom, JSON, HTML, XHTML, XML, and plaintext media types, as per the samples provided. I've also been able to successfully use a MessageBodyWriter to "manually" generate the XHTML output. So far, great. I'm happy to return most of the media types via the existing Wink mechanism.

What I'm trying to do now is have the returned XHTML content use a JSP. I.e., I'd like to use a JSP as an output template, sending the POJO through as a parameter to populate the fields in the JSP. Below is some pseudocode for what I've got right now.

@Path("{id}")
@GET
@Produces({MediaType.APPLICATION_XHTML_XML})
public Response getXhtml( @PathParam("id") String id )
{
    try { 
        MyBean mybean = service.getBean(id);
        return Response.ok(new MyAsset(mybean))
                .location(new URI(baseurl+"Output.jsp"))
                .type(MediaType.APPLICATION_XHTML_XML).build();
    } catch ( Exception e ) {
        throw new WebApplicationException(e,Status.INTERNAL_SERVER_ERROR);
    }
}

It just seems to ignore the JSP entirely. And if I finally do figure out how, I'll need to know how to pass the POJO as a parameter. I know there's something I'm missing here, as I assume Apache Wink can interoperate with a JSP-based web service. The Wink documentation is generally good, but I couldn't find anything on this. Thanks for any assistance, ideally a link to a working example.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
Ichiro Furusato
  • 620
  • 6
  • 12

2 Answers2

0

There is no built-in mechanism to pass POJO as parameter. You need to build the URI yourself. Also, I guess you want to send redirect to your jsp and not just 200 OK. So you can do something like this:

@Path("{id}")
@GET
@Produces({MediaType.APPLICATION_XHTML_XML})
public Response getXhtml( @PathParam("id") String id )
{
    try { 
        MyBean mybean = service.getBean(id);
        return Response.seeOther(UriBuilder.fromUri(baseurl+"Output.jsp")
                 .replaceQuery(convertMyBeanToQuery(mybean)).build())
                .type(MediaType.APPLICATION_XHTML_XML).build();
    } catch ( Exception e ) {
        throw new WebApplicationException(e,Status.INTERNAL_SERVER_ERROR);
    }
}

You need to implement convertMyBeanToQuery to build a query string: key=value&key1=value

You can also use UriBuilder.queryPatam to add parameters one by one.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
  • Hi, thanks for your reply. Returning the output in the URI means that we'd be forced to expose all of the output parameters in the URI string (i.e., showing up in the user's browser location bar) rather than hiding that information in the communication between the web service and the JSP. This doesn't seem very restful, and more importantly, exposes some private information we wish to keep purely between the service and the JSP. – Ichiro Furusato Jul 08 '12 at 23:14
  • You can also return it in headers and access headers from the jsp. So the private information won't appear in url. – Tarlog Jul 09 '12 at 07:19
0

In the end I found a solution by playing around with some ideas gained by looking at Wink's DefectAsset example. The clue was the HtmlDescriptor class, which is found in Wink's internal API.

I'm providing my bean as the argument in an Asset's constructor (i.e., a class annotated "@Asset"), then passing the Asset through to the JSP directly as an entity in the Response:

MyBean mybean = service.getBean(id);
return Response.seeOther(new URI(baseurl+"Output.jsp"))
        .entity(new MyAsset(bean))
        .type(MediaType.APPLICATION_XHTML_XML).build();

This requires a method in MyAsset.java:

@Produces({MediaType.APPLICATION_XHTML_XML})
public HtmlDescriptor getHtml()
{
    return new HtmlDescriptor(bean,"/Output.jsp","MyBeanPayload");
}

and in Output.jsp, I gain access to the bean via the attribute name provided as the third argument in the HtmlDescriptor's constructor:

<%
  MyBean bean = (MyBean)request.getAttribute("MyBeanPayload");
  String id = bean.getId();
%>

In the JSP I'm using the returned values in an HTML form so I escape them using Apache Commons Lang's StringEscapeUtils.escapeHtml() method. The returned URI doesn't include anything except the RESTful path appended with "?alt=application%2Fxhtml%2Bxml", which is used to specify the media type.

This seems to demonstrate how to pass a POJO into an output JSP via Apache Wink.

Ichiro Furusato
  • 620
  • 6
  • 12
  • This solution will work only if the jsp and your REST application is the same web application on the same server. – Tarlog Jul 09 '12 at 07:25
  • In this project the JSPs are part of the same web application, but I understand your point. I might try out using the HTTP header approach you mention above to see how that works. Thanks for your help! – Ichiro Furusato Jul 09 '12 at 21:48