You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Geary, Frank" <fr...@zoominfo.com> on 2012/01/04 22:50:06 UTC

Extraneous query parameters

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?

Thanks

Frank Geary

______________________________________________



Frank Geary, Senior Software Engineer
ZoomInfo | 307 Waverley Oaks Road | Waltham, MA | 02452
Direct:  781.478.6008  |  Toll free:  866.904.ZOOM ( 9666 )


[cid:image001.gif@01CCCB00.E9B84D30]<http://www.zoominfo.com/>


Follow Us!


[cid:image002.jpg@01CCCB00.E9B84D30]<http://www.twitter.com/zoominfo?utm_source=signature+block&utm_medium=email&utm_campaign=signature2twitter>[cid:image003.png@01CCCB00.E9B84D30]<http://www.linkedin.com/groupsDirectory?itemaction=mclk&anetid=2164316&impid=&pgkey=anet_search_results&actpref=anetsrch_name&trk=anetsrch_name&goback=.gdr_1315592851794_1&utm_source=signature+block&utm_medium=email&utm_campaign=signature2linkedin>[cid:image004.jpg@01CCCB00.E9B84D30]<http://www.facebook.com/ZoomInformation?utm_source=signature+block&utm_medium=email&utm_campaign=signature2facebook>


RE: Extraneous query parameters

Posted by "Geary, Frank" <fr...@zoominfo.com>.
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:sberyozkin@gmail.com] 
Sent: Thursday, January 05, 2012 4:09 AM
To: users@cxf.apache.org
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
>

Re: Extraneous query parameters

Posted by Sergey Beryozkin <sb...@gmail.com>.
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
>