Skip to content

Commit

Permalink
use regexp to store Map<String,String>
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Apr 11, 2022
1 parent 0ae3712 commit 9453fd1
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions modules/qrest/src/main/java/org/jpos/qrest/ExtractFormParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@
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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.jpos.qrest.Constants.*;

public class ExtractFormParams implements TransactionParticipant {
private static final Pattern FORM_PARAM_PATTERN = Pattern.compile("([^&]*)=([^&]*)");
@Config("ignore-content-type") boolean ignoreContentType;

@Override
Expand All @@ -36,9 +43,20 @@ public int prepare(long id, Serializable 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());
try {
Map<String,String> params = new LinkedHashMap<>();
ctx.put (FORMPARAMS, params);
String body = request.content().toString(CharsetUtil.UTF_8);
Matcher m = FORM_PARAM_PATTERN.matcher(body);
while (m.find()) {
if (m.groupCount() == 2) {
params.put(
URLDecoder.decode(m.group(1), CharsetUtil.UTF_8.name()),
URLDecoder.decode(m.group(2), CharsetUtil.UTF_8.name())
);
}
}
} catch (UnsupportedEncodingException ignored) { }
} else if (!ignoreContentType)
return FAIL;

Expand Down

0 comments on commit 9453fd1

Please # to comment.