-
Notifications
You must be signed in to change notification settings - Fork 210
Look up headers in a case insensitive way #199
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* -\-\- | ||
* Spotify Apollo API Interfaces | ||
* -- | ||
* Copyright (C) 2013 - 2017 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
package com.spotify.apollo; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import java.util.AbstractMap.SimpleImmutableEntry; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@AutoValue | ||
abstract class Headers { | ||
|
||
static Headers EMPTY = create(Collections.emptyMap()); | ||
|
||
static Headers create(Map<String, String> headers) { | ||
final List<Map.Entry<String, String>> headersList = new ArrayList<>(headers.size()); | ||
headers.entrySet().forEach(h -> insertIfAbsent(headersList, h)); | ||
return new AutoValue_Headers(ImmutableList.copyOf(headersList)); | ||
} | ||
|
||
public Optional<String> get(String name) { | ||
Preconditions.checkArgument(name != null, "Header names cannot be null"); | ||
|
||
for (int i = 0; i < entries().size(); i++) { | ||
final Map.Entry<String, String> headerEntry = entries().get(i); | ||
if (name.equalsIgnoreCase(headerEntry.getKey())) { | ||
return Optional.ofNullable(headerEntry.getValue()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public Map<String, String> asMap() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is used a lot in apollo. Should I memoized it already in the constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure; doing so would increase the memory footprint - you'd need some measurements to be sure, I guess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll leave the optimizations for later then. |
||
ImmutableMap.Builder<String, String> headers = ImmutableMap.builder(); | ||
entries().forEach(headers::put); | ||
return headers.build(); | ||
} | ||
|
||
public abstract ImmutableList<Map.Entry<String, String>> entries(); | ||
|
||
private static void insertIfAbsent(List<Map.Entry<String, String>> headerList, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused here. Method name suggest that the value would not be overwritten (as it will be entered only if it's absent), but that's what the method does? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I fixed the name to insertOrReplace(). Is this better? |
||
Map.Entry<String, String> newHeader) { | ||
|
||
// Replace existing header with new key (letter case can be overwritten) and value | ||
for (int i = 0; i < headerList.size(); i++) { | ||
Map.Entry<String, String> currentHeader = headerList.get(i); | ||
if (currentHeader.getKey().equalsIgnoreCase(newHeader.getKey())) { | ||
headerList.set(i, new SimpleImmutableEntry<>(newHeader)); | ||
return; | ||
} | ||
} | ||
|
||
// No matching entry present, add new entry | ||
headerList.add(new SimpleImmutableEntry<>(newHeader)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
*/ | ||
package com.spotify.apollo; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -67,14 +69,31 @@ default Optional<String> parameter(String parameter) { | |
|
||
/** | ||
* The headers of the request message. | ||
* Deprecated in favor of {@link Request#headerEntries()} | ||
*/ | ||
@Deprecated | ||
Map<String, String> headers(); | ||
|
||
/** | ||
* A header of the request message, or empty if no header with that name is found. | ||
* All the headers of the request message. | ||
*/ | ||
Iterable<Map.Entry<String, String>> headerEntries(); | ||
|
||
|
||
/** | ||
* A header of the request message, looked up in a case insensitive way, | ||
* or empty if no header with that name is found. | ||
*/ | ||
default Optional<String> header(String name) { | ||
return Optional.ofNullable(headers().get(name)); | ||
Preconditions.checkArgument(name != null, "Header names cannot be null"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to permit nulls here, and reverse the comparison below so that it would just fail to find something, rather than throw an exception for a null? I'm not sure. Either way, if nulls shouldn't be permitted, then I think we should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Talked offline about this. I will leave the check with |
||
|
||
for (Map.Entry<String, String> headerEntry : headerEntries()) { | ||
if (name.equalsIgnoreCase(headerEntry.getKey())) { | ||
return Optional.ofNullable(headerEntry.getValue()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
*/ | ||
package com.spotify.apollo; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
|
@@ -40,9 +42,32 @@ public interface Response<T> { | |
|
||
/** | ||
* The response headers. | ||
* Deprecated in favor of {@link Response#headerEntries()} | ||
*/ | ||
@Deprecated | ||
Map<String, String> headers(); | ||
|
||
/** | ||
* The response headers | ||
*/ | ||
Iterable<Map.Entry<String, String>> headerEntries(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why Iterable here? Since we're spending some effort on retaining order of headers, would it not make more sense to return a List? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to be conservative and restrictive, conveying the message that this is only for iterating over all the headers. I want to prevent users from looking up keys from whatever object this method returns. Also, I didn't want to lock a specific implementation (we could change the internal implementation for a Map, etc). But I see now that I chose the wrong interface and it should have been Iterator, but maybe it's too annoying, specially since it does not have size(). What do you think would be better: Iterator or List? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Iterator is generally wrong, since they are one-use only. Iterable is better in that case. I would still prefer List; it's still quite generic, and I feel like we have committed to retaining order. We could even specify that in the javadocs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. Changed to |
||
|
||
/** | ||
* A header of the request message, looked up in a case insensitive way, | ||
* or empty if no header with that name is found. | ||
*/ | ||
default Optional<String> header(String name) { | ||
Preconditions.checkArgument(name != null, "Header names cannot be null"); | ||
|
||
for (Map.Entry<String, String> headerEntry : headerEntries()) { | ||
if (name.equalsIgnoreCase(headerEntry.getKey())) { | ||
return Optional.ofNullable(headerEntry.getValue()); | ||
} | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* The single payload of the response. | ||
*/ | ||
|
@@ -119,4 +144,5 @@ static <T> Response<T> forPayload(T payload) { | |
static <T> Response<T> of(StatusType statusCode, T payload) { | ||
return ResponseImpl.create(statusCode, payload); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably remove this empty line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to use
Objects.requireNonNull
in this case - generally null arguments lead to NPEs, and it's better to use the JDK classes than Guava where feasible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fixed