Skip to content
Closed
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
* A Data class used to hold the results from requests made by the Downloader implementation.
*/
public class Response {
private final int responseCode;
private final String responseMessage;
private final Map<String, List<String>> responseHeaders;
private final SortedMap<String, List<String>> responseHeaders;
private final String responseBody;

private final String latestUrl;

public Response(final int responseCode,
final String responseMessage,
@Nullable final Map<String, List<String>> responseHeaders,
@Nonnull final Map<String, List<String>> responseHeaders,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed to be @Nonnull? Isn't this a breaking change?

@Nullable final String responseBody,
@Nullable final String latestUrl) {
this.responseCode = responseCode;
this.responseMessage = responseMessage;
this.responseHeaders = responseHeaders == null ? Collections.emptyMap() : responseHeaders;

if (responseHeaders instanceof SortedMap) {
this.responseHeaders = (SortedMap<String, List<String>>) responseHeaders;
} else {
this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.responseHeaders.putAll(responseHeaders);
}
Comment on lines +28 to +33
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmh, this still means that all items are reprocessed when building a Response in case the headers were not already in a SortedMap. Also, how can you be sure that responseHeaders was built using String.CASE_INSENSITIVE_ORDER?

I would just build a new TreeMap every time.


this.responseBody = responseBody == null ? "" : responseBody;
this.latestUrl = latestUrl;
Expand Down Expand Up @@ -71,13 +78,8 @@ public String latestUrl() {
*/
@Nullable
public String getHeader(final String name) {
for (final Map.Entry<String, List<String>> headerEntry : responseHeaders.entrySet()) {
final String key = headerEntry.getKey();
if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) {
return headerEntry.getValue().get(0);
}
}

return null;
// Header lookup is case-insensitive
final var values = responseHeaders.getOrDefault(name, Collections.emptyList());
return values.isEmpty() ? null : values.get(0);
}
}
Loading