/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2021 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.qrest;

import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.util.CharsetUtil;
import org.jpos.core.annotation.Config;
import org.jpos.transaction.Context;
import org.jpos.transaction.TransactionParticipant;
import java.io.Serializable;
import static org.jpos.qrest.Constants.*;

public class ExtractFormParams implements TransactionParticipant {
    @Config("ignore-content-type") boolean ignoreContentType;

    @Override
    public int prepare(long id, Serializable context) {
        Context ctx = (Context) context;
        FullHttpRequest request = ctx.get(REQUEST);
        String contentType = request.headers().get("Content-Type");
        if (contentType != null && contentType.contains("application/x-www-form-urlencoded")) {
            QueryStringDecoder decoder = new QueryStringDecoder("?" + request.content().toString(CharsetUtil.UTF_8));
            if (!decoder.parameters().isEmpty())
                ctx.put(FORMPARAMS, decoder.parameters());
        } else if (!ignoreContentType)
            return FAIL;

        return PREPARED | READONLY | NO_JOIN;
    }
}

@@ -831,63 +831,8 @@
 st;%0A
-import io.netty.handler.codec.http.QueryStringDecoder;%0A
 impo
@@ -861,16 +861,16 @@
 etUtil;%0A
+
 import o
@@ -1019,16 +1019,208 @@
 izable;%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 import s
@@ -1321,16 +1321,106 @@
 ipant %7B%0A
+    private static final Pattern FORM_PARAM_PATTERN = Pattern.compile(%22(%5B%5E&%5D*)=(%5B%5E&%5D*)%22);%0A
     @Con
@@ -1817,65 +1817,156 @@
     
-QueryStringDecoder decoder = new QueryStringDecoder(%22?%22 +
+try %7B%0A                Map%3CString,String%3E params = new LinkedHashMap%3C%3E();%0A                ctx.put (FORMPARAMS, params);%0A                String body =
  req
@@ -2007,17 +2007,16 @@
 l.UTF_8)
-)
 ;%0A      
@@ -2025,103 +2025,470 @@
     
-if (!decoder.parameters().isEmpty())%0A                ctx.put(FORMPARAMS, decoder.parameters());
+    Matcher m = FORM_PARAM_PATTERN.matcher(body);%0A                while (m.find()) %7B%0A                    if (m.groupCount() == 2) %7B%0A                        params.put(%0A                          URLDecoder.decode(m.group(1), CharsetUtil.UTF_8.name()),%0A                          URLDecoder.decode(m.group(2), CharsetUtil.UTF_8.name())%0A                        );%0A                    %7D%0A                %7D%0A            %7D catch (UnsupportedEncodingException ignored) %7B %7D
 %0A   

use regexp to store Map<String,String>