Skip to content

Commit 76a13ac

Browse files
authored
fix: Update sample with best practices (GoogleCloudPlatform#5455)
1 parent 0cae60c commit 76a13ac

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

run/image-processing/src/main/java/com/example/cloudrun/PubSubController.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,42 @@
3232
@RestController
3333
public class PubSubController {
3434
@RequestMapping(value = "/", method = RequestMethod.POST)
35-
public ResponseEntity receiveMessage(@RequestBody Body body) {
35+
public ResponseEntity<String> receiveMessage(@RequestBody Body body) {
3636
// Get PubSub message from request body.
3737
Body.Message message = body.getMessage();
3838
if (message == null) {
3939
String msg = "Bad Request: invalid Pub/Sub message format";
4040
System.out.println(msg);
41-
return new ResponseEntity(msg, HttpStatus.BAD_REQUEST);
41+
return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
4242
}
4343

4444
// Decode the Pub/Sub message.
4545
String pubSubMessage = message.getData();
4646
JsonObject data;
4747
try {
4848
String decodedMessage = new String(Base64.getDecoder().decode(pubSubMessage));
49-
data = new JsonParser().parse(decodedMessage).getAsJsonObject();
49+
data = JsonParser.parseString(decodedMessage).getAsJsonObject();
5050
} catch (Exception e) {
5151
String msg = "Error: Invalid Pub/Sub message: data property is not valid base64 encoded JSON";
5252
System.out.println(msg);
53-
return new ResponseEntity(msg, HttpStatus.BAD_REQUEST);
53+
return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
5454
}
5555

5656
// Validate the message is a Cloud Storage event.
5757
if (data.get("name") == null || data.get("bucket") == null) {
5858
String msg = "Error: Invalid Cloud Storage notification: expected name and bucket properties";
5959
System.out.println(msg);
60-
return new ResponseEntity(msg, HttpStatus.BAD_REQUEST);
60+
return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
6161
}
6262

6363
try {
6464
ImageMagick.blurOffensiveImages(data);
6565
} catch (Exception e) {
6666
String msg = String.format("Error: Blurring image: %s", e.getMessage());
6767
System.out.println(msg);
68-
return new ResponseEntity(msg, HttpStatus.INTERNAL_SERVER_ERROR);
68+
return new ResponseEntity<>(msg, HttpStatus.INTERNAL_SERVER_ERROR);
6969
}
70-
return new ResponseEntity(HttpStatus.OK);
70+
return new ResponseEntity<>(HttpStatus.OK);
7171
}
7272
}
7373
// [END run_imageproc_controller]

0 commit comments

Comments
 (0)