When creating a resource (POST /items) i was a bit annoyed by the fact
that my implementation has a dependency on jax-rs classes: it uses
response builder to build a 201 response. This also pollutes my
service interface, since the return value has to be a Response.

To fix this, i introduced an annotation which gets handled by the
interceptor code below:
I'm using this interceptor-construction to hack the response and turn
it into a 201 with a Location header (leaving the entity like it is).

Now my service implementation class or interface has no dependencies
on jax-rs (it just returns a String, which will be used to replace the
{return} placeholder.
I'ld like to get some feedback on this trick. Is there anything
"built-in" that i could have used?

public class RespondWithStatusAndHeaderInterceptor implements
PostProcessInterceptor {

        @Context HttpServletRequest req;
        public void postProcess(ServerResponse arg0) {
                for (Annotation a : arg0.getAnnotations()) {
                        if (a.annotationType().equals(RespondWithStatus.class)){
                                RespondWithStatus x = (RespondWithStatus) a;
                                String template = x.value();
                                String rv =  arg0.getEntity().toString();
                                int u = template.indexOf("{return}");
                                int y = template.indexOf('?');
                                rv = (u < y || y == -1) ?
org.jboss.resteasy.util.Encode.encodePath(rv) :
                                String p = 
                                String rep = template.replace("{return}",rv);
                                if (!"".equals(x.name())) {
                                        arg0.getMetadata().add(x.name(), p+rep);
        public static @interface RespondWithStatus {
                int status();
                String name() default "";
                String value() default "";

Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
Resteasy-users mailing list

Reply via email to