Skip to content

Commit

Permalink
adding UriHandler to avoid checking nulls in code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartofanych committed Dec 28, 2024
1 parent 2f7bc6c commit 83346e8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/takes/misc/Href.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -358,12 +359,12 @@ private static SortedMap<String, List<String>> 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('?');
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/org/takes/rq/UriHandler.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<Integer> 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<String> 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<String> getQuery(URI uri) {
String query = uri.getQuery();
if (query == null) {
return new Opt.Empty<>();
}
return new Opt.Single<>(query);
}
}
18 changes: 10 additions & 8 deletions src/main/java/org/takes/servlet/HttpServletRequestFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -218,11 +220,11 @@ public String getServerName() {
ex
);
}
String host = uri.getHost();
if (host == null || host.isEmpty()) {
host = "localhost";
Opt<String> host = UriHandler.getHostFromUri(uri);
if (!host.has()) {
return "localhost";
}
return host;
return host.get();
}

@Override
Expand All @@ -237,11 +239,11 @@ public int getServerPort() {
ex
);
}
int port = uri.getPort();
if (port == -1) {
port = 80;
Opt<Integer> port = UriHandler.getPortFromUri(uri);
if (!port.has()) {
return 80;
}
return port;
return port.get();
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/takes/tk/TkSmartRedirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<String> 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('#');
Expand All @@ -123,5 +126,4 @@ public String location() throws IOException {
return loc.toString();
}
}

}

0 comments on commit 83346e8

Please sign in to comment.