Skip to content

Commit

Permalink
Merge pull request #82 from provenance-io/improve-search
Browse files Browse the repository at this point in the history
Improve search and allow for disabling object fetching
  • Loading branch information
piercetrey-figure authored Aug 9, 2021
2 parents 48bec5a + 679827d commit 9e0bd45
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ class ObjectStoreProperties {
@ConfigurationProperties(prefix = "service")
class ServiceProperties {
@NotNull lateinit var name: String
var objectFetchEnabled: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@ import io.p8e.util.orThrowNotFound
import io.p8e.util.toJavaPublicKey
import io.provenance.p8e.shared.domain.AffiliateRecord
import io.provenance.p8e.shared.service.AffiliateService
import io.provenance.p8e.webservice.config.ErrorMessage
import io.provenance.p8e.webservice.config.ServiceProperties
import org.jetbrains.exposed.sql.transactions.transaction
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*

@CrossOrigin(origins = ["http://localhost:3000"], allowCredentials = "true")
@RestController
@RequestMapping("object")
open class ObjectController(private val affiliateService: AffiliateService) {
open class ObjectController(private val affiliateService: AffiliateService, private val serviceProperties: ServiceProperties) {
@GetMapping()
fun getJson(
@RequestParam("hash") hash: String,
@RequestParam("className") className: String,
@RequestParam("contractSpecHash") contractSpecHash: String,
@RequestParam("publicKey") publicKey: String
): Any {
if (!serviceProperties.objectFetchEnabled) {
return ResponseEntity(ErrorMessage(listOf("Object fetching disabled")), HttpStatus.FORBIDDEN)
}

val affiliate = transaction { affiliateService.get(publicKey.toJavaPublicKey()) }

requireNotNull(affiliate) { "Affiliate not found with public key $publicKey" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ provenance.keystone.url=${PROVENANCE_KEYSTONE_URL}
# Smart Key
smartkey.apikey=${SMARTKEY_API_KEY}
smartkey.group-id=${SMARTKEY_GROUP_ID}

# Object Fetching
service.object-fetch-enabled=${SERVICE_OBJECT_FETCH_ENABLED:false}
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ objectstore.key=
# Smart Key
smartkey.apikey=${SMARTKEY_API_KEY}
smartkey.group-id=${SMARTKEY_GROUP_ID}

# Object Fetching
service.object-fetch-enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ provenance.keystone.url=https://test.provenance.io/keystone/key/registration/sec
# Smart Key
smartkey.apikey=${SMARTKEY_API_KEY}
smartkey.group-id=${SMARTKEY_GROUP_ID}

# Object Fetching
service.object-fetch-enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.isNull
import org.jetbrains.exposed.sql.transactions.TransactionManager
import java.lang.IllegalStateException
import java.security.PublicKey
import java.sql.ResultSet
import java.time.OffsetDateTime
Expand Down Expand Up @@ -166,14 +167,31 @@ open class EnvelopeEntityClass : UUIDEntityClass<EnvelopeRecord>(
.plus(EnvelopeTable.actualScopeUuid)
var query = EnvelopeTable.slice(columns)
if (q != null) {
val searchableUuidColumns = listOf("scope_uuid", "uuid", "execution_uuid", "group_uuid")
val segments = q.split(';').map { it.split(',') }
.filter {
it.size == 2
&& searchableUuidColumns.any { column -> column == it[0] }
&& try {
UUID.fromString(it[1])
true
} catch(e: Exception) { false }
}
.map { it[0] to UUID.fromString(it[1]) }

try {
val uuid = UUID.fromString(q)
query = EnvelopeTable
.join(ScopeTable, JoinType.INNER)
.slice(columns)
expressions.add((EnvelopeTable.scope eq uuid) or (ScopeTable.scopeUuid eq uuid) or (EnvelopeTable.id eq uuid) or (EnvelopeTable.executionUuid eq uuid) or (EnvelopeTable.groupUuid eq uuid))
} catch (e: IllegalArgumentException) {
// invalid uuid

expressions.addAll(segments.map { (column, uuid) -> when (column) {
"scope_uuid" -> ScopeTable.scopeUuid eq uuid
"uuid" -> EnvelopeTable.id eq uuid
"execution_uuid" -> EnvelopeTable.executionUuid eq uuid
"group_uuid" -> EnvelopeTable.groupUuid eq uuid
else -> throw IllegalStateException("Invalid column for searching, should be impossible to get here")
} })
} catch (e: IllegalStateException) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ open class ScopeEntityClass : UUIDEntityClass<ScopeRecord>(
if (q != null) {
try {
val uuid = UUID.fromString(q.trim())
expressions.add((ScopeTable.id eq uuid) or (ScopeTable.scopeUuid eq uuid))
expressions.add(ScopeTable.scopeUuid eq uuid)
} catch (e: IllegalArgumentException) {
// invalid uuid
}
Expand Down

0 comments on commit 9e0bd45

Please sign in to comment.