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