summaryrefslogtreecommitdiffstats
path: root/pnfsimulator/src/main
diff options
context:
space:
mode:
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-04-22 12:06:23 +0200
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-04-22 12:06:23 +0200
commit655973da2561ddc74f76d693c48c142dcb24f15d (patch)
tree593e61ea39be4e061c5cd1ca4bf755c5f5ec8e4f /pnfsimulator/src/main
parent6e6db5b854d8b531c87260e89a96775a26c672ad (diff)
Fix getting template by name from rest endpoint
Issue-ID: INT-1533 Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> Change-Id: If7839bb91228d0e9cb43afe38c48dd11f52cb871
Diffstat (limited to 'pnfsimulator/src/main')
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/TemplateController.java24
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")