82d9ebea9c334c76844073976c0ae163beef98f7
[externalapi/nbi.git] / src / main / java / org / onap / nbi / commons / ResourceManagement.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.commons;
17
18 import java.net.URI;
19 import java.util.List;
20 import java.util.Set;
21 import org.springframework.http.HttpHeaders;
22 import org.springframework.http.HttpStatus;
23 import org.springframework.http.ResponseEntity;
24 import org.springframework.web.context.request.RequestContextHolder;
25 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
26
27 public class ResourceManagement {
28
29     /**
30      * Build default 201 filtered response for resource
31      *
32      * @param resource
33      * @param jsonRepresentation
34      * @return
35      */
36     protected ResponseEntity<Object> createResponse(final Resource resource,
37             final JsonRepresentation jsonRepresentation) {
38
39         URI location = null;
40         if (RequestContextHolder.getRequestAttributes() != null) {
41             location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.getId())
42                     .toUri();
43         } else {
44             location = URI.create("/");
45         }
46
47
48         // Get entity representation
49         final Object entity = this.getEntity(resource, jsonRepresentation);
50
51         return ResponseEntity.created(location).body(entity);
52
53     }
54
55     /**
56      * Build default 200 filtered response for resource
57      *
58      * @param resource
59      * @param jsonRepresentation
60      * @return
61      */
62     protected ResponseEntity<Object> getResponse(final Object resource, final JsonRepresentation jsonRepresentation) {
63
64         // Get entity representation
65         final Object entity = this.getEntity(resource, jsonRepresentation);
66
67         return ResponseEntity.ok(entity);
68
69     }
70
71
72
73     /**
74      * Build default 206 filtered partial response for resource
75      *
76      * @param resource
77      * @param jsonRepresentation
78      * @return
79      */
80     protected ResponseEntity<Object> getPartialResponse(final Object resource,
81             final JsonRepresentation jsonRepresentation) {
82
83         // Get entity representation
84         final Object entity = this.getEntity(resource, jsonRepresentation);
85
86         return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(entity);
87
88     }
89
90
91     /**
92      * Build default 200 filtered response for resource collection
93      *
94      * @param resources
95      * @param jsonRepresentation
96      * @param headers
97      * @return
98      */
99     protected ResponseEntity<Object> findResponse(final List<?> resources, final JsonRepresentation jsonRepresentation,
100             HttpHeaders headers) {
101
102         // Get entities representation
103         final Object entities = this.getEntities(resources, jsonRepresentation);
104
105         return ResponseEntity.ok().headers(headers).body(entities);
106
107     }
108
109
110     /**
111      * Build 204 Empty response
112      *
113      * @return
114      */
115     protected ResponseEntity<Object> deleteResponse() {
116
117         return ResponseEntity.noContent().build();
118     }
119
120     /**
121      * Get entity, as resource or jacksonNode depending fields value
122      *
123      * @param resource
124      * @param jsonRepresentation
125      * @return
126      */
127     protected Object getEntity(final Object resource, JsonRepresentation jsonRepresentation) {
128
129         Object entity;
130
131         Set<String> attributes = jsonRepresentation.getAttributes();
132
133         if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
134             entity = resource;
135         } else {
136             entity = JacksonFilter.createNode(resource, jsonRepresentation);
137         }
138
139         return entity;
140     }
141
142     /**
143      * Get entities, as resource list or jacksonNode depending fields value
144      *
145      * @param resources
146      * @param jsonRepresentation
147      * @return
148      */
149     protected Object getEntities(final List<?> resources, JsonRepresentation jsonRepresentation) {
150
151         Object entities;
152
153         Set<String> attributes = jsonRepresentation.getAttributes();
154
155         if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
156             entities = resources;
157         } else {
158             entities = JacksonFilter.createNodes(resources, jsonRepresentation);
159         }
160
161         return entities;
162     }
163
164 }