OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Porting URIBuilder call sites


Hi All,

I just had to port this 'nice' fluent code:

            final String uri = new URIBuilder()
                .setScheme(scheme)
                .setHost("localhost")
                .setUserInfo(userInfo)
                .setPort(proxyPort)
                .setPath(path)
                .setCustomQuery(queryParameterString)
                .build()
                .toString();

to:

            final URIBuilder builder = new URIBuilder()
                .setScheme(scheme)
                .setHost("localhost")
                .setPort(proxyPort)
                .setPath(path)
                .setCustomQuery(queryParameterString);
            if (StringUtils.isNotBlank(userInfo)) {
                builder.setUserInfo(userInfo);
            }
            final String uri = builder.build().toString();

To avoid an IllegalArgumentException when userInfo is "":

java.lang.IllegalArgumentException: User info may not be empty
at org.apache.hc.core5.util.Args.containsNoBlanks(Args.java:84)
at org.apache.hc.core5.net.URIAuthority.<init>(URIAuthority.java:73)
at
org.apache.hc.core5.http.message.BasicHttpRequest.setUri(BasicHttpRequest.java:172)
at
org.apache.hc.core5.http.message.BasicHttpRequest.<init>(BasicHttpRequest.java:102)
at
org.apache.hc.core5.http.message.BasicClassicHttpRequest.<init>(BasicClassicHttpRequest.java:77)
at
org.apache.hc.client5.http.classic.methods.HttpUriRequestBase.<init>(HttpUriRequestBase.java:48)
at
org.apache.hc.client5.http.classic.methods.RequestBuilder.build(RequestBuilder.java:482)
at
com.rs.seagull.httpmonitor.ClientTests.testHttpMethod(ClientTests.java:106)
...

I would be nice to still offer this fluent style when userInfo is null (and
even "")

Thoughts?

Gary