我想知道是否有任何简单的方法来验证使用Java的Diameter URI(下面的描述)?在Java中验证Diameter URI的方法?
Note, a Diameter URI must have one of the forms:
aaa://FQDN[:PORT][;transport=TRANS][;protocol=PROT]
aaas://FQDN[:PORT][;transport=TRANS][;protocol=PROT]
The FQDN (mandatory) has to be replaced with the fully qualified host name (or IP), the PORT (optional, default is 3868) with the port number, TRANS (optional) with the transport protocol (can be TCP or SCTP) and PROT (optional) with diameter.
Some examples of the acceptable forms are:
aaa://server.com
aaa://127.0.0.1
aaa://server.com:1234
aaas://server.com:1234;transport=tcp
aaas://[::1]
aaas://[::1]:1234
aaas://[::1]:1234;transport=tcp;protocol=diameter
Note, as shown above, if using an IPv6 address, the address must be placed in box brackets, whereas the port number (if specified), with its colon separator, should be outside of the brackets.
我觉得这样使用正则表达式将是相当混乱和难以理解,我已经看到了其他的例子不使用正则表达式是一样尴尬找(如https://code.google.com/p/cipango/source/browse/trunk/cipango-diameter/src/main/java/org/cipango/diameter/util/AAAUri.java?r=763)。
所以想知道是否有更好的方法来做到这一点,就像一个URI验证器库,它需要一些规则(比如上面Diameter URI的规则),然后将它们应用于某些输入来验证它?
我已经看过谷歌Guava的图书馆,看看是否有什么可以帮助,但我什么都看不到。
非常感谢!
您是否尝试过简单地使用'java.net.URI'构造函数,如果URI无效,会引发URISyntaxException? – Steinar 2013-03-24 18:34:18
是的,java.net.URI与我的用例略有不同。例如,接受新的URI(“127.0.0.1”),但这不是有效的Diameter URI(开始时没有方案部分) – user1977749 2013-03-24 19:20:22
为什么不用正则表达式解析FQDN,如:'#aaas?://( [^;] +)(; transport = \ w +)?(; protocol = \ w +)?#'然后在新的URI构造函数中使用子组1来验证它是否为FQDN? – FrankieTheKneeMan 2013-03-24 20:21:43