|
32 | 32 | @RestController |
33 | 33 | public class PubSubController { |
34 | 34 | @RequestMapping(value = "/", method = RequestMethod.POST) |
35 | | - public ResponseEntity receiveMessage(@RequestBody Body body) { |
| 35 | + public ResponseEntity<String> receiveMessage(@RequestBody Body body) { |
36 | 36 | // Get PubSub message from request body. |
37 | 37 | Body.Message message = body.getMessage(); |
38 | 38 | if (message == null) { |
39 | 39 | String msg = "Bad Request: invalid Pub/Sub message format"; |
40 | 40 | System.out.println(msg); |
41 | | - return new ResponseEntity(msg, HttpStatus.BAD_REQUEST); |
| 41 | + return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST); |
42 | 42 | } |
43 | 43 |
|
44 | 44 | // Decode the Pub/Sub message. |
45 | 45 | String pubSubMessage = message.getData(); |
46 | 46 | JsonObject data; |
47 | 47 | try { |
48 | 48 | String decodedMessage = new String(Base64.getDecoder().decode(pubSubMessage)); |
49 | | - data = new JsonParser().parse(decodedMessage).getAsJsonObject(); |
| 49 | + data = JsonParser.parseString(decodedMessage).getAsJsonObject(); |
50 | 50 | } catch (Exception e) { |
51 | 51 | String msg = "Error: Invalid Pub/Sub message: data property is not valid base64 encoded JSON"; |
52 | 52 | System.out.println(msg); |
53 | | - return new ResponseEntity(msg, HttpStatus.BAD_REQUEST); |
| 53 | + return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST); |
54 | 54 | } |
55 | 55 |
|
56 | 56 | // Validate the message is a Cloud Storage event. |
57 | 57 | if (data.get("name") == null || data.get("bucket") == null) { |
58 | 58 | String msg = "Error: Invalid Cloud Storage notification: expected name and bucket properties"; |
59 | 59 | System.out.println(msg); |
60 | | - return new ResponseEntity(msg, HttpStatus.BAD_REQUEST); |
| 60 | + return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST); |
61 | 61 | } |
62 | 62 |
|
63 | 63 | try { |
64 | 64 | ImageMagick.blurOffensiveImages(data); |
65 | 65 | } catch (Exception e) { |
66 | 66 | String msg = String.format("Error: Blurring image: %s", e.getMessage()); |
67 | 67 | System.out.println(msg); |
68 | | - return new ResponseEntity(msg, HttpStatus.INTERNAL_SERVER_ERROR); |
| 68 | + return new ResponseEntity<>(msg, HttpStatus.INTERNAL_SERVER_ERROR); |
69 | 69 | } |
70 | | - return new ResponseEntity(HttpStatus.OK); |
| 70 | + return new ResponseEntity<>(HttpStatus.OK); |
71 | 71 | } |
72 | 72 | } |
73 | 73 | // [END run_imageproc_controller] |
|
0 commit comments