/*
 * Copyright (c) 2016 Network New Technologies Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.networknt.body;

import io.undertow.server.handlers.form.FormData;

import java.util.*;

/**
 * To convert the form into a map, we first convert the formValue into a String and then put into the map.
 * For the same form key, there might be multiple form values, we handle it differently based on the size.
 */
public class BodyConverter {
    static Map<String, Object> convert(FormData data) {
        Map<String, Object> map = new HashMap<>();
        for (String key : data) {
            if (data.get(key).size() == 1) {
                String value = data.getFirst(key).getValue();
                map.put(key, value);
            } else if (data.get(key).size() > 1) {
                List<Object> list = new ArrayList<>();
                for (FormData.FormValue value : data.get(key)) {
                    list.add(value.getValue());
                }
                map.put(key, list);
            }
            // ignore size == 0
        }
        return map;
    }
}

@@ -1020,24 +1020,18 @@
 data) %7B%0A
-        
+%09%09
 Map%3CStri
@@ -1065,24 +1065,18 @@
 ap%3C%3E();%0A
-        
+%09%09
 for (Str
@@ -1093,28 +1093,20 @@
 data) %7B%0A
-            
+%0A%09%09%09
 if (data
@@ -1134,86 +1134,286 @@
 ) %7B%0A
-                String value = data.getFirst(key).getValue();%0A                
+%09%09%09%09// If the form data is file, read it as FileItem, else read as String.%0A%09%09%09%09if (data.getFirst(key).getFileName() == null) %7B%0A%09%09%09%09%09String value = data.getFirst(key).getValue();%0A%09%09%09%09%09map.put(key, value);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09FileItem value = data.getFirst(key).getFileItem();%0A%09%09%09%09%09
 map.
@@ -1429,28 +1429,25 @@
 value);%0A
-            
+%09%09%09%09%7D%0A%09%09%09
 %7D else i
@@ -1477,32 +1477,20 @@
  %3E 1) %7B%0A
-                
+%09%09%09%09
 List%3CObj
@@ -1520,32 +1520,20 @@
 st%3C%3E();%0A
-                
+%09%09%09%09
 for (For
@@ -1577,136 +1577,254 @@
 ) %7B%0A
-                    list.add(value.getValue());%0A                %7D%0A                map.put(key, list);%0A            %7D%0A            
+%09%09%09%09%09// If the form data is file, read it as FileItem, else read as String.%0A%09%09%09%09%09if (value.getFileName() == null) %7B%0A%09%09%09%09%09%09list.add(value.getValue());%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09list.add(value.getFileItem());%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09map.put(key, list);%0A%09%09%09%7D%0A%09%09%09
 // i
@@ -1843,26 +1843,14 @@
 = 0%0A
-        %7D%0A        
+%09%09%7D%0A%09%09
 retu
@@ -1857,16 +1857,13 @@
 rn map;%0A
-    
+%09
 %7D%0A%7D%0A

Update BodyConverter.java (#712)