SEC-1132: Moved TextUtils to web module and StringSplit utils into Digest authentication package (as they aren't used elsewhere).
This commit is contained in:
@@ -1,194 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.util;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Provides several <code>String</code> manipulation methods.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public final class StringSplitUtils {
|
||||
//~ Static fields/initializers =====================================================================================
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
private StringSplitUtils() {
|
||||
}
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
/**
|
||||
* Splits a <code>String</code> at the first instance of the delimiter.<p>Does not include the delimiter in
|
||||
* the response.</p>
|
||||
*
|
||||
* @param toSplit the string to split
|
||||
* @param delimiter to split the string up with
|
||||
* @return a two element array with index 0 being before the delimiter, and index 1 being after the delimiter
|
||||
* (neither element includes the delimiter)
|
||||
* @throws IllegalArgumentException if an argument was invalid
|
||||
*/
|
||||
public static String[] split(String toSplit, String delimiter) {
|
||||
Assert.hasLength(toSplit, "Cannot split a null or empty string");
|
||||
Assert.hasLength(delimiter, "Cannot use a null or empty delimiter to split a string");
|
||||
|
||||
if (delimiter.length() != 1) {
|
||||
throw new IllegalArgumentException("Delimiter can only be one character in length");
|
||||
}
|
||||
|
||||
int offset = toSplit.indexOf(delimiter);
|
||||
|
||||
if (offset < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String beforeDelimiter = toSplit.substring(0, offset);
|
||||
String afterDelimiter = toSplit.substring(offset + 1);
|
||||
|
||||
return new String[]{beforeDelimiter, afterDelimiter};
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an array of <code>String</code>s, and for each element removes any instances of
|
||||
* <code>removeCharacter</code>, and splits the element based on the <code>delimiter</code>. A <code>Map</code> is
|
||||
* then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the
|
||||
* value.<p>Will trim both the key and value before adding to the <code>Map</code>.</p>
|
||||
*
|
||||
* @param array the array to process
|
||||
* @param delimiter to split each element using (typically the equals symbol)
|
||||
* @param removeCharacters one or more characters to remove from each element prior to attempting the split
|
||||
* operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur
|
||||
* @return a <code>Map</code> representing the array contents, or <code>null</code> if the array to process was
|
||||
* null or empty
|
||||
*/
|
||||
public static Map<String, String> splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters) {
|
||||
if ((array == null) || (array.length == 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
String postRemove;
|
||||
|
||||
if (removeCharacters == null) {
|
||||
postRemove = array[i];
|
||||
} else {
|
||||
postRemove = StringUtils.replace(array[i], removeCharacters, "");
|
||||
}
|
||||
|
||||
String[] splitThisArrayElement = split(postRemove, delimiter);
|
||||
|
||||
if (splitThisArrayElement == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
map.put(splitThisArrayElement[0].trim(), splitThisArrayElement[1].trim());
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String substringBeforeLast(String str, String separator) {
|
||||
if (str == null || separator == null || str.length() == 0 || separator.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
int pos = str.lastIndexOf(separator);
|
||||
if (pos == -1) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, pos);
|
||||
}
|
||||
|
||||
public static String substringAfterLast(String str, String separator) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return str;
|
||||
}
|
||||
if (separator == null || separator.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
int pos = str.lastIndexOf(separator);
|
||||
if (pos == -1 || pos == (str.length() - separator.length())) {
|
||||
return "";
|
||||
}
|
||||
return str.substring(pos + separator.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a given string on the given separator character, skips the contents of quoted substrings
|
||||
* when looking for separators.
|
||||
* Introduced for use in DigestProcessingFilter (see SEC-506).
|
||||
* <p/>
|
||||
* This was copied and modified from commons-lang StringUtils
|
||||
*/
|
||||
public static String[] splitIgnoringQuotes(String str, char separatorChar) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int len = str.length();
|
||||
|
||||
if (len == 0) {
|
||||
return EMPTY_STRING_ARRAY;
|
||||
}
|
||||
|
||||
List<String> list = new ArrayList<String>();
|
||||
int i = 0;
|
||||
int start = 0;
|
||||
boolean match = false;
|
||||
|
||||
while (i < len) {
|
||||
if (str.charAt(i) == '"') {
|
||||
i++;
|
||||
while (i < len) {
|
||||
if (str.charAt(i) == '"') {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
match = true;
|
||||
continue;
|
||||
}
|
||||
if (str.charAt(i) == separatorChar) {
|
||||
if (match) {
|
||||
list.add(str.substring(start, i));
|
||||
match = false;
|
||||
}
|
||||
start = ++i;
|
||||
continue;
|
||||
}
|
||||
match = true;
|
||||
i++;
|
||||
}
|
||||
if (match) {
|
||||
list.add(str.substring(start, i));
|
||||
}
|
||||
|
||||
return list.toArray(new String[list.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package org.springframework.security.util;
|
||||
|
||||
/**
|
||||
* Utilities for working with Strings and text.
|
||||
*
|
||||
* @author Luke Taylor
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class TextUtils {
|
||||
|
||||
public static String escapeEntities(String s) {
|
||||
if (s == null || s.length() == 0) {
|
||||
return s;
|
||||
}
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i=0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if(c == '<') {
|
||||
sb.append("<");
|
||||
} else if (c == '>') {
|
||||
sb.append(">");
|
||||
} else if (c == '"') {
|
||||
sb.append(""");
|
||||
} else if (c == '\'') {
|
||||
sb.append("'");
|
||||
} else if (c == '&') {
|
||||
sb.append("&");
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link org.springframework.security.util.StringSplitUtils}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class StringSplitUtilsTests extends TestCase {
|
||||
//~ Constructors ===================================================================================================
|
||||
|
||||
//~ Methods ========================================================================================================
|
||||
|
||||
public void testSplitEachArrayElementAndCreateMapNormalOperation() {
|
||||
// note it ignores malformed entries (ie those without an equals sign)
|
||||
String unsplit = "username=\"rod\", invalidEntryThatHasNoEqualsSign, realm=\"Contacts Realm\", nonce=\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\", uri=\"/spring-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\", response=\"38644211cf9ac3da63ab639807e2baff\", qop=auth, nc=00000004, cnonce=\"2b8d329a8571b99a\"";
|
||||
String[] headerEntries = StringUtils.commaDelimitedListToStringArray(unsplit);
|
||||
Map<String, String> headerMap = StringSplitUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");
|
||||
|
||||
assertEquals("rod", headerMap.get("username"));
|
||||
assertEquals("Contacts Realm", headerMap.get("realm"));
|
||||
assertEquals("MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==", headerMap.get("nonce"));
|
||||
assertEquals("/spring-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4",
|
||||
headerMap.get("uri"));
|
||||
assertEquals("38644211cf9ac3da63ab639807e2baff", headerMap.get("response"));
|
||||
assertEquals("auth", headerMap.get("qop"));
|
||||
assertEquals("00000004", headerMap.get("nc"));
|
||||
assertEquals("2b8d329a8571b99a", headerMap.get("cnonce"));
|
||||
assertEquals(8, headerMap.size());
|
||||
}
|
||||
|
||||
public void testSplitEachArrayElementAndCreateMapRespectsInstructionNotToRemoveCharacters() {
|
||||
String unsplit = "username=\"rod\", realm=\"Contacts Realm\", nonce=\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\", uri=\"/spring-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\", response=\"38644211cf9ac3da63ab639807e2baff\", qop=auth, nc=00000004, cnonce=\"2b8d329a8571b99a\"";
|
||||
String[] headerEntries = StringUtils.commaDelimitedListToStringArray(unsplit);
|
||||
Map<String, String> headerMap = StringSplitUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", null);
|
||||
|
||||
assertEquals("\"rod\"", headerMap.get("username"));
|
||||
assertEquals("\"Contacts Realm\"", headerMap.get("realm"));
|
||||
assertEquals("\"MTEwOTAyMzU1MTQ4NDo1YzY3OWViYWM5NDNmZWUwM2UwY2NmMDBiNDQzMTQ0OQ==\"", headerMap.get("nonce"));
|
||||
assertEquals("\"/spring-security-sample-contacts-filter/secure/adminPermission.htm?contactId=4\"",
|
||||
headerMap.get("uri"));
|
||||
assertEquals("\"38644211cf9ac3da63ab639807e2baff\"", headerMap.get("response"));
|
||||
assertEquals("auth", headerMap.get("qop"));
|
||||
assertEquals("00000004", headerMap.get("nc"));
|
||||
assertEquals("\"2b8d329a8571b99a\"", headerMap.get("cnonce"));
|
||||
assertEquals(8, headerMap.size());
|
||||
}
|
||||
|
||||
public void testSplitEachArrayElementAndCreateMapReturnsNullIfArrayEmptyOrNull() {
|
||||
assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(null, "=", "\""));
|
||||
assertNull(StringSplitUtils.splitEachArrayElementAndCreateMap(new String[]{}, "=", "\""));
|
||||
}
|
||||
|
||||
public void testSplitNormalOperation() {
|
||||
String unsplit = "username=\"rod==\"";
|
||||
assertEquals("username", StringSplitUtils.split(unsplit, "=")[0]);
|
||||
assertEquals("\"rod==\"", StringSplitUtils.split(unsplit, "=")[1]); // should not remove quotes or extra equals
|
||||
}
|
||||
|
||||
public void testSplitRejectsNullsAndIncorrectLengthStrings() {
|
||||
try {
|
||||
StringSplitUtils.split(null, "="); // null
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
StringSplitUtils.split("", "="); // empty string
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
StringSplitUtils.split("sdch=dfgf", null); // null
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
StringSplitUtils.split("fvfv=dcdc", ""); // empty string
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
StringSplitUtils.split("dfdc=dcdc", "BIGGER_THAN_ONE_CHARACTER");
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testSplitWorksWithDifferentDelimiters() {
|
||||
assertEquals(2, StringSplitUtils.split("18/rod", "/").length);
|
||||
assertNull(StringSplitUtils.split("18/rod", "!"));
|
||||
|
||||
// only guarantees to split at FIRST delimiter, not EACH delimiter
|
||||
assertEquals(2, StringSplitUtils.split("18|rod|foo|bar", "|").length);
|
||||
}
|
||||
|
||||
|
||||
public void testAuthorizationHeaderWithCommasIsSplitCorrectly() {
|
||||
String header = "Digest username=\"hamilton,bob\", realm=\"bobs,ok,realm\", nonce=\"the,nonce\", " +
|
||||
"uri=\"the,Uri\", response=\"the,response,Digest\", qop=theqop, nc=thenc, cnonce=\"the,cnonce\"";
|
||||
|
||||
String[] parts = StringSplitUtils.splitIgnoringQuotes(header, ',');
|
||||
|
||||
assertEquals(8, parts.length);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package org.springframework.security.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TextUtilsTests {
|
||||
|
||||
@Test
|
||||
public void charactersAreEscapedCorrectly() {
|
||||
assertEquals("a<script>"'", TextUtils.escapeEntities("a<script>\"'"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user