Format Java code with respect to ONAP Code Style
[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
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 {
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         // 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      * Build default 206 filtered partial response for resource
73      *
74      * @param resource
75      * @param jsonRepresentation
76      * @return
77      */
78     protected ResponseEntity<Object> getPartialResponse(final Object resource,
79             final JsonRepresentation jsonRepresentation) {
80
81         // Get entity representation
82         final Object entity = this.getEntity(resource, jsonRepresentation);
83
84         return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(entity);
85
86     }
87
88     /**
89      * Build default 200 filtered response for resource collection
90      *
91      * @param resources
92      * @param jsonRepresentation
93      * @param headers
94      * @return
95      */
96     protected ResponseEntity<Object> findResponse(final List<?> resources, final JsonRepresentation jsonRepresentation,
97             HttpHeaders headers) {
98
99         // Get entities representation
100         final Object entities = this.getEntities(resources, jsonRepresentation);
101
102         return ResponseEntity.ok().headers(headers).body(entities);
103
104     }
105
106     /**
107      * Build 204 Empty response
108      *
109      * @return
110      */
111     protected ResponseEntity<Object> deleteResponse() {
112
113         return ResponseEntity.noContent().build();
114     }
115
116     /**
117      * Get entity, as resource or jacksonNode depending fields value
118      *
119      * @param resource
120      * @param jsonRepresentation
121      * @return
122      */
123     protected Object getEntity(final Object resource, JsonRepresentation jsonRepresentation) {
124
125         Object entity;
126
127         Set<String> attributes = jsonRepresentation.getAttributes();
128
129         if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
130             entity = resource;
131         } else {
132             entity = JacksonFilter.createNode(resource, jsonRepresentation);
133         }
134
135         return entity;
136     }
137
138     /**
139      * Get entities, as resource list or jacksonNode depending fields value
140      *
141      * @param resources
142      * @param jsonRepresentation
143      * @return
144      */
145     protected Object getEntities(final List<?> resources, JsonRepresentation jsonRepresentation) {
146
147         Object entities;
148
149         Set<String> attributes = jsonRepresentation.getAttributes();
150
151         if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
152             entities = resources;
153         } else {
154             entities = JacksonFilter.createNodes(resources, jsonRepresentation);
155         }
156
157         return entities;
158     }
159
160 }