Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove use of commons-lang3. #51

Merged
merged 1 commit into from
Jul 28, 2024
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
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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-_]";
Expand Down Expand Up @@ -124,7 +123,7 @@ private static Optional<JOSEObject> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
18 changes: 2 additions & 16 deletions src/main/java/com/blackberry/jwteditor/presenter/EditorModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,26 +55,13 @@ List<String> getJOSEObjectStrings() {

String getMessage() {
synchronized (lock) {
// Create two lists, one containing the original, the other containing the modified version at the same index
List<String> searchList = new ArrayList<>();
List<String> 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;
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/blackberry/jwteditor/utils/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/blackberry/jwteditor/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 50 additions & 0 deletions src/test/java/com/blackberry/jwteditor/utils/StringUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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);
}
}
Loading