Skip to content

Commit cdc3cfb

Browse files
committed
288
1 parent 659805b commit cdc3cfb

3 files changed

Lines changed: 63 additions & 72 deletions

File tree

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/Metadata.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.github.mgramin.sqlboot.model.resourcetype
22

33
import com.fasterxml.jackson.annotation.JsonAutoDetect
4+
import com.google.gson.Gson
5+
import com.google.gson.JsonParser
6+
import com.google.gson.reflect.TypeToken
47
import org.json.JSONException
58
import org.json.JSONObject
69
import java.util.HashMap
@@ -21,9 +24,10 @@ data class Metadata(
2124
init {
2225
this.properties = HashMap()
2326
try {
24-
this.properties.putAll(JSONObject(description).toMap())
27+
val map: Map<String, Any> = Gson().fromJson(description, object : TypeToken<Map<String, Any>>() {}.type)
28+
this.properties.putAll(map)
2529
this.properties["key"] = name.replace("@", "")
26-
} catch (ignored: JSONException) {
30+
} catch (ignored: Exception) {
2731
this.properties.clear()
2832
val prop = HashMap<String, Any>()
2933
prop["key"] = name.replace("@", "")

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.github.mgramin.sqlboot.model.connection.DbConnectionList
2929
import com.github.mgramin.sqlboot.model.resourcetype.Metadata
3030
import com.github.mgramin.sqlboot.model.resourcetype.ResourceType
3131
import com.github.mgramin.sqlboot.model.resourcetype.impl.composite.FsResourceTypes
32+
import com.github.mgramin.sqlboot.model.uri.Uri
3233
import com.github.mgramin.sqlboot.model.uri.impl.DbUri
3334
import com.github.mgramin.sqlboot.model.uri.impl.FakeUri
3435
import com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper
@@ -67,77 +68,27 @@ class ApiController {
6768
}
6869

6970

70-
@RequestMapping(value = ["/api/{connectionName}/headers/**"], method = [GET, POST])
71+
@RequestMapping(value = ["/api/{connectionName}/**"], method = [GET, POST])
7172
fun getResourcesHeadersJson(
7273
request: HttpServletRequest,
7374
@PathVariable connectionName: String
7475
): ResponseEntity<List<Map<String, Any>>> {
75-
val filter = request.servletPath
76+
val uriString = request.servletPath
7677
.split("/")
78+
.asSequence()
7779
.filter { it.isNotEmpty() }
7880
.filter { it != "api" }
79-
.filterIndexed{ index, _ -> index != 0 && index != 1 }
81+
.filter { it != "headers" }
82+
.filterIndexed { index, _ -> index != 0 }
8083
.joinToString(separator = "/") { it }
81-
return getListResponseEntityHeaders(request, connectionName, filter)
84+
val uri: Uri = SqlPlaceholdersWrapper(DbUri(parseUri(uriString, request)))
85+
return getListResponseEntityHeaders(uri, connectionName)
8286
}
8387

84-
85-
86-
@RequestMapping(value = ["/api/{connectionName}/meta/{type}"], method = [GET, POST])
87-
fun getResourceMetadata(
88-
request: HttpServletRequest,
89-
@PathVariable connectionName: String,
90-
@PathVariable type: String
91-
): ResponseEntity<List<Metadata>> {
92-
val uri = SqlPlaceholdersWrapper(DbUri(parseUri(type, request)))
93-
return responseEntity(connectionName, uri)
94-
}
95-
96-
97-
@RequestMapping(value = ["/api/{connectionName}/meta/{type}/{path:.+}"], method = [GET, POST])
98-
fun getResourceMetadata2(
99-
request: HttpServletRequest,
100-
@PathVariable connectionName: String,
101-
@PathVariable type: String,
102-
@PathVariable path: String
103-
): ResponseEntity<List<Metadata>> {
104-
val uri = SqlPlaceholdersWrapper(DbUri(parseUri(path, request)))
105-
return responseEntity(connectionName, uri)
106-
}
107-
108-
109-
@RequestMapping(value = ["/api/{connectionName}/meta/{type}/{path:.+}/{action}"], method = [GET, POST])
110-
fun getResourceMetadata3(
111-
request: HttpServletRequest,
112-
@PathVariable connectionName: String,
113-
@PathVariable type: String,
114-
@PathVariable path: String,
115-
@PathVariable action: String
116-
): ResponseEntity<List<Metadata>> {
117-
val uri = SqlPlaceholdersWrapper(DbUri(parseUri("$type/$path/$action", request)))
118-
return responseEntity(connectionName, uri)
119-
}
120-
121-
private fun responseEntity(connectionName: String, uri: SqlPlaceholdersWrapper): ResponseEntity<List<Metadata>> {
122-
val fsResourceTypes = FsResourceTypes(
123-
listOf(dbConnectionList.getConnectionByName(connectionName)), uri)
124-
val resourceType = fsResourceTypes
125-
.resourceTypes()
126-
.stream()
127-
.filter { v -> v.name().equals(uri.type(), ignoreCase = true) }
128-
.findAny()
129-
.orElse(null) ?: return ResponseEntity(ArrayList(), HttpStatus.NO_CONTENT)
130-
return ResponseEntity(resourceType.metaData(uri), HttpStatus.OK)
131-
}
132-
133-
134-
13588
private fun getListResponseEntityHeaders(
136-
request: HttpServletRequest,
137-
connectionName: String,
138-
path: String
89+
uri: Uri,
90+
connectionName: String
13991
): ResponseEntity<List<Map<String, Any>>> {
140-
val uri = SqlPlaceholdersWrapper(DbUri(parseUri(path, request)))
14192
val connections = dbConnectionList.getConnectionsByMask(connectionName)
14293
try {
14394
val headers = FsResourceTypes(connections, uri)
@@ -159,6 +110,36 @@ class ApiController {
159110
}
160111

161112

113+
@RequestMapping(value = ["/api/meta/{connectionName}/**"], method = [GET, POST])
114+
fun getResourceMetadata(
115+
request: HttpServletRequest,
116+
@PathVariable connectionName: String
117+
): ResponseEntity<List<Metadata>> {
118+
val uriString = request.servletPath
119+
.split("/")
120+
.filter { it.isNotEmpty() }
121+
.filter { it != "api" }
122+
.filter { it != "meta" }
123+
.filterIndexed { index, _ -> index != 0 }
124+
.joinToString(separator = "/") { it }
125+
val uri = SqlPlaceholdersWrapper(DbUri(parseUri(uriString, request)))
126+
return responseEntity(connectionName, uri)
127+
}
128+
129+
130+
private fun responseEntity(connectionName: String, uri: Uri): ResponseEntity<List<Metadata>> {
131+
val fsResourceTypes = FsResourceTypes(
132+
listOf(dbConnectionList.getConnectionByName(connectionName)), uri)
133+
val resourceType = fsResourceTypes
134+
.resourceTypes()
135+
.stream()
136+
.filter { v -> v.name().equals(uri.type(), ignoreCase = true) }
137+
.findAny()
138+
.orElse(null) ?: return ResponseEntity(ArrayList(), HttpStatus.NO_CONTENT)
139+
return ResponseEntity(resourceType.metaData(uri), HttpStatus.OK)
140+
}
141+
142+
162143
private fun parseUri(path: String, request: HttpServletRequest): String {
163144
return if (request.queryString == null || request.queryString.isEmpty()) {
164145
path

src/test/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiControllerITCase.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,41 @@ class ApiControllerITCase {
5151
private val client: TestRestTemplate? = null
5252

5353

54-
/*@ParameterizedTest
54+
@ParameterizedTest
5555
@CsvSource(
56+
"204#/api/h2/table/foo",
57+
"200#/api/h2/table/BOOKINGS",
58+
"200#/api/h2/table",
5659
"200#/api/h2/table/BOOKINGS.AIRCRAFTS",
5760
"204#/api/h2/table/not_exist_schema",
5861
"200#/api/h2/table",
5962
"404#/api/h2/not_exist_type",
6063
"200#/api/h2/table/BOOKINGS.AIRPORTS?select=remarks",
61-
delimiter = '#'
62-
)
63-
fun test(code : Int, uri : String) {
64+
delimiter = '#')
65+
fun testHeaders(code: Int, uri: String) {
6466
val headers = HttpHeaders()
6567
headers.contentType = MediaType.TEXT_PLAIN
6668
val result = client!!.exchange(uri, HttpMethod.GET, HttpEntity<Any>(headers), String::class.java)
6769
assertEquals(code, result.statusCodeValue)
68-
}*/
70+
}
6971

7072

7173
@ParameterizedTest
7274
@CsvSource(
73-
"204#/api/h2/headers/table/foo",
74-
"200#/api/h2/headers/table/BOOKINGS",
75-
"200#/api/h2/headers/table",
75+
"200#6#/api/meta/h2/table",
76+
"200#6#/api/meta/h2/table/foo",
77+
"200#6#/api/meta/h2/table/foo.bar",
78+
"200#8#/api/meta/h2/column",
79+
"200#8#/api/meta/h2/column/foo",
80+
"200#8#/api/meta/h2/column/foo.bar",
7681
delimiter = '#'
7782
)
78-
fun testHeaders(code : Int, uri : String) {
83+
fun testHeadersMeta(code: Int, fieldCount: Int, uri: String) {
7984
val headers = HttpHeaders()
80-
headers.contentType = MediaType.TEXT_PLAIN
81-
val result = client!!.exchange(uri, HttpMethod.GET, HttpEntity<Any>(headers), String::class.java)
85+
headers.contentType = MediaType.APPLICATION_JSON
86+
val result = client!!.exchange(uri, HttpMethod.GET, HttpEntity<Any>(headers), List::class.java)
8287
assertEquals(code, result.statusCodeValue)
88+
assertEquals(fieldCount, result.body.count())
8389
}
8490

8591
}

0 commit comments

Comments
 (0)