Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactoring bootstrap #2202

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class IdValidateUtils {

private static final int DEFAULT_MAX_LENGTH = PinpointConstants.AGENT_NAME_MAX_LEN;

// private static final Pattern ID_PATTERN = Pattern.compile("[a-zA-Z0-9\\._\\-]{1,24}");
// private static final Pattern ID_PATTERN = Pattern.compile("[a-zA-Z0-9\\._\\-]{1,24}");
private static final Pattern ID_PATTERN = Pattern.compile("[a-zA-Z0-9\\._\\-]+");

private IdValidateUtils() {
Expand All @@ -47,21 +47,44 @@ public static boolean validateId(String id, int maxLength) {
throw new IllegalArgumentException("negative maxLength:" + maxLength);
}

final Matcher matcher = ID_PATTERN.matcher(id);
if (matcher.matches()) {
return checkBytesLength(id, maxLength);
} else {
if (!checkPattern(id)) {
return false;
}
if (!checkLength(id, maxLength)) {
return false;
}

return true;
}

private static boolean checkBytesLength(String id, int maxLength) {
public static boolean checkPattern(String id) {
final Matcher matcher = ID_PATTERN.matcher(id);
return matcher.matches();
}

public static boolean checkLength(String id, int maxLength) {
if (id == null) {
throw new NullPointerException("id must not be null");
}
// try encode
final int idLength = getLength(id);
if (idLength <= 0) {
return false;
}
return idLength <= maxLength;
}

public static int getLength(String id) {
if (id == null) {
return -1;
}

final byte[] idBytes = BytesUtils.toBytes(id);
if (idBytes == null || idBytes.length == 0) {
throw new IllegalArgumentException("toBytes fail. id:" + id);
if (idBytes == null) {
// encoding fail
return -1;
}
return idBytes.length <= maxLength;
return idBytes.length;
}

}
Loading