将Base64字符串作为选项传递给Apache Commons

编程入门 行业动态 更新时间:2024-10-15 04:25:34
将Base64字符串作为选项传递给Apache Commons-CLI分析器(Passing a Base64 string as an option to Apache Commons-CLI parser)

我构建了一个Java webstart应用程序,它需要接收Base64参数作为命令行参数。

我正在使用Apache Common-CLI来解析JNLP中的参数。 只有两个参数,其中一个是JSON,而在这个JSON中有一个Base64字符串。

问题是Base64字符串在末尾填充了“=”(等于char /等号),并且解析器无法将字符串识别为参数的一部分,而是尝试将“=”作为键=参数的值分隔符,它抛出以下异常: org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option

有没有办法来逃避这个字符,或者让解析器忽略它?

CLI选项使用以下代码构建:

Option appletBehaviourConfigJSONOption = Option.builder("J") .longOpt("appletBehaviourConfigJSON") .hasArg() .argName("JSON") .desc("JSON config") .build(); clioptions.addOption(appletBehaviourConfigJSONOption); CommandLineParser cliparser = new DefaultParser(); CommandLine cmd = null; try { cmd = cliparser.parse(clioptions, args); } catch (ParseException e) { e.printStackTrace(); }

它是从JNLP调用的:

<argument>-appletBehaviourConfigJSON { "appletBehaviourSignatureType": "HASH", "appletBehaviourHashList": [{"id": "1234", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}, {"id": "5678", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}], "appletBehaviourCookies": [{"name": "JSESSIONID", "value": "2edee5627c84937f707bdd390b1c"}, {"name": "STICKY", "value": "ASD123213123adsf"}], "appletBehaviourPostURL": "http://example.org", "stampSelection": "WHITE", "setStampAll": true}</argument>

当我在两个哈希上将'='更改为'a'时,解析器按预期工作(但该选项现在具有无效的Base64)。

I built a Java webstart application which needs to receive a Base64 parameter as a command line argument.

I'm using Apache Common-CLI to parse the arguments from inside the JNLP. There are just two arguments, one of which is a JSON, and inside this JSON there is a Base64 string.

The problem is that the Base64 string is padded at the end with "=" (equal char/equal sign), and the parser is having trouble recognizing the string as part of the argument, instead trying to evaluate the "=" as a key=value separator for the argument and it's trowing the following exception: org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option

Is there a way to escape this char, or make the parser ignore it?

The CLI option is built using the following code:

Option appletBehaviourConfigJSONOption = Option.builder("J") .longOpt("appletBehaviourConfigJSON") .hasArg() .argName("JSON") .desc("JSON config") .build(); clioptions.addOption(appletBehaviourConfigJSONOption); CommandLineParser cliparser = new DefaultParser(); CommandLine cmd = null; try { cmd = cliparser.parse(clioptions, args); } catch (ParseException e) { e.printStackTrace(); }

And it's being called from a JNLP:

<argument>-appletBehaviourConfigJSON { "appletBehaviourSignatureType": "HASH", "appletBehaviourHashList": [{"id": "1234", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}, {"id": "5678", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}], "appletBehaviourCookies": [{"name": "JSESSIONID", "value": "2edee5627c84937f707bdd390b1c"}, {"name": "STICKY", "value": "ASD123213123adsf"}], "appletBehaviourPostURL": "http://example.org", "stampSelection": "WHITE", "setStampAll": true}</argument>

When I change the '=' to 'a' on both hashes, the parser works as expected (but the option now has an invalid Base64 of course).

最满意答案

看起来commons-cli并不像处理等号那样处理选项名称和值之间的空白。

因此,尝试使用等号而不是空格

-appletBehaviourConfigJSON={...

It seems commons-cli does not handle blanks between name and value for options the same way as an equal sign.

Therefore try to use an equal-sign instead of blank as in

-appletBehaviourConfigJSON={...

更多推荐

本文发布于:2023-08-04 09:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   选项   Apache   Commons

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!