Testing JBoss RESTeasy JAX-RS JAXB with Decorator

I try to check all the incoming XML files that fall into my (contract first) REST interface in an application running inside JBoss AS 7. I wrote @Decorator for Pretty-Printing (as in the example in JBoss RESTeasy documentation) and similar to enable schema checking XML for unmarshaller. Unfortunately, the decorator for unmarshaller is never called.

Here is the code for Pretty Decorator:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.xml.bind.Marshaller;

import org.jboss.resteasy.annotations.Decorator;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Decorator(processor = PrettyProcessor.class, target = Marshaller.class)
public @interface Pretty {}

And implementation:

import java.lang.annotation.Annotation;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import org.apache.log4j.Logger;
import org.jboss.resteasy.annotations.DecorateTypes;
import org.jboss.resteasy.spi.interception.DecoratorProcessor;

@DecorateTypes({ "text/*+xml", "application/*+xml", MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class PrettyProcessor implements DecoratorProcessor<Marshaller, Pretty> {
    private static final Logger LOGGER = Logger.getLogger(PrettyProcessor.class);

    @Override
    public Marshaller decorate(Marshaller target, Pretty annotation, Class type, Annotation[] annotations, MediaType mediaType) {
        LOGGER.debug("Pretty.");
        try {
            target.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        } catch (PropertyException e) {
        }
        return target;
    }
}

Now annotation to check (which does not work):

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.xml.bind.Unmarshaller;

import org.jboss.resteasy.annotations.Decorator;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Decorator(processor = ValidateProcessor.class, target = Unmarshaller.class)
public @interface Validate {}

Implementation:

import java.lang.annotation.Annotation;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import javax.xml.XMLConstants;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.log4j.Logger;
import org.jboss.resteasy.annotations.DecorateTypes;
import org.jboss.resteasy.spi.interception.DecoratorProcessor;
import org.xml.sax.SAXException;

@DecorateTypes({ "text/*+xml", "application/*+xml", MediaType.APPLICATION_XML, MediaType.TEXT_XML })
public class ValidateProcessor implements DecoratorProcessor<Unmarshaller, Validate> {
    private static final Logger LOGGER = Logger.getLogger(ValidateProcessor.class);

    @Override
    public Unmarshaller decorate(Unmarshaller target, Validate annotation,  Class type, Annotation[] annotations, MediaType mediaType) {
        target.setSchema(getSchema());
        LOGGER.debug("Set validation schema.");
        System.out.println("Set validation schema.");
        return target;
    }
}

And the REST interface code:

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.xml.ws.WebServiceException;

@Path("/{mdvt}/{ouid}/order")
public interface OrderResource {

    @RolesAllowed({ "mpa" })
    @POST
    @Path("/update")
    @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
    @Produces(MediaType.APPLICATION_XML)
    @Pretty
    public Response update(@Context SecurityContext sec,
            @PathParam("ouid") String ouID, 
            @PathParam("mdvt") long masterDataVersionTag,
            @Validate UpdateOrdersRequest uor) throws WebServiceException;
}

In the same REST (update) method, @Pretty Decorator is called, but @Validate is not. What am I doing wrong here?

I found an old error. Decorator for jaxb unmarshaller does not work , which is closed.

, , , . , Unmarshaller.

+5
2

, JBoss AS 7.

Resteasy, 2.3.5.Final.

https://issues.jboss.org/browse/RESTEASY-711, , 2.3.5.

- Restwasy 2.3.5.Final , resteasy-jboss-modules-2.3.5.Final.zip

JBoss AS 7.1.1, resteasy . .

, ,

+2

MessageBodyReader, Schema JAXB Unmarshaller . :

0

All Articles