diff --git a/src/main/java/com/fr/plugin/form/widget/URLUtils.java b/src/main/java/com/fr/plugin/form/widget/URLUtils.java new file mode 100644 index 0000000..b4553a5 --- /dev/null +++ b/src/main/java/com/fr/plugin/form/widget/URLUtils.java @@ -0,0 +1,81 @@ +package com.fr.plugin.form.widget; + +import com.fr.third.org.apache.http.NameValuePair; +import com.fr.third.org.apache.http.message.BasicNameValuePair; +import com.fr.third.org.apache.http.message.ParserCursor; +import com.fr.third.org.apache.http.message.TokenParser; +import com.fr.third.org.apache.http.util.Args; +import com.fr.third.org.apache.http.util.CharArrayBuffer; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Collections; +import java.util.List; + +/** + * 参照{@link com.fr.third.org.apache.http.client.utils.URLEncodedUtils} + * 的代码,但是不进行解码操作,传入的 url 可以是未编码的 + * + * @see com.fr.third.org.apache.http.client.utils.URLEncodedUtils + */ +@SuppressWarnings("ALL") +public class URLUtils { + + public static List parse(String s) { + if (s == null) { + return Collections.emptyList(); + } else { + CharArrayBuffer buffer = new CharArrayBuffer(s.length()); + buffer.append(s); + return parse(buffer, '&', ';'); + } + } + + public static List parse(String s, char... separators) { + if (s == null) { + return Collections.emptyList(); + } else { + CharArrayBuffer buffer = new CharArrayBuffer(s.length()); + buffer.append(s); + return parse(buffer, separators); + } + } + + public static List parse(CharArrayBuffer buf, char... separators) { + Args.notNull(buf, "Char array buffer"); + TokenParser tokenParser = TokenParser.INSTANCE; + BitSet delimSet = new BitSet(); + int var6 = separators.length; + + for (int var7 = 0; var7 < var6; ++var7) { + char separator = separators[var7]; + delimSet.set(separator); + } + + ParserCursor cursor = new ParserCursor(0, buf.length()); + ArrayList list = new ArrayList(); + + while (!cursor.atEnd()) { + delimSet.set(61); + String name = tokenParser.parseToken(buf, cursor, delimSet); + String value = null; + if (!cursor.atEnd()) { + int delim = buf.charAt(cursor.getPos()); + cursor.updatePos(cursor.getPos() + 1); + if (delim == '=') { + delimSet.clear(61); + value = tokenParser.parseValue(buf, cursor, delimSet); + if (!cursor.atEnd()) { + cursor.updatePos(cursor.getPos() + 1); + } + } + } + + if (!name.isEmpty()) { + list.add(new BasicNameValuePair(name, value)); + } + } + + return list; + } +} diff --git a/src/main/java/com/fr/plugin/form/widget/core/TemplateSource.java b/src/main/java/com/fr/plugin/form/widget/core/TemplateSource.java index 367e169..50d3cea 100644 --- a/src/main/java/com/fr/plugin/form/widget/core/TemplateSource.java +++ b/src/main/java/com/fr/plugin/form/widget/core/TemplateSource.java @@ -9,20 +9,20 @@ import com.fr.general.FArray; import com.fr.json.JSONArray; import com.fr.json.JSONException; import com.fr.json.JSONObject; +import com.fr.plugin.form.widget.URLUtils; import com.fr.script.Calculator; import com.fr.stable.CodeUtils; import com.fr.stable.ParameterProvider; import com.fr.stable.ProductConstants; +import com.fr.stable.StringUtils; import com.fr.stable.UtilEvalError; import com.fr.stable.js.WidgetName; import com.fr.stable.script.CalculatorProvider; import com.fr.stable.xml.XMLPrintWriter; import com.fr.stable.xml.XMLableReader; import com.fr.third.org.apache.http.NameValuePair; -import com.fr.third.org.apache.http.client.utils.URLEncodedUtils; import javax.servlet.http.HttpServletRequest; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -55,10 +55,10 @@ public class TemplateSource extends RHIframeSource { public String getCalculatedUrl(Calculator calculator, HttpServletRequest req) { try { String realPath = TemplateUtils.render(path, calculator); - List pairs = URLEncodedUtils.parse("__emb__=" + realPath, Charset.forName("utf-8")); + List pairs = URLUtils.parse("__emb__=" + realPath); List extraPairs = new ArrayList(); for (NameValuePair pair : pairs) { - if (pair.getName().equals("__emb__")) { + if ("__emb__".equals(pair.getName())) { realPath = pair.getValue(); } else { extraPairs.add(pair); @@ -73,7 +73,7 @@ public class TemplateSource extends RHIframeSource { } } else if (realPath.contains(".frm")) { if (isNewerThan9()) { - return NetworkHelper.createServletURL(req) + "/view/form?viewlet=" + CodeUtils.encodeURIComponent(realPath) + createQueryString(extraPairs); + return NetworkHelper.createServletURL(req) + "/view/form?viewlet=" + CodeUtils.encodeURIComponent(realPath) + createQueryString(extraPairs); } return NetworkHelper.createServletURL(req) + "?formlet=" + CodeUtils.encodeURIComponent(realPath) + createQueryString(extraPairs); } @@ -89,12 +89,20 @@ public class TemplateSource extends RHIframeSource { private String createQueryString(List pairs) { StringBuilder sb = new StringBuilder(); - for (int i = 0, len = pairs.size(); i < len; i ++) { + for (int i = 0, len = pairs.size(); i < len; i++) { NameValuePair pair = pairs.get(i); sb.append("&"); - sb.append(CodeUtils.encodeURIComponent(pair.getName())); + String key = pair.getName(); + if (key == null) { + key = StringUtils.EMPTY; + } + sb.append(CodeUtils.encodeURIComponent(key)); sb.append("="); - sb.append(CodeUtils.encodeURIComponent(pair.getValue())); + String value = pair.getValue(); + if (value == null) { + value = StringUtils.EMPTY; + } + sb.append(CodeUtils.encodeURIComponent(value)); } return sb.toString(); }