diff --git a/src/main/java/org/takes/misc/Href.java b/src/main/java/org/takes/misc/Href.java index 4ac1434ad..e84fbc636 100644 --- a/src/main/java/org/takes/misc/Href.java +++ b/src/main/java/org/takes/misc/Href.java @@ -37,6 +37,7 @@ import java.util.TreeMap; import java.util.regex.Pattern; import org.cactoos.text.FormattedText; +import org.takes.rq.UriHandler; /** * HTTP URI/HREF. @@ -358,12 +359,12 @@ private static SortedMap> asMap(final String query) { */ private static URI createBare(final URI link) { final URI uri; - if (link.getRawQuery() == null && link.getRawFragment() == null) { + if (!UriHandler.getRawQuery(link).has() && link.getRawFragment() == null) { uri = link; } else { final String href = link.toString(); final int idx; - if (link.getRawQuery() == null) { + if (!UriHandler.getRawQuery(link).has()) { idx = href.indexOf('#'); } else { idx = href.indexOf('?'); diff --git a/src/main/java/org/takes/rq/UriHandler.java b/src/main/java/org/takes/rq/UriHandler.java new file mode 100644 index 000000000..09d4f6ca4 --- /dev/null +++ b/src/main/java/org/takes/rq/UriHandler.java @@ -0,0 +1,77 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2024 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.rq; + +import org.takes.misc.Opt; +import java.net.URI; + +/** + * Handler to hide nullability of URI class. + */ +public class UriHandler { + + /** + * returns Opt of URI host. + */ + public static Opt getHostFromUri(URI uri) { + String host = uri.getHost(); + if (host == null || host.isEmpty()) { + return new Opt.Empty<>(); + } + return new Opt.Single<>(host); + } + + /** + * returns Opt of URI port. + */ + public static Opt getPortFromUri(URI uri) { + int port = uri.getPort(); + if (port == -1) { + return new Opt.Empty<>(); + } + return new Opt.Single<>(port); + } + + /** + * returns Opt of URI raw query. + */ + public static Opt getRawQuery(URI uri) { + String rawQuery = uri.getRawQuery(); + if (rawQuery == null) { + return new Opt.Empty<>(); + } + return new Opt.Single<>(rawQuery); + } + + /** + * returns Opt of URI raw query. + */ + public static Opt getQuery(URI uri) { + String query = uri.getQuery(); + if (query == null) { + return new Opt.Empty<>(); + } + return new Opt.Single<>(query); + } +} diff --git a/src/main/java/org/takes/servlet/HttpServletRequestFake.java b/src/main/java/org/takes/servlet/HttpServletRequestFake.java index 1988aab45..93a71b8fd 100644 --- a/src/main/java/org/takes/servlet/HttpServletRequestFake.java +++ b/src/main/java/org/takes/servlet/HttpServletRequestFake.java @@ -50,9 +50,11 @@ import java.util.Map; import java.util.NoSuchElementException; import org.takes.Request; +import org.takes.misc.Opt; import org.takes.rq.RqHeaders; import org.takes.rq.RqHref; import org.takes.rq.RqMethod; +import org.takes.rq.UriHandler; /** * Fake HttpServletRequest (for unit tests). @@ -218,11 +220,11 @@ public String getServerName() { ex ); } - String host = uri.getHost(); - if (host == null || host.isEmpty()) { - host = "localhost"; + Opt host = UriHandler.getHostFromUri(uri); + if (!host.has()) { + return "localhost"; } - return host; + return host.get(); } @Override @@ -237,11 +239,11 @@ public int getServerPort() { ex ); } - int port = uri.getPort(); - if (port == -1) { - port = 80; + Opt port = UriHandler.getPortFromUri(uri); + if (!port.has()) { + return 80; } - return port; + return port.get(); } @Override diff --git a/src/main/java/org/takes/tk/TkSmartRedirect.java b/src/main/java/org/takes/tk/TkSmartRedirect.java index 3d51db71e..4df5a0b85 100644 --- a/src/main/java/org/takes/tk/TkSmartRedirect.java +++ b/src/main/java/org/takes/tk/TkSmartRedirect.java @@ -29,7 +29,9 @@ import lombok.EqualsAndHashCode; import lombok.ToString; import org.takes.Request; +import org.takes.misc.Opt; import org.takes.rq.RqRequestLine; +import org.takes.rq.UriHandler; import org.takes.rs.RsRedirect; /** @@ -108,13 +110,14 @@ public String location() throws IOException { final StringBuilder loc = new StringBuilder(this.origin); final URI target = URI.create(this.origin); final URI uri = URI.create(new RqRequestLine.Base(this.req).uri()); - if (uri.getQuery() != null) { - if (target.getQuery() == null) { + final Opt uriQuery = UriHandler.getQuery(uri); + if (uriQuery.has()) { + if (!UriHandler.getQuery(target).has()) { loc.append('?'); } else { loc.append('&'); } - loc.append(uri.getQuery()); + loc.append(uriQuery.get()); } if (uri.getFragment() != null) { loc.append('#'); @@ -123,5 +126,4 @@ public String location() throws IOException { return loc.toString(); } } - }