Skip to content

Commit

Permalink
Apply code improvements suggested by IDE (#10906)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov committed Jun 14, 2024
1 parent 0118db9 commit f147772
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 196 deletions.
22 changes: 10 additions & 12 deletions http/src/main/java/io/micronaut/http/uri/DefaultUriBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
*/
package io.micronaut.http.uri;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.value.MutableConvertibleMultiValues;
import io.micronaut.core.convert.value.MutableConvertibleMultiValuesMap;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.exceptions.UriSyntaxException;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -57,7 +59,7 @@ class DefaultUriBuilder implements UriBuilder {
private static final Pattern PATTERN_FULL_URI = Pattern.compile(
"^(" + STRING_PATTERN_SCHEME + ")?" + "(//(" + STRING_PATTERN_USER_INFO + "@)?" + STRING_PATTERN_HOST + "(:" + STRING_PATTERN_PORT +
")?" + ")?" + STRING_PATTERN_PATH + "(\\?" + STRING_PATTERN_QUERY + ")?" + "(#" + STRING_PATTERN_REMAINING + ")?");

private String authority;
private final MutableConvertibleMultiValues<String> queryParams;
private String scheme;
Expand Down Expand Up @@ -120,7 +122,7 @@ class DefaultUriBuilder implements UriBuilder {
this.host = host;
}
if (port != null) {
this.port = Integer.valueOf(port);
this.port = Integer.parseInt(port);
}
if (path != null) {

Expand Down Expand Up @@ -345,7 +347,7 @@ private String reconstructAsString(Map<String, ? super Object> values) {

StringBuilder path = this.path;
if (StringUtils.isNotEmpty(path)) {
if (builder.length() > 0 && path.charAt(0) != '/') {
if (!builder.isEmpty() && path.charAt(0) != '/') {
builder.append('/');
}
String pathStr = path.toString();
Expand Down Expand Up @@ -413,10 +415,6 @@ private String expandOrEncode(String value, Map<String, ? super Object> values)
}

private String encode(String userInfo) {
try {
return URLEncoder.encode(userInfo, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("No available charset: " + e.getMessage());
}
return URLEncoder.encode(userInfo, StandardCharsets.UTF_8);
}
}
17 changes: 10 additions & 7 deletions http/src/main/java/io/micronaut/http/uri/UriMatchTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public UriMatchTemplate(CharSequence templateString) {
*/
protected UriMatchTemplate(CharSequence templateString, Object... parserArguments) {
super(templateString, parserArguments);
if (variables.isEmpty() && Pattern.quote(templateString.toString()).equals(pattern.toString())) {
if (variables.isEmpty() && Pattern.quote(templateString.toString()).contentEquals(pattern)) {
// if there are no variables and a match pattern matches template we can assume it matches exactly
this.matchPattern = null;
this.exactMatch = true;
Expand Down Expand Up @@ -389,13 +389,12 @@ protected void addVariableSegment(List<PathSegment> segments,
char operator,
String previousDelimiter, boolean isQuerySegment) {
matchTemplate.variables.add(new UriMatchVariable(variable, modifierChar, operator));
StringBuilder pattern = matchTemplate.pattern;
int modLen = modifierStr.length();
boolean hasModifier = modifierChar == ':' && modLen > 0;
String operatorPrefix = "";
String operatorQuantifier = "";
String variableQuantifier = "+?)";
String variablePattern = getVariablePattern(variable, operator);
String variablePattern = null;
if (hasModifier) {
char firstChar = modifierStr.charAt(0);
if (firstChar == '?') {
Expand All @@ -409,25 +408,26 @@ protected void addVariableSegment(List<PathSegment> segments,
(modLen > 1 && lastChar == '?' && (modifierStr.charAt(modLen - 2) == '*' || modifierStr.charAt(modLen - 2) == '+'))) {
operatorQuantifier = "?";
}
String s = (firstChar == '^') ? modifierStr.substring(1) : modifierStr;
if (operator == '/' || operator == '.') {
variablePattern = "(" + ((firstChar == '^') ? modifierStr.substring(1) : modifierStr) + ")";
variablePattern = "(" + s + ")";
} else {
operatorPrefix = "(";
variablePattern = ((firstChar == '^') ? modifierStr.substring(1) : modifierStr) + ")";
variablePattern = s + ")";
}
variableQuantifier = "";
}
}

boolean operatorAppended = false;

StringBuilder pattern = matchTemplate.pattern;
switch (operator) {
case '.':
case '/':
pattern.append("(")
.append(operatorPrefix)
.append("\\")
.append(String.valueOf(operator))
.append(operator)
.append(operatorQuantifier);
operatorAppended = true;
// fall through
Expand All @@ -436,6 +436,9 @@ protected void addVariableSegment(List<PathSegment> segments,
if (!operatorAppended) {
pattern.append("(").append(operatorPrefix);
}
if (variablePattern == null) {
variablePattern = getVariablePattern(variable, operator);
}
pattern.append(variablePattern)
.append(variableQuantifier)
.append(")");
Expand Down
Loading

0 comments on commit f147772

Please sign in to comment.