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

Use TreeMap in Response #1198

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
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,
@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);
}

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