diff --git a/build.gradle b/build.gradle index 0d9c8d8..eeaa9e4 100644 --- a/build.gradle +++ b/build.gradle @@ -36,8 +36,7 @@ dependencies { 'com.nimbusds:nimbus-jose-jwt:9.21', 'org.exbin.deltahex:deltahex-swing:0.1.2', 'com.fifesoft:rsyntaxtextarea:3.4.1', - 'org.json:json:20240303', - 'org.apache.commons:commons-lang3:3.14.0' + 'org.json:json:20240303' ) testImplementation( "org.bouncycastle:bcprov-jdk18on:${bouncycastle_version}", diff --git a/src/main/java/com/blackberry/jwteditor/model/jose/JOSEObjectFinder.java b/src/main/java/com/blackberry/jwteditor/model/jose/JOSEObjectFinder.java index 8150350..cd5f2c3 100644 --- a/src/main/java/com/blackberry/jwteditor/model/jose/JOSEObjectFinder.java +++ b/src/main/java/com/blackberry/jwteditor/model/jose/JOSEObjectFinder.java @@ -29,7 +29,6 @@ import static com.nimbusds.jose.Header.MAX_HEADER_STRING_LENGTH; import static com.nimbusds.jose.HeaderParameterNames.ALGORITHM; -import static org.apache.commons.lang3.StringUtils.isBlank; public class JOSEObjectFinder { public static final String BASE64_REGEX = "[A-Za-z0-9-_]"; @@ -124,7 +123,7 @@ private static Optional parseJWS(String candidate) { String algValue = JSONObjectUtils.getString(headerJson, ALGORITHM); - if (isBlank(algValue)) { + if (algValue == null || algValue.isBlank()) { throw new ParseException("Missing \"alg\" in header JSON object", 0); } diff --git a/src/main/java/com/blackberry/jwteditor/model/jose/JWEFactory.java b/src/main/java/com/blackberry/jwteditor/model/jose/JWEFactory.java index 4737343..b761d9e 100644 --- a/src/main/java/com/blackberry/jwteditor/model/jose/JWEFactory.java +++ b/src/main/java/com/blackberry/jwteditor/model/jose/JWEFactory.java @@ -22,13 +22,13 @@ import com.blackberry.jwteditor.model.keys.Key; import com.nimbusds.jose.*; import com.nimbusds.jose.util.Base64URL; -import org.apache.commons.lang3.StringUtils; import java.nio.charset.StandardCharsets; import java.security.Provider; import java.security.Security; import java.text.ParseException; +import static com.blackberry.jwteditor.utils.StringUtils.countOccurrences; import static java.util.Arrays.stream; public class JWEFactory { @@ -75,7 +75,7 @@ public static JWE encrypt(JWS jws, Key key, JWEAlgorithm kek, EncryptionMethod c * @throws ParseException if the value is not a valid JWE */ public static JWE parse(String compactJWE) throws ParseException { - if (StringUtils.countMatches(compactJWE, ".") != 4) { + if (countOccurrences(compactJWE, '.') != 4) { throw new ParseException("Invalid number of encoded fields", 0); } diff --git a/src/main/java/com/blackberry/jwteditor/model/jose/JWSFactory.java b/src/main/java/com/blackberry/jwteditor/model/jose/JWSFactory.java index 64b44d4..1b6f058 100644 --- a/src/main/java/com/blackberry/jwteditor/model/jose/JWSFactory.java +++ b/src/main/java/com/blackberry/jwteditor/model/jose/JWSFactory.java @@ -23,12 +23,12 @@ import com.nimbusds.jose.JWSAlgorithm; import com.nimbusds.jose.JWSHeader; import com.nimbusds.jose.util.Base64URL; -import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; import java.text.ParseException; +import static com.blackberry.jwteditor.utils.StringUtils.countOccurrences; import static com.nimbusds.jose.HeaderParameterNames.*; import static java.util.Arrays.stream; @@ -94,7 +94,7 @@ public static JWS sign(Key key, JWSAlgorithm algorithm, Base64URL header, Base64 * @throws ParseException if parsing fails */ public static JWS parse(String compactJWS) throws ParseException { - if (StringUtils.countMatches(compactJWS, ".") != 2) { + if (countOccurrences(compactJWS, '.') != 2) { throw new ParseException("Invalid number of encoded sections", 0); } diff --git a/src/main/java/com/blackberry/jwteditor/presenter/EditorModel.java b/src/main/java/com/blackberry/jwteditor/presenter/EditorModel.java index a5920b0..5808877 100644 --- a/src/main/java/com/blackberry/jwteditor/presenter/EditorModel.java +++ b/src/main/java/com/blackberry/jwteditor/presenter/EditorModel.java @@ -19,7 +19,6 @@ package com.blackberry.jwteditor.presenter; import com.blackberry.jwteditor.model.jose.MutableJOSEObject; -import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; @@ -56,26 +55,13 @@ List getJOSEObjectStrings() { String getMessage() { synchronized (lock) { - // Create two lists, one containing the original, the other containing the modified version at the same index - List searchList = new ArrayList<>(); - List replacementList = new ArrayList<>(); - - // Add a replacement pair to the lists if the JOSEObjectPair has changed for (MutableJOSEObject mutableJoseObject : mutableJoseObjects) { if (mutableJoseObject.changed()) { - searchList.add(mutableJoseObject.getOriginal()); - replacementList.add(mutableJoseObject.getModified().serialize()); + message = message.replace(mutableJoseObject.getOriginal(), mutableJoseObject.getModified().serialize()); } } - // Convert the lists to arrays - String[] search = new String[searchList.size()]; - searchList.toArray(search); - String[] replacement = new String[replacementList.size()]; - replacementList.toArray(replacement); - - // Use the Apache Commons StringUtils library to do in-place replacement - return StringUtils.replaceEach(message, search, replacement); + return message; } } diff --git a/src/main/java/com/blackberry/jwteditor/utils/JSONUtils.java b/src/main/java/com/blackberry/jwteditor/utils/JSONUtils.java index cb53187..3d54b66 100644 --- a/src/main/java/com/blackberry/jwteditor/utils/JSONUtils.java +++ b/src/main/java/com/blackberry/jwteditor/utils/JSONUtils.java @@ -18,7 +18,6 @@ package com.blackberry.jwteditor.utils; -import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -53,14 +52,14 @@ public static String prettyPrintJSON(String json) { indentationLevel++; stringBuilder.append(c); stringBuilder.append('\n'); - stringBuilder.append(StringUtils.repeat(' ', indentationLevel * JSON_INDENTATION)); + stringBuilder.append(" ".repeat( indentationLevel * JSON_INDENTATION)); break; case '}': case ']': indentationLevel--; stringBuilder.append('\n'); - stringBuilder.append(StringUtils.repeat(' ', indentationLevel * JSON_INDENTATION)); + stringBuilder.append(" ".repeat(indentationLevel * JSON_INDENTATION)); stringBuilder.append(c); break; @@ -70,7 +69,7 @@ public static String prettyPrintJSON(String json) { case ',': stringBuilder.append(",\n"); - stringBuilder.append(StringUtils.repeat(' ', indentationLevel * JSON_INDENTATION)); + stringBuilder.append(" ".repeat(indentationLevel * JSON_INDENTATION)); break; default: diff --git a/src/main/java/com/blackberry/jwteditor/utils/StringUtils.java b/src/main/java/com/blackberry/jwteditor/utils/StringUtils.java new file mode 100644 index 0000000..e642858 --- /dev/null +++ b/src/main/java/com/blackberry/jwteditor/utils/StringUtils.java @@ -0,0 +1,38 @@ +/* +Author : Dolph Flynn + +Copyright 2024 Dolph Flynn + +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 com.blackberry.jwteditor.utils; + +public class StringUtils { + + public static int countOccurrences(String data, char seek) { + if (data == null) { + return 0; + } + + int count = 0; + + for (char c : data.toCharArray()) { + if (c == seek) { + count++; + } + } + + return count; + } +} diff --git a/src/test/java/com/blackberry/jwteditor/utils/StringUtilsTest.java b/src/test/java/com/blackberry/jwteditor/utils/StringUtilsTest.java new file mode 100644 index 0000000..978b3b0 --- /dev/null +++ b/src/test/java/com/blackberry/jwteditor/utils/StringUtilsTest.java @@ -0,0 +1,50 @@ +/* +Author : Dolph Flynn + +Copyright 2024 Dolph Flynn + +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 com.blackberry.jwteditor.utils; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +class StringUtilsTest { + private static Stream data() { + return Stream.of( + arguments(null, 'z', 0), + arguments("", 'z', 0), + arguments("abc", 'z', 0), + arguments("zabc", 'z', 1), + arguments("zabz", 'z', 2), + arguments("abc reggegeg\0fwfwfwz", 'z', 1), + arguments("abc zzzzz", 'z', 5) + ); + } + + @MethodSource("data") + @ParameterizedTest + void testCountOccurrences(String data, char c, int expected) { + int actual = StringUtils.countOccurrences(data, c); + + assertThat(actual).isEqualTo(expected); + } +} \ No newline at end of file