Skip to content

Commit

Permalink
SEBSP-143 add utf8 encoding to body encoding function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadim Ritter committed Jun 19, 2024
1 parent ae37a8c commit a8bcaa5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
63 changes: 42 additions & 21 deletions src/main/java/ch/ethz/seb/sebserver/gbl/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,27 +648,24 @@ public static String toAppFormUrlEncodedBody(final MultiValueMap<String, String>
return StringUtils.EMPTY;
}

return attributes
.entrySet()
.stream()
.reduce(
new StringBuilder(),
(sb, entry) -> {
final String name = entry.getKey();
final List<String> values = entry.getValue();
if (values == null) {
return sb;
}
if (!sb.isEmpty()) {
sb.append(Constants.AMPERSAND);
}
if (values.size() == 1) {
return sb.append(name).append(Constants.EQUALITY_SIGN).append(values.get(0));
}
return sb.append(toAppFormUrlEncodedBody(name, values));
},
StringBuilder::append)
.toString();
return reduceFormUrlAttributes(attributes);
}

public static String toAppFormUrlEncodedBodyForSPService(final MultiValueMap<String, String> attributes) {
if (attributes == null) {
return StringUtils.EMPTY;
}

for (String key : attributes.keySet()) {
List<String> values = attributes.get(key);
if (values != null) {
for (int i = 0; i < values.size(); i++) {
values.set(i, encodeFormURL_UTF_8(values.get(i)));
}
}
}

return reduceFormUrlAttributes(attributes);
}

public static String toAppFormUrlEncodedBody(@NotNull final String name, final Collection<String> array) {
Expand Down Expand Up @@ -925,4 +922,28 @@ public static Set<Long> getIdsFromString(final String idsString) {
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

private static String reduceFormUrlAttributes(final MultiValueMap<String, String> attributes){
return attributes
.entrySet()
.stream()
.reduce(
new StringBuilder(),
(sb, entry) -> {
final String name = entry.getKey();
final List<String> values = entry.getValue();
if (values == null) {
return sb;
}
if (!sb.isEmpty()) {
sb.append(Constants.AMPERSAND);
}
if (values.size() == 1) {
return sb.append(name).append(Constants.EQUALITY_SIGN).append(values.get(0));
}
return sb.append(toAppFormUrlEncodedBody(name, values));
},
StringBuilder::append)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,11 @@ String createSEBSession(
params.add(SPS_API.SESSION.ATTR_UUID, token);
params.add(SPS_API.SESSION.ATTR_GROUP_ID, localGroup.uuid);
params.add(SPS_API.SESSION.ATTR_CLIENT_IP, clientConnection.getClientAddress());
params.add(SPS_API.SESSION.ATTR_CLIENT_NAME, Utils.encodeFormURL_UTF_8(clientConnection.getExamUserSessionId()));
params.add(SPS_API.SESSION.ATTR_CLIENT_MACHINE_NAME, Utils.encodeFormURL_UTF_8(clientConnection.getClientMachineName()));
params.add(SPS_API.SESSION.ATTR_CLIENT_OS_NAME, Utils.encodeFormURL_UTF_8(clientConnection.getClientOsName()));
params.add(SPS_API.SESSION.ATTR_CLIENT_VERSION, Utils.encodeFormURL_UTF_8(clientConnection.getClientVersion()));
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBody(params);
params.add(SPS_API.SESSION.ATTR_CLIENT_NAME, clientConnection.getExamUserSessionId());
params.add(SPS_API.SESSION.ATTR_CLIENT_MACHINE_NAME, clientConnection.getClientMachineName());
params.add(SPS_API.SESSION.ATTR_CLIENT_OS_NAME, clientConnection.getClientOsName());
params.add(SPS_API.SESSION.ATTR_CLIENT_VERSION, clientConnection.getClientVersion());
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBodyForSPService(params);

final ResponseEntity<String> exchange = apiTemplate.exchange(uri, paramsFormEncoded);
if (exchange.getStatusCode() != HttpStatus.OK) {
Expand Down Expand Up @@ -851,7 +851,7 @@ private ScreenProctoringGroup createGroupOnSPS(
params.add(SPS_API.GROUP.ATTR_NAME, name);
params.add(SPS_API.GROUP.ATTR_DESCRIPTION, description);
params.add(SPS_API.GROUP.ATTR_EXAM_ID, spsExamUUID);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBody(params);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBodyForSPService(params);

final ResponseEntity<String> exchange = apiTemplate.exchange(uri, paramsFormEncoded);
if (exchange.getStatusCode() != HttpStatus.OK) {
Expand Down Expand Up @@ -886,7 +886,7 @@ private void createExam(

final String uuid = createExamUUID(exam);
final MultiValueMap<String, String> params = createExamCreationParams(exam, uuid, userIds);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBody(params);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBodyForSPService(params);

final ResponseEntity<String> exchange = apiTemplate.exchange(uri, paramsFormEncoded);
if (exchange.getStatusCode() != HttpStatus.OK) {
Expand Down Expand Up @@ -996,7 +996,7 @@ private void createSEBAccess(
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add(SPS_API.SEB_ACCESS.ATTR_NAME, name);
params.add(SPS_API.SEB_ACCESS.ATTR_DESCRIPTION, description);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBody(params);
final String paramsFormEncoded = Utils.toAppFormUrlEncodedBodyForSPService(params);

final ResponseEntity<String> exchange = apiTemplate.exchange(uri, paramsFormEncoded);
if (exchange.getStatusCode() != HttpStatus.OK) {
Expand Down

0 comments on commit a8bcaa5

Please sign in to comment.