File tree Expand file tree Collapse file tree 2 files changed +7
-2
lines changed
kotlin/de/jakkoble/plugins Expand file tree Collapse file tree 2 files changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -19,11 +19,13 @@ fun Application.configureHTTP() {
1919
2020 // Get-Request with the companion object parameter of Fruit Class
2121 get(Fruit .path) {
22+ if (isInvalidKey(call.parameters[" key" ].toString())) return @get
2223 call.respond(Fruits .list) // Respond with the Fruit List (because of installed ContentNegotiation Plugin in JSON)
2324 }
2425
2526 // Post-Request with the companion object parameter of Fruit Class
2627 post(Fruit .path) {
28+ if (isInvalidKey(call.parameters[" key" ].toString())) return @post
2729 val fruit = call.receive<Fruit >() // Receive Content of Request as Fruit
2830 if (! Fruits .list.none { it.name == fruit.name }) error(" Name already in use" ) // If name is already in the List throw IllegalStateException
2931 Fruits .list.add(fruit) // Add Fruit to Fruit List
@@ -32,9 +34,12 @@ fun Application.configureHTTP() {
3234
3335 // Delete-Request with the companion object parameter of Fruit Class
3436 delete(Fruit .path) {
37+ if (isInvalidKey(call.parameters[" key" ].toString())) return @delete
3538 val name = call.receive<Fruit >().name // Receive Content of Request as Fruit
3639 Fruits .list.removeIf { it.name == name } // Removes the Element with the name of the Request item
3740 call.respond(HttpStatusCode .OK ) // Respond Status Code for clean Communication
3841 }
3942 }
4043}
44+ // Methode to check if Key is valid
45+ fun isInvalidKey (key : String ) = key != " Usw98ayVeR"
Original file line number Diff line number Diff line change @@ -67,15 +67,15 @@ <h1>Hello Developer!</h1>
6767 < button id ="delete " onclick ="sendRequest('DELETE') "> Remove a Cherry</ button >
6868 </ div >
6969 < br >
70- < a href ="http://localhost:8080/fruits " target ="_blank "> Link to the Fruit List</ a >
70+ < a href ="http://localhost:8080/fruits?key=Usw98ayVeR " target ="_blank "> Link to the Fruit List</ a >
7171 < br > < br >
7272 < a style ="color: white; text-decoration: underline " href ="https://jakkoble.de " target ="_blank "> by Jakkoble</ a >
7373 </ div >
7474 </ div >
7575< script >
7676 function sendRequest ( type ) {
7777 let request = new XMLHttpRequest ( )
78- request . open ( type , "http://localhost:8080/fruits" )
78+ request . open ( type , "http://localhost:8080/fruits?key=Usw98ayVeR " )
7979 request . setRequestHeader ( "Content-Type" , "application/json" )
8080 let data = `{
8181 "name": "Cherry",
You can’t perform that action at this time.
0 commit comments