Works great thanks!

Here's a code snippet in case anyone else is interested:

        Map<String, List<String>> unexpectedQueryParams = new HashMap<String, 
List<String>>();
        MultivaluedMap<String,String> allQueryParams = ui.getQueryParameters(); 
// @Context UriInfo ui passed in
        
        Class<?> ExpectedQueryParamsInputClass = null;
        try {
            ExpectedQueryParamsInputClass = 
Class.forName("YOUR_FULLY_QUALIFIED_CLASS_WITH_QUERY_PARAM_MEMBERS_ANNOTATED");
        } catch (ClassNotFoundException e) {
            // This should NEVER happen!
        }
        
        boolean foundUnexpectedParam = false;
        for (String queryParamName: allQueryParams.keySet()) {
            try {
                // First see if we find this field as an inherited field.
                Field tempInheritedField = 
ExpectedQueryParamsInputClass.getField(queryParamName);
                if 
(!(tempInheritedField.isAnnotationPresent(ExpectedQueryParam.class))) {
                    throw new NoSuchFieldException();
                }
            } catch (NoSuchFieldException e) {
                try {
                    // Then see if we find this field as a private class member 
of the class itself.
                    Field tempPrivateField = 
ExpectedQueryParamsInputClass.getDeclaredField(queryParamName);
                    if 
(!(tempPrivateField.isAnnotationPresent(ExpectedQueryParam.class))) {
                        throw new NoSuchFieldException();
                    }
                } catch (NoSuchFieldException e2) {
                    foundUnexpectedParam = true;
                    // We've found an unexpected query parameter.
                    if (unexpectedQueryParams.containsKey(queryParamName)) {
                        List<String> unexpectedQueryParameterValueList = 
unexpectedQueryParams.get(queryParamName);
                        
unexpectedQueryParameterValueList.addAll(allQueryParams.get(queryParamName));
                    } else {
                        unexpectedQueryParams.put(queryParamName, 
allQueryParams.get(queryParamName));    
                    }            
                }
            }
        }
        
        if (foundUnexpectedParam) {
            return (Object) new DemoError("Received Unexpected Parameter");
        }

Frank Geary

-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]] 
Sent: Thursday, January 05, 2012 4:09 AM
To: [email protected]
Subject: Re: Extraneous query parameters

Hi Frank,

On 04/01/12 23:50, Geary, Frank wrote:
> Hi,
>
> I use a Java class to specify my expected query parameters using
> @QueryParam(""). But I'm wondering what is the Apache CXF
> easiest/recommended way to determine when extraneous (or even
> misspelled) parameters were passed in so I can generate an error. I
> cannot just ignore extra params. Any suggestions?

One option is to register a custom RequestHandler filter and get UriInfo 
injected, and in the filter's handle method do check 
uriInfo.getQueryParameters and throw an exception if an unrecognized 
query is found

If needed you can avoid using @QueryParam("") and work directly with 
UriInfo in the application code

Cheers, Sergey

>
> Thanks
>
> Frank Geary
>

Reply via email to