You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Nacho G. Mac Dowell (JIRA)" <ji...@apache.org> on 2009/05/28 15:13:45 UTC

[jira] Commented: (CXF-2242) FormEncodingProvider writes the application/x-www-form-urlencoded params incorrectly when multiple params with the same name are present

    [ https://issues.apache.org/jira/browse/CXF-2242?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12713973#action_12713973 ] 

Nacho G. Mac Dowell commented on CXF-2242:
------------------------------------------

Workaround: (Since it will probably take some time to include this patch in a release, I am including the implemented workaround)

When invoking a application/x-www-form-urlencoded service register the following provider and everything will be fine:

public class FixedFormEncodingProvider extends FormEncodingProvider {

    public void writeTo(MultivaluedMap<String, String> map, Class<?> c, Type t, Annotation[] anns, 
                        MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) 
        throws IOException, WebApplicationException {
        boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
        for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, List<String>> entry = it.next();
            for (Iterator<String> entryIterator = entry.getValue().iterator(); entryIterator.hasNext();) {
				String value = entryIterator.next();
				os.write(entry.getKey().getBytes("UTF-8"));
                os.write('=');
                String data = encoded ? value : HttpUtils.urlEncode(value); 
                os.write(data.getBytes("UTF-8"));
                if (entryIterator.hasNext()) {
                    os.write('&');
                }
			}
        }
    }
}

> FormEncodingProvider writes the application/x-www-form-urlencoded params incorrectly when multiple params with the same name are present
> ----------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CXF-2242
>                 URL: https://issues.apache.org/jira/browse/CXF-2242
>             Project: CXF
>          Issue Type: Bug
>          Components: REST
>    Affects Versions: 2.2.1
>            Reporter: Nacho G. Mac Dowell
>         Attachments: FormEncodingProvider.patch
>
>
> Hi, there is a problem with FormEncodingProvider when writing the url-encoded string. It is checking an incorrect iterator.
>     public void writeTo(MultivaluedMap<String, String> map, Class<?> c, Type t, Annotation[] anns, 
>                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) 
>         throws IOException, WebApplicationException {
>         boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
>         for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator(); it.hasNext();) {
>             Map.Entry<String, List<String>> entry = it.next();
>             for (String value : entry.getValue()) {
>                 os.write(entry.getKey().getBytes("UTF-8"));
>                 os.write('=');
>                 String data = encoded ? value : HttpUtils.urlEncode(value); 
>                 os.write(data.getBytes("UTF-8"));
>                 if (it.hasNext()) {
>                     os.write('&');
>                 }
>             }
>         }
>     }
> it.hasNext() should be checking the entry iterator and not the map iterator. The consequence is that & is never appended if, for example, there is only one parameter
> Fixed version would be:
>     public void writeTo(MultivaluedMap<String, String> map, Class<?> c, Type t, Annotation[] anns, 
>                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) 
>         throws IOException, WebApplicationException {
>         boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
>         for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator(); it.hasNext();) {
>             Map.Entry<String, List<String>> entry = it.next();
>             for (Iterator<String> entryIterator = entry.getValue().iterator(); entryIterator.hasNext();) {
>             	String value = entryIterator.next();
> 				os.write(entry.getKey().getBytes("UTF-8"));
>                 os.write('=');
>                 String data = encoded ? value : HttpUtils.urlEncode(value); 
>                 os.write(data.getBytes("UTF-8"));
>                 if (entryIterator.hasNext()) {
>                     os.write('&');
>                 }
>             }
>         }
>     }
> I don't have time just now to provide test cases but a careful look should do.
> best regards

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.