diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2020-04-22 12:44:11 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-04-22 12:44:11 +0000 |
commit | c9743fbf57cafeb4bf02b6ba09aac5969ea8ef67 (patch) | |
tree | de338412c9054df2d0c5f964137d4bca85436eff /pnfsimulator/src/main | |
parent | 6ac1ea35981e6b99e5ef1755a76e0f5f722473ff (diff) | |
parent | 655973da2561ddc74f76d693c48c142dcb24f15d (diff) |
Merge "Fix getting template by name from rest endpoint"
Diffstat (limited to 'pnfsimulator/src/main')
-rw-r--r-- | pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java index 7eaa9ff..8f8fee7 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Optional; import javax.validation.Valid; +import com.google.gson.Gson; import org.onap.pnfsimulator.db.Storage; import org.onap.pnfsimulator.rest.model.TemplateRequest; import org.onap.pnfsimulator.rest.model.SearchExp; @@ -63,14 +64,23 @@ public class TemplateController { } @GetMapping("get/{templateName}") - public ResponseEntity<?> get(@PathVariable String templateName) { + public ResponseEntity<String> get(@PathVariable String templateName) { Optional<Template> template = service.get(templateName); - if (!template.isPresent()) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.TEXT_PLAIN); - return new ResponseEntity<>(TEMPLATE_NOT_FOUND_MSG, headers, HttpStatus.NOT_FOUND); - } - return new ResponseEntity<>(template, HttpStatus.OK); + return template + .map(this::createTemplateResponse) + .orElse(this.createTemplateNotFoundResponse()); + } + + private ResponseEntity<String> createTemplateResponse(Template template) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + return new ResponseEntity<>(new Gson().toJson(template),headers, HttpStatus.OK); + } + + private ResponseEntity<String> createTemplateNotFoundResponse() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_PLAIN); + return new ResponseEntity<>(TEMPLATE_NOT_FOUND_MSG, headers, HttpStatus.NOT_FOUND); } @PostMapping("upload") |