Skip to content

Commit b795f27

Browse files
author
Vladimir Kotal
committed
move isAlphanumeric() to org.opensolaris.opengrok.util
1 parent 87058e2 commit b795f27

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

plugins/LdapPlugin/src/opengrok/auth/plugin/LdapUserPlugin.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import opengrok.auth.plugin.entity.User;
3434
import org.opensolaris.opengrok.configuration.Group;
3535
import org.opensolaris.opengrok.configuration.Project;
36+
import org.opensolaris.opengrok.util.StringUtils;
3637

3738
/**
3839
* Authorization plug-in to extract user's LDAP attributes.
@@ -49,17 +50,6 @@ public class LdapUserPlugin extends AbstractLdapPlugin {
4950
private String objectClass;
5051
private final Pattern usernameCnPattern = Pattern.compile("(cn=[a-zA-Z0-9_-]+)");
5152

52-
private boolean isAlphanumeric(String str) {
53-
for (int i = 0; i < str.length(); i++) {
54-
char c = str.charAt(i);
55-
if (!Character.isDigit(c) && !Character.isLetter(c)) {
56-
return false;
57-
}
58-
}
59-
60-
return true;
61-
}
62-
6353
@Override
6454
public void load(Map<String, Object> parameters) {
6555
super.load(parameters);
@@ -69,7 +59,7 @@ public void load(Map<String, Object> parameters) {
6959
"] in the setup");
7060
}
7161

72-
if (!isAlphanumeric(objectClass)) {
62+
if (!StringUtils.isAlphanumeric(objectClass)) {
7363
throw new NullPointerException("object class '" + objectClass +
7464
"' contains non-alphanumeric characters");
7565
}

src/org/opensolaris/opengrok/util/StringUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,20 @@ public static int countURIEndingPushback(String value) {
225225
}
226226
return n;
227227
}
228+
229+
/**
230+
* Find out if string contains only alphanumeric characters.
231+
* @param str string to check
232+
* @return {@code true} if the string is alphanumeric, {@code false} otherwise
233+
*/
234+
public static boolean isAlphanumeric(String str) {
235+
for (int i = 0; i < str.length(); i++) {
236+
char c = str.charAt(i);
237+
if (!Character.isDigit(c) && !Character.isLetter(c)) {
238+
return false;
239+
}
240+
}
241+
242+
return true;
243+
}
228244
}

test/org/opensolaris/opengrok/util/StringUtilsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.util;
2424

25+
import org.junit.Assert;
2526
import org.junit.Test;
2627

2728
import static org.junit.Assert.assertEquals;
@@ -129,4 +130,10 @@ public void uriEmptyShouldNotCountAnyPushback() {
129130
int n = StringUtils.countURIEndingPushback(uri);
130131
assertEquals("empty pushback", 0, n);
131132
}
133+
134+
@Test
135+
public void testIsAlphanumeric() {
136+
Assert.assertTrue(StringUtils.isAlphanumeric("foo123"));
137+
Assert.assertFalse(StringUtils.isAlphanumeric("foo_123"));
138+
}
132139
}

0 commit comments

Comments
 (0)