Skip to content

Commit 52f063b

Browse files
committed
validate incoming JSON
1 parent 033a506 commit 52f063b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

grails-app/resources/jsonblob/JsonBlobCollectionResource.groovy

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package jsonblob
22

3+
import grails.converters.JSON
34
import org.bson.types.ObjectId
5+
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
46
import org.grails.jaxrs.provider.DomainObjectNotFoundException
57

68
import javax.ws.rs.*
@@ -19,12 +21,12 @@ class JsonBlobCollectionResource {
1921
@POST
2022
@Path('/jsonBlob')
2123
Response create(String json) {
22-
def newBlob = jsonBlobResourceService.create(json)
23-
def objectId = newBlob["_id"]
24-
if (objectId) {
25-
URI uri = UriBuilder.fromPath(objectId.toString()).build()
24+
try {
25+
JSON.parse(json)
26+
def newBlob = jsonBlobResourceService.create(json)
27+
URI uri = UriBuilder.fromPath(newBlob["_id"].toString()).build()
2628
Response.created(uri).entity(jsonService.writeValueAsString(newBlob?.blob)).build()
27-
} else {
29+
} catch (ConverterException ce) {
2830
Response.serverError().build()
2931
}
3032
}

grails-app/resources/jsonblob/JsonBlobResource.groovy

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package jsonblob
22

3+
import grails.converters.JSON
4+
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
5+
36
import javax.ws.rs.*
47
import javax.ws.rs.core.Response
58

@@ -19,8 +22,13 @@ class JsonBlobResource {
1922

2023
@PUT
2124
Response update(String json) {
22-
def updatedBlob = jsonBlobResourceService.update(id, json)
23-
Response.ok(jsonService.writeValueAsString(updatedBlob?.blob)).build()
25+
try {
26+
JSON.parse(json)
27+
def updatedBlob = jsonBlobResourceService.update(id, json)
28+
Response.ok(jsonService.writeValueAsString(updatedBlob?.blob)).build()
29+
} catch (ConverterException ce) {
30+
Response.serverError().build()
31+
}
2432
}
2533

2634
// @DELETE

0 commit comments

Comments
 (0)