diff options
author | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2024-11-02 00:16:58 +0530 |
---|---|---|
committer | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2024-11-05 08:49:35 +0000 |
commit | 14d6a9b990005965307bbab66b0ffe15327a8758 (patch) | |
tree | cbda47e225ce83cf5d86cadc26dc0b9fb1b48d79 /cps-rest/src/main/java/org | |
parent | d1774233355349a6176639cbeb141c883a1781de (diff) |
XML content support on get a node
Here to bring Support for XML Response Entity In GET A NODE
- Made changes in components.yml to support contentType as
application/xml
- Add ContentTypeInheadr in cpsDataV2.yml to support application/xml
- Add contentTypeInHeader parameter to accept xml in
DataRestController.java
- Implemented Logic to convert DataMaps To XML Data
- written testcase for above changes made
Issue-ID: CPS-2280
Change-Id: Ibe7ffb66ccbb03703626132c6d5c2eade0e7ab4b
Signed-off-by: Rudrangi Anupriya <ra00745022@techmahindra.com>
Diffstat (limited to 'cps-rest/src/main/java/org')
-rwxr-xr-x | cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java index 7390afcf98..6d22581845 100755 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/DataRestController.java @@ -48,8 +48,8 @@ import org.onap.cps.utils.ContentType; import org.onap.cps.utils.DataMapUtils; import org.onap.cps.utils.JsonObjectMapper; import org.onap.cps.utils.PrefixResolver; +import org.onap.cps.utils.XmlFileUtils; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -75,7 +75,7 @@ public class DataRestController implements CpsDataApi { final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, final Boolean dryRunEnabled, final String observedTimestamp) { - final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); + final ContentType contentType = ContentType.fromString(contentTypeInHeader); if (Boolean.TRUE.equals(dryRunEnabled)) { cpsDataService.validateData(dataspaceName, anchorName, parentNodeXpath, nodeData, contentType); return ResponseEntity.ok().build(); @@ -105,7 +105,7 @@ public class DataRestController implements CpsDataApi { final String anchorName, final String parentNodeXpath, final String contentTypeInHeader, final String nodeData, final String observedTimestamp) { - final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); + final ContentType contentType = ContentType.fromString(contentTypeInHeader); cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, nodeData, toOffsetDateTime(observedTimestamp), contentType); return new ResponseEntity<>(HttpStatus.CREATED); @@ -129,8 +129,9 @@ public class DataRestController implements CpsDataApi { @Timed(value = "cps.data.controller.datanode.get.v2", description = "Time taken to get data node") public ResponseEntity<Object> getNodeByDataspaceAndAnchorV2(final String dataspaceName, final String anchorName, - final String xpath, + final String contentTypeInHeader, final String xpath, final String fetchDescendantsOptionAsString) { + final ContentType contentType = ContentType.fromString(contentTypeInHeader); final FetchDescendantsOption fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString); final Collection<DataNode> dataNodes = cpsDataService.getDataNodes(dataspaceName, anchorName, xpath, @@ -142,7 +143,7 @@ public class DataRestController implements CpsDataApi { final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix); dataMaps.add(dataMap); } - return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataMaps), HttpStatus.OK); + return buildResponseEntity(dataMaps, contentType); } @Override @@ -150,7 +151,7 @@ public class DataRestController implements CpsDataApi { final String anchorName, final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, final String observedTimestamp) { - final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); + final ContentType contentType = ContentType.fromString(contentTypeInHeader); cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, nodeData, toOffsetDateTime(observedTimestamp), contentType); return new ResponseEntity<>(HttpStatus.OK); @@ -161,7 +162,7 @@ public class DataRestController implements CpsDataApi { final String anchorName, final String contentTypeInHeader, final String nodeData, final String parentNodeXpath, final String observedTimestamp) { - final ContentType contentType = getContentTypeFromHeader(contentTypeInHeader); + final ContentType contentType = ContentType.fromString(contentTypeInHeader); cpsDataService.updateDataNodeAndDescendants(dataspaceName, anchorName, parentNodeXpath, nodeData, toOffsetDateTime(observedTimestamp), contentType); return new ResponseEntity<>(HttpStatus.OK); @@ -219,12 +220,19 @@ public class DataRestController implements CpsDataApi { final List<DeltaReport> deltaBetweenAnchors = cpsDataService.getDeltaByDataspaceAndAnchors(dataspaceName, sourceAnchorName, - targetAnchorName, xpath, fetchDescendantsOption); + targetAnchorName, xpath, fetchDescendantsOption); return new ResponseEntity<>(jsonObjectMapper.asJsonString(deltaBetweenAnchors), HttpStatus.OK); } - private static ContentType getContentTypeFromHeader(final String contentTypeInHeader) { - return contentTypeInHeader.contains(MediaType.APPLICATION_XML_VALUE) ? ContentType.XML : ContentType.JSON; + ResponseEntity<Object> buildResponseEntity(final List<Map<String, Object>> dataMaps, + final ContentType contentType) { + final String responseData; + if (contentType == ContentType.XML) { + responseData = XmlFileUtils.convertDataMapsToXml(dataMaps); + } else { + responseData = jsonObjectMapper.asJsonString(dataMaps); + } + return new ResponseEntity<>(responseData, HttpStatus.OK); } private static boolean isRootXpath(final String xpath) { |