-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from microsoft/andrueastman/ResponseHeaders
Adds reponseheaders to API excpetion
- Loading branch information
Showing
9 changed files
with
312 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
components/abstractions/src/main/java/com/microsoft/kiota/CaseInsensitiveMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package com.microsoft.kiota; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
class CaseInsensitiveMap implements Map<String, Set<String>>{ | ||
private final HashMap<String, HashSet<String>> internalMap = new HashMap<>(); | ||
|
||
protected String normalizeKey(@Nonnull final String key) { | ||
Objects.requireNonNull(key); | ||
return key.toLowerCase(Locale.ROOT); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public int size() { | ||
return internalMap.size(); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public boolean isEmpty() { | ||
return internalMap.isEmpty(); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public boolean containsKey(@Nonnull final Object key) { | ||
Objects.requireNonNull(key); | ||
if (key instanceof String) { | ||
return internalMap.containsKey(normalizeKey((String) key)); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public boolean containsValue(@Nonnull final Object value) { | ||
Objects.requireNonNull(value); | ||
return internalMap.containsValue(value); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
@Nonnull | ||
public Set<String> get(@Nonnull final Object key) { | ||
Objects.requireNonNull(key); | ||
if (key instanceof String) { | ||
return internalMap.get(normalizeKey((String) key)); | ||
} else { | ||
return Collections.emptySet(); | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
@Nonnull | ||
public Set<String> put(@Nonnull final String key, @Nonnull final Set<String> value) { | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(value); | ||
return internalMap.put(normalizeKey(key), new HashSet<>(value)); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
@Nonnull | ||
public Set<String> remove(@Nonnull final Object key) { | ||
Objects.requireNonNull(key); | ||
if (key instanceof String) { | ||
return internalMap.remove(normalizeKey((String) key)); | ||
} else { | ||
return Collections.emptySet(); | ||
} | ||
} | ||
|
||
@Override | ||
public void putAll(@Nullable final Map<? extends String, ? extends Set<String>> m) { | ||
if (m == null) { | ||
return; | ||
} | ||
for (final Entry<? extends String, ? extends Set<String>> entry : m.entrySet()) { | ||
final String key = entry.getKey(); | ||
final Set<String> value = entry.getValue(); | ||
if (key != null && value != null) { | ||
internalMap.put(normalizeKey(key), new HashSet<>(value)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
internalMap.clear(); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Set<String> keySet() { | ||
return internalMap.keySet(); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Collection<Set<String>> values() { | ||
return new ArrayList<>(internalMap.values()); | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Set<Entry<String, Set<String>>> entrySet() { | ||
final HashSet<Entry<String, Set<String>>> result = new HashSet<>(); | ||
for (final Entry<String, HashSet<String>> entry : internalMap.entrySet()) { | ||
result.add(new Entry<String, Set<String>>() { | ||
@Override | ||
public String getKey() { | ||
return entry.getKey(); | ||
} | ||
|
||
@Override | ||
public Set<String> getValue() { | ||
return entry.getValue(); | ||
} | ||
|
||
@Override | ||
public Set<String> setValue(Set<String> value) { | ||
return entry.setValue(new HashSet<>(value)); | ||
} | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
components/abstractions/src/main/java/com/microsoft/kiota/Headers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.microsoft.kiota; | ||
|
||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
class Headers extends CaseInsensitiveMap { | ||
/** Default constructor */ | ||
public Headers() { | ||
super(); | ||
} | ||
|
||
/** Copy constructor */ | ||
public Headers(@Nonnull Headers headers) { | ||
super(); | ||
Objects.requireNonNull(headers); | ||
putAll(headers); | ||
} | ||
|
||
/** | ||
* Adds a header to the current request. | ||
* | ||
* @param key the key of the header to add. | ||
* @param value the value of the header to add. | ||
*/ | ||
public void add(@Nonnull final String key, @Nonnull final String value) { | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(value); | ||
final String normalizedKey = normalizeKey(key); | ||
if (this.containsKey(normalizedKey)) { | ||
final Set<String> values = this.get(normalizedKey); | ||
values.add(value); | ||
} else { | ||
final Set<String> values = new HashSet<>(1); | ||
values.add(value); | ||
this.put(normalizedKey, values); | ||
} | ||
} | ||
|
||
/** | ||
* Removes a value from a header | ||
* | ||
* @param key the key of the header to remove the value from | ||
* @param value the value to remove | ||
* @return true if the value was removed, false otherwise | ||
*/ | ||
public boolean remove(@Nonnull final String key, @Nonnull final String value) { | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(value); | ||
final String normalizedKey = normalizeKey(key); | ||
if (this.containsKey(normalizedKey)) { | ||
final Set<String> values = this.get(normalizedKey); | ||
if (values.contains(value)) { | ||
values.remove(value); | ||
if (values.isEmpty()) { | ||
this.remove(normalizedKey); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.