Skip to content

The Unified and Autonomous Development Platform

Update WebUserNameReservation to support CompoundRequestMapper

steve02f404767378
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/WebUserNameReservation.java b/gitop.web/src/main/java/com/pmease/gitop/web/WebUserNameReservation.java
index 2d452103d3..8518f1f9b6 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/WebUserNameReservation.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/WebUserNameReservation.java
@@ -1,6 +1,7 @@
package com.pmease.gitop.web;
import java.lang.reflect.Field;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@@ -10,9 +11,11 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.core.request.mapper.MountedMapper;
import org.apache.wicket.core.request.mapper.ResourceMapper;
import org.apache.wicket.request.IRequestMapper;
+import org.apache.wicket.request.mapper.CompoundRequestMapper;
import org.eclipse.jetty.servlet.ServletMapping;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
import com.pmease.commons.jetty.JettyPlugin;
import com.pmease.commons.util.ReflectionUtils;
import com.pmease.gitop.model.validation.UserNameReservation;
@@ -44,21 +47,40 @@ public class WebUserNameReservation implements UserNameReservation {
reserved.add("wicket");
for (IRequestMapper mapper: webApp.getRequestMappers()) {
- if (mapper instanceof MountedMapper || mapper instanceof ResourceMapper) {
- try {
- Field field = ReflectionUtils.findField(mapper.getClass(), "mountSegments");
- Preconditions.checkNotNull(field);
- field.setAccessible(true);
- String[] mountSegments = (String[]) field.get(mapper);
- if (mountSegments != null && mountSegments.length != 0 && mountSegments[0] != null)
- reserved.add(mountSegments[0]);
- } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
+ Set<String> set = getReservedNames(mapper);
+ reserved.addAll(set);
}
return reserved;
}
+ private Set<String> getReservedNames(IRequestMapper mapper) {
+ if (mapper instanceof MountedMapper || mapper instanceof ResourceMapper) {
+ try {
+ Field field = ReflectionUtils.findField(mapper.getClass(), "mountSegments");
+ Preconditions.checkNotNull(field);
+ field.setAccessible(true);
+ String[] mountSegments = (String[]) field.get(mapper);
+ if (mountSegments != null && mountSegments.length != 0 && mountSegments[0] != null)
+ return Collections.singleton(mountSegments[0]);
+ else
+ return Collections.emptySet();
+ } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ if (mapper instanceof CompoundRequestMapper) {
+ CompoundRequestMapper m = (CompoundRequestMapper) mapper;
+ Set<String> result = Sets.newHashSet();
+ for (IRequestMapper each : m) {
+ Set<String> set = getReservedNames(each);
+ result.addAll(set);
+ }
+
+ return result;
+ }
+
+ return Collections.emptySet();
+ }
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/validator/UsernameValidator.java b/gitop.web/src/main/java/com/pmease/gitop/web/validator/UsernameValidator.java
index eabb98e547..96036e2468 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/validator/UsernameValidator.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/validator/UsernameValidator.java
@@ -2,6 +2,7 @@ package com.pmease.gitop.web.validator;
import java.util.HashSet;
import java.util.Set;
+import java.util.regex.Pattern;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
@@ -20,6 +21,8 @@ public class UsernameValidator implements IValidator<String> {
private final String oldName;
private static Set<String> reservedNames;
+ static final Pattern pName = Pattern.compile("^[a-zA-Z0-9\\.-_@]{2,}");
+
public UsernameValidator(String oldName) {
this.oldName = oldName;
}
@@ -32,6 +35,12 @@ public class UsernameValidator implements IValidator<String> {
return; // not updated
}
+ if (!pName.matcher(newName).matches()) {
+ validatable.error(new ValidationError("Username can only contains "
+ + "alphabetical characters, numbers, '.', '@', '_' and '-'"));
+ return;
+ }
+
Set<String> reserved = getReservedNames();
if (reserved.contains(newName)) {
validatable.error(new ValidationError("'" + newName + "' is reserved by system"));