Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
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 @@ -70,14 +76,9 @@ public String latestUrl() {
* @return the first value assigned to this header
*/
@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;
public String getHeader(@Nonnull final String name) {
// Header lookup is case-insensitive
final var values = responseHeaders.getOrDefault(name, List.of());
return values.isEmpty() ? null : values.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<String> suggestionList(final String query) throws IOException, Extra
final String contentTypeHeader = response.getHeader("Content-Type");
if (isNullOrEmpty(contentTypeHeader) || !contentTypeHeader.contains("application/json")) {
throw new ExtractionException("Invalid response type (got \"" + contentTypeHeader
+ "\", excepted a JSON response) (response code "
+ "\", expected a JSON response) (response code "
+ response.responseCode() + ")");
}

Expand Down
Loading