Hi

Judging from you said your requirement is to do the filtering outside of the application class. As such I see no other way other than providing a custom message body writer, something similar to what I suggested below earlier, or just rely on a servlet filter. If you were prepared to return a Source from your resource class - it does not have to be the same class which returns application-specific types like Contacts then it would be feasible to provide a provider which say would apply an pre-configured XPath expression to returned Source such that empty contatcs are eliminated, or such that only those contacts which are meeting certain criterias are kept - this would be a more generic solution

Cheers, Sergey

Sergey Beryozkin wrote:
Hi

I would like to allow the user to somehow specify which elements they
care about for an object, so that the output can be optimized as well as
the queries backing the service.

It shoud be possible to do with  JAX-RS.

great

For instance, if a call is made to retrieve a Contact and the user
specifies "firstName,lastName", I only want the output to contain those
fields.  Secondarily, getting rid of empty elements would be nice.  This
could be via a URL for instance:

    /contact/2?fields=firstName,lastName

Sure, depends on how you'd like to do it. One way is to deal with it explicitly 
in the application code by checking the query
parameters and acting accordingly. Alternatively you can do a post-processing, 
If you're not after using a servlet filter then
another option to rely on a JAX-RS MessageBodyWriter.

So I can use the servlet filter to get the fields (I assume this is what you 
mean).
So, how do I then get CXF to constrain the output? I see that in the first example below you are returning a StreamSource...? You saying that I should change the signature of each of my methods to return a stream source and generate the XML myself?

Is there a way to just tell JAXB or another serializer to only contain or omit certain elements? And can I tell it to omit empty elements while I am at it?
Eg.

@Path("/contacts")
@ProduceMime("application/xml")
public class Contacts {

    @GET
    public  Contacts getContact(@PathParam int id) {
          retrun new StreamSource(...);
     }
}

class ContactsWriter implements MessageBodyWriter<Contacts> {
   private MultivaluedMap<String, String> currentQuery;

   @Context
   public void setUriInfo(UriInfo ui) {
          currentQuery = ui.getQueryparameters();
   }
   void writeTo(Contacts cs, Class<Contacts> clazz, ..., OutputStream os) {
          // write start tag
          for (Contact c : cs) {
             if (c.getName().equals(currentQuery.getFirst("firstName"))) {
               // write
             }
         }
          // write end tag
   }
}

There could be other options available, depending on how you'd like to do it.

Did I understand your question correctly ?

Also it's possible to have the same method's response be serialized as json or 
xml, etc, depending on the value of Accept, it's a
standard JAX-RS feature
Cool, so then I just need to set Accept as json only and then I will get back 
JSON data instead of XML?
Cheers, Sergey


Does CXF provide any mechanism for doing the above?  If there is no
mechanism for performing the dynamic selection of elements, a
configuration for eliminating empty elements in output would at least
allow me to easily create a servlet filter/interceptor that got the job
done.

Thanks in advance.

Cheers,
David

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Reply via email to