我想知道是否有任何簡單的方法來驗證使用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