Using ParamConverterProvider to validate @PathParam value.

211 views
Skip to first unread message

Metin KILIÇ

unread,
Jan 29, 2015, 6:54:00 PM1/29/15
to cxf...@googlegroups.com
Hi, 

I am using cxf version 2.7.12 and I have an end-point in my application takes two PathParam parameters. one of the parameter takes Long and the other one takes java.util.UUID. I am running into an issue where validating the UUID. I created a ParameterHandler by implementing ParamConverterProvider

public class AppParameterHandler implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
        if (rawType.getName().equals(Long.class.getName())) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(String value) {
                    try {
                        //
                    } catch (NumberFormatException e) {
                        //
                    }
                }
                @Override
                public String toString(T value) {
                    if (value == null) {
                        return null;
                    }
                    return value.toString();
                }
            };
        }
        else if (rawType.getName().equals(UUID.class.getName())) {
            return new ParamConverter<T>() {

                @Override
                public T fromString(String value) {
                    try {
                       //
                    } catch (Exception e) {
                        //
                    }
                }

                @Override
                public String toString(T value) {
                    if (value == null) {
                        return null;
                    }
                    return value.toString();
                }
            };
        }
        return null;
    }
}

This code is working fine for Long type (if the param parameter is passed like 123123a123123 , I can handle it and send back the custom message in the response), but It throws an exception for UUID validation. 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <title>Error 500 javax.ws.rs.core.Response.hasEntity()Z</title>
    </head>
    <body>
        <h2>HTTP ERROR 500</h2>
        <p>Problem accessing /v1/foo/LONG/bar/INVALID_UUID. Reason:

            <pre>    javax.ws.rs.core.Response.hasEntity()Z</pre>
        </p>
        <h3>Caused by:</h3>
        <pre>java.lang.NoSuchMethodError: javax.ws.rs.core.Response.hasEntity()Z
at org.apache.cxf.jaxrs.utils.ExceptionUtils.convertFaultToResponse(ExceptionUtils.java:67)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.convertFaultToResponse(JAXRSUtils.java:1524)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.convertExceptionToResponseIfPossible(JAXRSInInterceptor.java:261)

So I debugged the cxf code and found out that it is not even creating the parameter handler that I registered. It hits https://github.com/apache/cxf/blob/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java#L394 line and threw NoSuchMethodException and then It calls https://github.com/apache/cxf/blob/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java#L490 to see if it can evaluate valueOf, fromString for UUID since it is not valid UUID evaluateFactoryMethod throws WebApplicationException : https://github.com/apache/cxf/blob/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java#L507

To my opinion, it should not throw that exception and let my AppParameterHandler handle the case in here https://github.com/apache/cxf/blob/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java#L423.

I can provide working/not working example as well if it is needed. 

So I would like these:
1- ParamConverterProvider should be used to validate ? if not, what should I use?
2- Why cxf does not call ParameterHandler even I registered it for the class type before throwing exception?

Thanks,
Metin



Reply all
Reply to author
Forward
0 new messages