-
Notifications
You must be signed in to change notification settings - Fork 32
Creates Backward Compatible Context Class #286
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
Merged
muditchaudhary
merged 15 commits into
cedar-policy:main
from
muditchaudhary:createContext
Feb 12, 2025
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
80f2a3b
Add skeleton Context
muditchaudhary 54f5681
Adds context methods
muditchaudhary 2d3c257
adds bin/ to .gitignore
muditchaudhary 6cc055d
adds toString and isEmpty method to Context
muditchaudhary 930ce2d
adds overloaded constructors to AuthorizationRequest
muditchaudhary 4aa1363
adds overloaded builder method to accept Context object for PartialAu…
muditchaudhary dac4502
adds unit testsfor Context
muditchaudhary 4112cdf
fixes styleCheck warnings
muditchaudhary 317c807
adds tests for backward compatible Context for isAuthorized
muditchaudhary 18c1b73
adds unit tests for Context for PartialAuthorization
muditchaudhary 1af1780
cleans up comments
muditchaudhary 582f921
fixes nits
muditchaudhary d557bdb
refactors Context
muditchaudhary 94f23a9
refactors Context
muditchaudhary 4ba2a1a
refactors Context to fix tests
muditchaudhary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
148 changes: 148 additions & 0 deletions
148
CedarJava/src/main/java/com/cedarpolicy/model/Context.java
This file contains hidden or 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,148 @@ | ||
| /* | ||
| * Copyright Cedar Contributors | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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.cedarpolicy.model; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Collections; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.StreamSupport; | ||
| import java.util.Map; | ||
| import com.cedarpolicy.value.Value; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
|
||
|
|
||
| public class Context { | ||
|
|
||
| private Map<String, Value> context; | ||
|
|
||
| /** | ||
| * Constructs a new empty Context with no key-value pairs. | ||
| * Initializes the internal context map as an empty immutable map. | ||
| */ | ||
| public Context() { | ||
| context = Collections.emptyMap(); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return context.isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new Context from an Iterable of key-value pairs. | ||
| * Creates a new HashMap and populates it with the provided entries. | ||
| * Equivalent to from_pairs in Cedar Rust. | ||
| * | ||
| * @param contextList An Iterable containing key-value pairs to initialize this context with | ||
| * @throws IllegalStateException if a duplicate key is found within the iterable | ||
| * @throws IllegalArgumentException if the contextList parameter is null | ||
| */ | ||
| @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") | ||
| public Context(Iterable<Map.Entry<String, Value>> contextList) { | ||
| context = new HashMap<>(); | ||
| fromIterable(contextList); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new Context with the provided map of key-value pairs. | ||
| * Creates a defensive copy of the input map to maintain immutability. | ||
| * | ||
| * @param contextMap The map of key-value pairs to initialize this context with | ||
| * @throws IllegalArgumentException if the contextMap parameter is null | ||
| */ | ||
| public Context(Map<String, Value> contextMap) { | ||
| context = new HashMap<>(); | ||
| context.putAll(contextMap); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a defensive copy of the internal context map. | ||
| * | ||
| * @return A new HashMap containing all key-value pairs from the internal context | ||
| */ | ||
| public Map<String, Value> getContext() { | ||
| return new HashMap<>(context); | ||
| } | ||
|
|
||
| /** | ||
| * Merges another Context object into the current context. | ||
| * | ||
| * @param contextToMerge The Context object to merge into this context | ||
| * @throws IllegalStateException if a duplicate key is found while merging the context | ||
| * @throws IllegalArgumentException if the contextToMerge parameter is null | ||
| */ | ||
| public void merge(Context contextToMerge) throws IllegalStateException, IllegalArgumentException { | ||
| fromIterable(contextToMerge.getContext().entrySet()); | ||
| } | ||
|
|
||
| /** | ||
| * Merges the provided key-value pairs into the current context. | ||
| * | ||
| * @param contextMaps An Iterable containing key-value pairs to merge into this context | ||
| * @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found within the iterable | ||
| * @throws IllegalArgumentException if the contextMaps parameter is null | ||
| */ | ||
| public void merge(Iterable<Map.Entry<String, Value>> contextMaps) throws IllegalStateException, IllegalArgumentException { | ||
| fromIterable(contextMaps); | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the Value associated with the specified key from the context. | ||
| * | ||
| * @param key The key whose associated Value is to be returned | ||
| * @return The Value associated with the specified key, or null if the key is not found replicating Cedar Rust behavior | ||
| * @throws IllegalArgumentException if the key parameter is null | ||
| */ | ||
| public Value get(String key) { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException("Key cannot be null"); | ||
| } | ||
| return context.getOrDefault(key, null); | ||
| } | ||
|
|
||
| /** | ||
| * Processes an Iterable of Map entries and adds them to the context. | ||
| * | ||
| * @param contextIterator The Iterable containing key-value pairs to add to the context | ||
| * @throws IllegalStateException if a duplicate key is found in the existing context or duplicate key found within the iterable | ||
| * @throws IllegalArgumentException if the contextIterator is null | ||
| */ | ||
| private void fromIterable(Iterable<Map.Entry<String, Value>> contextIterator) throws IllegalStateException, IllegalArgumentException { | ||
| if (contextIterator == null) { | ||
| throw new IllegalArgumentException("Context iterator cannot be null"); | ||
| } | ||
|
|
||
| Map<String, Value> newEntries = StreamSupport.stream(contextIterator.spliterator(), false) | ||
| .peek(entry -> { | ||
| if (context.containsKey(entry.getKey())) { | ||
| throw new IllegalStateException( | ||
| String.format("Duplicate key '%s' in existing context", entry.getKey()) | ||
| ); | ||
| } | ||
| }) | ||
| .collect(Collectors.toMap( | ||
| Map.Entry::getKey, | ||
| Map.Entry::getValue | ||
| )); | ||
| context.putAll(newEntries); | ||
| } | ||
|
|
||
| /** Readable string representation. */ | ||
| @Override | ||
| public String toString() { | ||
| return context.toString(); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.