How to deal with input parameter in CXF request handler in general?

Dylan picture Dylan · May 26, 2012 · Viewed 8.5k times · Source

I have been doing some work with apache CXF(version 2.2.2) JAX-RS. I am trying to introduce data validation layer in CXF request handler before business method be invoked. Fortunately :), I am encountering an issue on input parameter handling in request handler(DataValidationHandler). I can read the JSON Object manually by following code lines in request handler. But it's duplicated with JSONProvider registered in CXF framework. Because JSON object input stream only can be read once, otherwise we will meet exception "java.io.EOFException: No content to map to Object due to end of input". Moreover, duplicated JSON object deserializing will impacts performance. Following code is sample for your reference.

Read JSON Object from HTTP body manually:

OperationResourceInfo ori = paramMessage.getExchange().get(OperationResourceInfo.class);
MultivaluedMap<String, String> values = new MetadataMap<String, String>();
List<Object> objList = JAXRSUtils.processParameters(ori, values, paramMessage);

Register JSONProvider in CXF JAX-RS framework:

<bean id="JSONProvider" class="com.accela.govxml2.jaxrs.util.JSONProvider"></bean>

Read JSON Object to Java Object from input stream:

public Object readFrom(......){
    ObjectMapper objectMapper = new ObjectMapper();
    Object result = objectMapper.readValue(entityStream, TypeFactory.defaultInstance().constructType(genericType));
    Return result;
}

I am dealing with Path Parameter manually by following code lines.

OperationResourceInfo ori = paramMessage.getExchange().get(OperationResourceInfo.class);
URITemplate t1 = ori.getClassResourceInfo().getURITemplate();
URITemplate t2 = ori.getURITemplate();
UriInfo uriInfo = new UriInfoImpl(paramMessage, null);
MultivaluedMap<String, String> map = new MetadataMap<String, String>();
t1.match(uriInfo.getPath(), map);
String str = map.get(URITemplate.FINAL_MATCH_GROUP).get(0);
t2.match(str, map);
String pathParameter= null;
if (map.containsKey("pathParam") && !ValidationUtil.isEmpty(map.get("pathParam")))
{
    pathParameter= map.get("pathParam").get(0);
}

My questions are here:

  1. How to deal with POST/PUT input parameter of http body in request handler in general?
  2. How to avoid performance issue to read input parameter efficiently?
  3. Is there any way to inject the validation (handler/interceptor) layer between parameter reading by CXF(JSONProvider) and business method invoking?
  4. Is there any elegant way to deal with path parameter?

Thanks for your help. Any comments & suggestions will be appreciated.

Regards, Dylan

Answer

Dylan picture Dylan · May 29, 2012

I have found another way to inject DataValidation Interceptor into reading parameter phase. We can reuse deserialized input model from message content, which be deserialized by JSONProvider registered in framework. It can improve performance, because only deserialize input model once.

public class DataValidationInInterceptor extends AbstractPhaseInterceptor<Message>
{
public DataValidationInInterceptor()
{
    super(Phase.READ);
}

@Override
public void handleMessage(Message message)
{
    OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
    Method method = ori.getMethodToInvoke();
    Class<?>[] types = method.getParameterTypes();
    Type[] genericParameterTypes = method.getGenericParameterTypes();

    for (int i = 0; i < types.length; i++)
    {
        Class<?> type = types[i];
        List obj = (List) message.getContent(List.class);
        System.out.println(obj);
        System.out.println(type);
    }
}
}