Copilot commented on code in PR #88:
URL:
https://github.com/apache/sling-org-apache-sling-api/pull/88#discussion_r4537307161
##########
src/main/java/org/apache/sling/api/request/header/JakartaMediaRangeList.java:
##########
@@ -281,6 +309,45 @@ public Map<String, String> getParameters() {
return parameters != null ? parameters : new HashMap<String,
String>(0);
}
+ private boolean matches(MediaRange mediaType) {
+ if ((!WILDCARD.equals(supertype) &&
!supertype.equalsIgnoreCase(mediaType.supertype))
+ || (!WILDCARD.equals(subtype) &&
!subtype.equalsIgnoreCase(mediaType.subtype))) {
+ return false;
+ }
+ if (parameters != null) {
+ for (Map.Entry<String, String> parameter :
parameters.entrySet()) {
+ if ("q".equals(parameter.getKey())) {
+ break;
+ }
+ if
(!parameter.getValue().equals(mediaType.getParameter(parameter.getKey()))) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ private int compareSpecificity(MediaRange other) {
+ int typeSpecificity = matchesAll() ? 0 : WILDCARD.equals(subtype)
? 1 : 2;
+ int otherTypeSpecificity = other.matchesAll() ? 0 :
WILDCARD.equals(other.subtype) ? 1 : 2;
+ return typeSpecificity != otherTypeSpecificity
+ ? Integer.compare(typeSpecificity, otherTypeSpecificity)
+ : Integer.compare(mediaParameterCount(),
other.mediaParameterCount());
+ }
Review Comment:
Same issue as in `MediaRangeList`: specificity ignores wildcard supertypes
(e.g., `*/json`) and can rank them as fully specific. Align the specificity
calculation to consider `WILDCARD.equals(supertype)` so broad matches are never
ranked above concrete type/subtype matches.
##########
src/main/java/org/apache/sling/api/request/header/MediaRangeList.java:
##########
@@ -199,18 +221,24 @@ public MediaRange(String exp) {
String[] parts = exp.split(";");
this.setType(parts[0].trim());
if (parts.length > 1) {
- this.parameters = new HashMap<String, String>(parts.length -
1);
+ this.parameters = new LinkedHashMap<String,
String>(parts.length - 1);
}
for (int i = 1, partsLength = parts.length; i < partsLength; i++) {
- String parameter = parts[i];
- String[] keyValue = parameter.split("=");
- if (keyValue[0].equals("q")) {
- this.q = Double.parseDouble(keyValue[1]);
- if (this.q < 0 || this.q > 1) {
+ String[] keyValue = parts[i].split("=", 2);
+ if (keyValue.length != 2
+ || keyValue[0].trim().isEmpty()
+ || keyValue[1].trim().isEmpty()) {
+ throw new IllegalArgumentException("Invalid media range
parameter: " + exp);
+ }
+ String name = keyValue[0].trim().toLowerCase(Locale.ROOT);
+ String value = keyValue[1].trim();
+ if (name.equals("q")) {
+ this.q = Double.parseDouble(value);
+ if (!Double.isFinite(this.q) || this.q < 0 || this.q > 1) {
throw new IllegalArgumentException("Quality factor out
of bounds: " + exp);
}
}
- this.parameters.put(keyValue[0], keyValue[1]);
+ this.parameters.put(name, value);
Review Comment:
Parameter names are now normalized to lowercase before being stored. Since
`getParameters()` is part of the public `MediaRange` API, this can be an
observable behavior change for consumers that iterate keys or expect original
casing. To avoid a surprising patch-level change, consider (a) documenting that
parameter names are stored case-insensitively / normalized, and/or (b)
preserving original key casing in `getParameters()` while still doing
case-insensitive lookup internally. (The same applies to the Jakarta variant.)
##########
src/main/java/org/apache/sling/api/request/header/MediaRangeList.java:
##########
@@ -283,6 +311,45 @@ public Map<String, String> getParameters() {
return parameters != null ? parameters : new HashMap<String,
String>(0);
}
+ private boolean matches(MediaRange mediaType) {
+ if ((!WILDCARD.equals(supertype) &&
!supertype.equalsIgnoreCase(mediaType.supertype))
+ || (!WILDCARD.equals(subtype) &&
!subtype.equalsIgnoreCase(mediaType.subtype))) {
+ return false;
+ }
+ if (parameters != null) {
+ for (Map.Entry<String, String> parameter :
parameters.entrySet()) {
+ if ("q".equals(parameter.getKey())) {
+ break;
+ }
+ if
(!parameter.getValue().equals(mediaType.getParameter(parameter.getKey()))) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ private int compareSpecificity(MediaRange other) {
+ int typeSpecificity = matchesAll() ? 0 : WILDCARD.equals(subtype)
? 1 : 2;
+ int otherTypeSpecificity = other.matchesAll() ? 0 :
WILDCARD.equals(other.subtype) ? 1 : 2;
+ return typeSpecificity != otherTypeSpecificity
+ ? Integer.compare(typeSpecificity, otherTypeSpecificity)
+ : Integer.compare(mediaParameterCount(),
other.mediaParameterCount());
+ }
Review Comment:
`compareSpecificity` does not account for a wildcard *supertype* (e.g.,
`*/json`). If such a range is parsed, it will currently be treated as fully
specific (`2`) even though it matches broadly on type. Consider incorporating
`WILDCARD.equals(supertype)` into the specificity calculation so wildcard
supertypes are treated as less specific than concrete supertypes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]