Skip to content

Commit

Permalink
feat: rename QueryParameter add allowed parameters to entityhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Nov 28, 2024
1 parent 3ba900f commit 9803a58
Show file tree
Hide file tree
Showing 28 changed files with 308 additions and 237 deletions.
2 changes: 1 addition & 1 deletion config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ style:
active: true
UnusedParameter:
active: true
allowedNames: 'ignored|expected'
allowedNames: 'ignored|expected|params'
UnusedPrivateClass:
active: true
UnusedPrivateMember:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.egm.stellio.search.csr.model.MiscellaneousWarning
import com.egm.stellio.search.csr.model.NGSILDWarning
import com.egm.stellio.search.csr.model.RevalidationFailedWarning
import com.egm.stellio.shared.model.CompactedEntity
import com.egm.stellio.shared.model.parameter.QueryParam
import com.egm.stellio.shared.model.parameter.QueryParameter
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.core.codec.DecodingException
Expand All @@ -36,9 +36,9 @@ object ContextSourceCaller {
val uri = URI("${csr.endpoint}$path")

val queryParams = CollectionUtils.toMultiValueMap(params.toMutableMap())
queryParams.remove(QueryParam.GEOMETRY_PROPERTY.key)
queryParams.remove(QueryParam.OPTIONS.key) // only normalized request
queryParams.remove(QueryParam.LANG.key)
queryParams.remove(QueryParameter.GEOMETRY_PROPERTY.key)
queryParams.remove(QueryParameter.OPTIONS.key) // only normalized request
queryParams.remove(QueryParameter.LANG.key)

val request = WebClient.create()
.method(HttpMethod.GET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.egm.stellio.shared.config.ApplicationProperties
import com.egm.stellio.shared.model.APIException
import com.egm.stellio.shared.model.AccessDeniedException
import com.egm.stellio.shared.model.PaginationQuery.Companion.parsePaginationParameters
import com.egm.stellio.shared.model.parameter.QueryParam
import com.egm.stellio.shared.model.parameter.QueryParameter
import com.egm.stellio.shared.util.JSON_LD_CONTENT_TYPE
import com.egm.stellio.shared.util.JsonUtils.deserializeAsMap
import com.egm.stellio.shared.util.Sub
Expand Down Expand Up @@ -86,8 +86,8 @@ class ContextSourceRegistrationHandler(
val mediaType = getApplicableMediaType(httpHeaders).bind()
val sub = getSubFromSecurityContext()

val includeSysAttrs = params.getOrDefault(QueryParam.OPTIONS.key, emptyList())
.contains(QueryParam.OptionValue.SYS_ATTRS.value)
val includeSysAttrs = params.getOrDefault(QueryParameter.OPTIONS.key, emptyList())
.contains(QueryParameter.SYS_ATTRS.key)
val paginationQuery = parsePaginationParameters(
params,
applicationProperties.pagination.limitDefault,
Expand Down Expand Up @@ -124,7 +124,7 @@ class ContextSourceRegistrationHandler(
@PathVariable contextSourceRegistrationId: URI,
@RequestParam options: String?
): ResponseEntity<*> = either {
val includeSysAttrs = options == QueryParam.OptionValue.SYS_ATTRS.value
val includeSysAttrs = options == QueryParameter.SYS_ATTRS.key
val contexts = getContextFromLinkHeaderOrDefault(httpHeaders, applicationProperties.contexts.core).bind()
val mediaType = getApplicableMediaType(httpHeaders).bind()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.egm.stellio.shared.model.EntitySelector
import com.egm.stellio.shared.model.LinkedEntityQuery.Companion.parseLinkedEntityQueryParameters
import com.egm.stellio.shared.model.PaginationQuery.Companion.parsePaginationParameters
import com.egm.stellio.shared.model.parameter.GeoQuery.Companion.parseGeoQueryParameters
import com.egm.stellio.shared.model.parameter.QueryParam
import com.egm.stellio.shared.model.parameter.QueryParameter
import com.egm.stellio.shared.util.JsonLdUtils
import com.egm.stellio.shared.util.decode
import com.egm.stellio.shared.util.expandTypeSelection
Expand All @@ -28,18 +28,18 @@ fun composeEntitiesQueryFromGet(
requestParams: MultiValueMap<String, String>,
contexts: List<String>
): Either<APIException, EntitiesQueryFromGet> = either {
val ids = requestParams.getFirst(QueryParam.ID.key)?.split(",").orEmpty().toListOfUri().toSet()
val typeSelection = expandTypeSelection(requestParams.getFirst(QueryParam.TYPE.key), contexts)
val idPattern = validateIdPattern(requestParams.getFirst(QueryParam.ID_PATTERN.key)).bind()
val ids = requestParams.getFirst(QueryParameter.ID.key)?.split(",").orEmpty().toListOfUri().toSet()
val typeSelection = expandTypeSelection(requestParams.getFirst(QueryParameter.TYPE.key), contexts)
val idPattern = validateIdPattern(requestParams.getFirst(QueryParameter.ID_PATTERN.key)).bind()

/**
* Decoding query parameters is not supported by default so a call to a decode function was added query
* with the right parameters values
*/
val q = requestParams.getFirst(QueryParam.Q.key)?.decode()
val scopeQ = requestParams.getFirst(QueryParam.SCOPEQ.key)
val attrs = parseAndExpandRequestParameter(requestParams.getFirst(QueryParam.ATTRS.key), contexts)
val datasetId = parseRequestParameter(requestParams.getFirst(QueryParam.DATASET_ID.key))
val q = requestParams.getFirst(QueryParameter.Q.key)?.decode()
val scopeQ = requestParams.getFirst(QueryParameter.SCOPEQ.key)
val attrs = parseAndExpandRequestParameter(requestParams.getFirst(QueryParameter.ATTRS.key), contexts)
val datasetId = parseRequestParameter(requestParams.getFirst(QueryParameter.DATASET_ID.key))
val paginationQuery = parsePaginationParameters(
requestParams,
defaultPagination.limitDefault,
Expand All @@ -48,9 +48,9 @@ fun composeEntitiesQueryFromGet(

val geoQuery = parseGeoQueryParameters(requestParams.toSingleValueMap(), contexts).bind()
val linkedEntityQuery = parseLinkedEntityQueryParameters(
requestParams.getFirst(QueryParam.JOIN.key),
requestParams.getFirst(QueryParam.JOIN_LEVEL.key),
requestParams.getFirst(QueryParam.CONTAINED_BY.key)
requestParams.getFirst(QueryParameter.JOIN.key),
requestParams.getFirst(QueryParameter.JOIN_LEVEL.key),
requestParams.getFirst(QueryParameter.CONTAINED_BY.key)
).bind()

EntitiesQueryFromGet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.egm.stellio.shared.model.NgsiLdDataRepresentation.Companion.parseRepr
import com.egm.stellio.shared.model.ResourceNotFoundException
import com.egm.stellio.shared.model.filterAttributes
import com.egm.stellio.shared.model.parameter.AllowedParameters
import com.egm.stellio.shared.model.parameter.QueryParam
import com.egm.stellio.shared.model.parameter.QueryParameter
import com.egm.stellio.shared.model.toFinalRepresentation
import com.egm.stellio.shared.model.toNgsiLdEntity
import com.egm.stellio.shared.util.GEO_JSON_CONTENT_TYPE
Expand Down Expand Up @@ -66,7 +66,6 @@ import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
import java.net.URI
import java.util.Optional

@RestController
@RequestMapping("/ngsi-ld/v1/entities")
Expand All @@ -85,7 +84,9 @@ class EntityHandler(
@PostMapping(consumes = [APPLICATION_JSON_VALUE, JSON_LD_CONTENT_TYPE])
suspend fun create(
@RequestHeader httpHeaders: HttpHeaders,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val (body, contexts) =
Expand All @@ -110,13 +111,23 @@ class EntityHandler(
suspend fun merge(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@RequestParam options: MultiValueMap<String, String>,
@RequestParam
@AllowedParameters(
implemented = [
QueryParameter.OPTIONS,
QueryParameter.TYPE,
QueryParameter.OBSERVED_AT,
QueryParameter.LANG
],
notImplemented = [QueryParameter.FORMAT, QueryParameter.LOCAL, QueryParameter.VIA]
)
options: MultiValueMap<String, String>,
@RequestBody requestBody: Mono<String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val (body, contexts) =
extractPayloadAndContexts(requestBody, httpHeaders, applicationProperties.contexts.core).bind()
val observedAt = options.getFirst(QueryParam.OptionValue.OBSERVED_AT.value)
val observedAt = options.getFirst(QueryParameter.OBSERVED_AT.key)
?.parseTimeParameter("'observedAt' parameter is not a valid date")
?.getOrElse { return@either BadRequestDataException(it).left().bind<ResponseEntity<*>>() }
val expandedAttributes = expandAttributes(body, contexts)
Expand Down Expand Up @@ -148,7 +159,9 @@ class EntityHandler(
suspend fun replace(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val (body, contexts) =
Expand Down Expand Up @@ -185,8 +198,38 @@ class EntityHandler(
@RequestHeader httpHeaders: HttpHeaders,
@RequestParam
@AllowedParameters(
implemented = [QueryParam.Q, QueryParam.TYPE, QueryParam.ID_PATTERN],
notImplemented = [QueryParam.ATTRS]
implemented = [
QueryParameter.OPTIONS,
QueryParameter.FORMAT,
QueryParameter.COUNT,
QueryParameter.OFFSET,
QueryParameter.LIMIT,
QueryParameter.ID,
QueryParameter.TYPE,
QueryParameter.ID_PATTERN,
QueryParameter.ATTRS,
QueryParameter.Q,
QueryParameter.GEOMETRY,
QueryParameter.GEOREL,
QueryParameter.COORDINATES,
QueryParameter.GEOPROPERTY,
QueryParameter.GEOMETRY_PROPERTY,
QueryParameter.LANG,
QueryParameter.SCOPEQ,
QueryParameter.CONTAINED_BY,
QueryParameter.JOIN,
QueryParameter.JOIN_LEVEL,
QueryParameter.DATASET_ID,
],
notImplemented = [
QueryParameter.PICK,
QueryParameter.OMIT,
QueryParameter.EXPAND_VALUES,
QueryParameter.CSF,
QueryParameter.ENTITY_MAP,
QueryParameter.LOCAL,
QueryParameter.VIA
]
)
params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
Expand Down Expand Up @@ -232,7 +275,28 @@ class EntityHandler(
suspend fun getByURI(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@RequestParam params: MultiValueMap<String, String>
@RequestParam
@AllowedParameters(
implemented = [
QueryParameter.OPTIONS,
QueryParameter.FORMAT,
QueryParameter.TYPE,
QueryParameter.ATTRS,
QueryParameter.GEOMETRY_PROPERTY,
QueryParameter.LANG,
QueryParameter.CONTAINED_BY,
QueryParameter.JOIN,
QueryParameter.JOIN_LEVEL,
QueryParameter.DATASET_ID,
],
notImplemented = [
QueryParameter.PICK,
QueryParameter.OMIT,
QueryParameter.ENTITY_MAP,
QueryParameter.LOCAL, QueryParameter.VIA
]
)
params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val mediaType = getApplicableMediaType(httpHeaders).bind()
val sub = getSubFromSecurityContext()
Expand Down Expand Up @@ -310,7 +374,9 @@ class EntityHandler(
*/
@DeleteMapping("/{entityId}")
suspend fun delete(
@PathVariable entityId: URI
@PathVariable entityId: URI,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()

Expand All @@ -330,11 +396,17 @@ class EntityHandler(
suspend fun appendEntityAttributes(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@RequestParam options: Optional<String>,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@RequestParam
@AllowedParameters(
implemented = [QueryParameter.OPTIONS], // todo type no implemented?
notImplemented = [QueryParameter.TYPE, QueryParameter.LOCAL, QueryParameter.VIA]
)
params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val options = params.getFirst(QueryParameter.OPTIONS.key)
val sub = getSubFromSecurityContext()
val disallowOverwrite = options.map { it == QueryParam.OptionValue.NO_OVERWRITE.value }.orElse(false)
val disallowOverwrite = options?.let { it == QueryParameter.NO_OVERWRITE.key } ?: false

val (body, contexts) =
extractPayloadAndContexts(requestBody, httpHeaders, applicationProperties.contexts.core).bind()
Expand Down Expand Up @@ -371,7 +443,9 @@ class EntityHandler(
suspend fun updateEntityAttributes(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val (body, contexts) =
Expand Down Expand Up @@ -409,7 +483,9 @@ class EntityHandler(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@PathVariable attrId: String,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()

Expand Down Expand Up @@ -448,11 +524,15 @@ class EntityHandler(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@PathVariable attrId: String,
@AllowedParameters(
implemented = [QueryParameter.DELETE_ALL, QueryParameter.TYPE, QueryParameter.DATASET_ID],
notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA]
)
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val deleteAll = params.getFirst("deleteAll")?.toBoolean() ?: false
val datasetId = params.getFirst("datasetId")?.toUri()
val deleteAll = params.getFirst(QueryParameter.DELETE_ALL.key)?.toBoolean() ?: false
val datasetId = params.getFirst(QueryParameter.DATASET_ID.key)?.toUri()

val contexts = getContextFromLinkHeaderOrDefault(httpHeaders, applicationProperties.contexts.core).bind()
val expandedAttrId = expandJsonLdTerm(attrId, contexts)
Expand Down Expand Up @@ -483,7 +563,9 @@ class EntityHandler(
@RequestHeader httpHeaders: HttpHeaders,
@PathVariable entityId: URI,
@PathVariable attrId: String,
@RequestBody requestBody: Mono<String>
@RequestBody requestBody: Mono<String>,
@AllowedParameters(implemented = [], notImplemented = [QueryParameter.LOCAL, QueryParameter.VIA])
@RequestParam params: MultiValueMap<String, String>
): ResponseEntity<*> = either {
val sub = getSubFromSecurityContext()
val (body, contexts) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.egm.stellio.shared.model.BadRequestDataException
import com.egm.stellio.shared.model.LdContextNotAvailableException
import com.egm.stellio.shared.model.NgsiLdDataRepresentation.Companion.parseRepresentations
import com.egm.stellio.shared.model.filterAttributes
import com.egm.stellio.shared.model.parameter.QueryParam
import com.egm.stellio.shared.model.parameter.QueryParameter
import com.egm.stellio.shared.model.toFinalRepresentation
import com.egm.stellio.shared.model.toNgsiLdEntity
import com.egm.stellio.shared.util.GEO_JSON_CONTENT_TYPE
Expand Down Expand Up @@ -144,7 +144,7 @@ class EntityOperationHandler(

val (parsedEntities, unparsableEntities) = prepareEntitiesFromRequestBody(requestBody, httpHeaders).bind()

val disallowOverwrite = options.map { it == QueryParam.OptionValue.NO_OVERWRITE.value }.orElse(false)
val disallowOverwrite = options.map { it == QueryParameter.NO_OVERWRITE.key }.orElse(false)

val batchOperationResult = BatchOperationResult().apply {
addEntitiesToErrors(unparsableEntities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import com.egm.stellio.shared.model.ExpandedAttributeInstances
import com.egm.stellio.shared.model.NgsiLdEntity
import com.egm.stellio.shared.model.OperationNotSupportedException
import com.egm.stellio.shared.model.getScopes
import com.egm.stellio.shared.model.parameter.TemporalQueryParameter.Companion.WHOLE_TIME_RANGE_DURATION
import com.egm.stellio.shared.util.INCONSISTENT_VALUES_IN_AGGREGATION_MESSAGE
import com.egm.stellio.shared.util.JsonLdUtils
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SCOPE_PROPERTY
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.egm.stellio.shared.util.Sub
import com.egm.stellio.shared.util.WHOLE_TIME_RANGE_DURATION
import io.r2dbc.postgresql.codec.Json
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.r2dbc.core.bind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import com.egm.stellio.shared.model.ExpandedAttributeInstances
import com.egm.stellio.shared.model.ExpandedTerm
import com.egm.stellio.shared.model.OperationNotSupportedException
import com.egm.stellio.shared.model.ResourceNotFoundException
import com.egm.stellio.shared.model.parameter.TemporalQueryParameter.Companion.WHOLE_TIME_RANGE_DURATION
import com.egm.stellio.shared.model.toNgsiLdAttribute
import com.egm.stellio.shared.util.INCONSISTENT_VALUES_IN_AGGREGATION_MESSAGE
import com.egm.stellio.shared.util.WHOLE_TIME_RANGE_DURATION
import com.egm.stellio.shared.util.attributeOrInstanceNotFoundMessage
import com.egm.stellio.shared.util.ngsiLdDateTime
import org.springframework.r2dbc.core.DatabaseClient
Expand Down
Loading

0 comments on commit 9803a58

Please sign in to comment.