-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add
Configuration
api (#44)
Feature: enhance `RequestMatcher` api Feature: enhance `ActionMatcherFactory` SPI Feature: enhance `ConditionMatcherFactory` SPI
- Loading branch information
Showing
62 changed files
with
576 additions
and
213 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
cosec-api/src/main/kotlin/me/ahoo/cosec/api/configuration/Configuration.kt
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,35 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosec.api.configuration | ||
|
||
interface Configuration { | ||
fun get(key: String): Configuration? | ||
fun getRequired(key: String): Configuration = requireNotNull(get(key)) { | ||
"Configuration[$key] is required!" | ||
} | ||
|
||
fun asList(): List<Configuration> | ||
fun asMap(): Map<String, Configuration> | ||
fun asString(): String | ||
fun asBoolean(): Boolean | ||
fun asInt(): Int | ||
fun asLong(): Long | ||
fun <T> asPojo(pojoClass: Class<T>): T | ||
|
||
fun has(key: String): Boolean = get(key) != null | ||
fun asStringList(): List<String> = asList().map { it.asString() } | ||
fun asStringMap(): Map<String, String> = asMap().mapValues { it.value.asString() } | ||
} | ||
|
||
inline fun <reified T> Configuration.asPojo(): T = asPojo(T::class.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
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
84 changes: 84 additions & 0 deletions
84
cosec-core/src/main/kotlin/me/ahoo/cosec/configuration/JsonConfiguration.kt
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,84 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosec.configuration | ||
|
||
import com.fasterxml.jackson.core.ObjectCodec | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.NullNode | ||
import me.ahoo.cosec.Delegated | ||
import me.ahoo.cosec.api.configuration.Configuration | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
|
||
class JsonConfiguration( | ||
override val delegate: JsonNode, | ||
private val objectCodec: ObjectCodec | ||
) : Configuration, | ||
Delegated<JsonNode> { | ||
|
||
companion object { | ||
val EMPTY: JsonConfiguration by lazy { | ||
JsonConfiguration(NullNode.getInstance(), CoSecJsonSerializer) | ||
} | ||
|
||
fun Map<String, *>.asConfiguration(): JsonConfiguration { | ||
val jsonString = CoSecJsonSerializer.writeValueAsString(this) | ||
return JsonConfiguration(CoSecJsonSerializer.readTree(jsonString), CoSecJsonSerializer) | ||
} | ||
} | ||
|
||
override fun get(key: String): Configuration? { | ||
return delegate.get(key)?.let { JsonConfiguration(it, objectCodec) } | ||
} | ||
|
||
override fun asList(): List<Configuration> { | ||
return buildList { | ||
val elements = delegate.elements() | ||
while (elements.hasNext()) { | ||
add(JsonConfiguration(elements.next(), objectCodec)) | ||
} | ||
} | ||
} | ||
|
||
override fun asMap(): Map<String, Configuration> { | ||
return buildMap { | ||
val fields = delegate.fields() | ||
while (fields.hasNext()) { | ||
val field = fields.next() | ||
put(field.key, JsonConfiguration(field.value, objectCodec)) | ||
} | ||
} | ||
} | ||
|
||
override fun asString(): String { | ||
return delegate.asText() | ||
} | ||
|
||
override fun asBoolean(): Boolean { | ||
return delegate.asBoolean() | ||
} | ||
|
||
override fun asInt(): Int { | ||
return delegate.asInt() | ||
} | ||
|
||
override fun asLong(): Long { | ||
return delegate.asLong() | ||
} | ||
|
||
override fun <T> asPojo(pojoClass: Class<T>): T { | ||
return delegate.traverse(objectCodec).use { | ||
it.readValueAs(pojoClass) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
cosec-core/src/main/kotlin/me/ahoo/cosec/policy/MatcherConfiguration.kt
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,27 @@ | ||
/* | ||
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)]. | ||
* 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 me.ahoo.cosec.policy | ||
|
||
import me.ahoo.cosec.api.configuration.Configuration | ||
|
||
const val MATCHER_TYPE_KEY = "type" | ||
const val MATCHER_PATTERN_KEY = "pattern" | ||
|
||
fun Configuration.getMatcherType(): String { | ||
return getRequired(MATCHER_TYPE_KEY).asString() | ||
} | ||
|
||
fun Configuration.getMatcherPattern(): String { | ||
return getRequired(MATCHER_PATTERN_KEY).asString() | ||
} |
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
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
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
Oops, something went wrong.