2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.nbi.commons;
19 import java.util.List;
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;
27 public class ResourceManagement<T extends Resource> {
30 * Build default 201 filtered response for resource
33 * @param jsonRepresentation
36 protected ResponseEntity<Object> createResponse(final Resource resource,
37 final JsonRepresentation jsonRepresentation) {
40 if (RequestContextHolder.getRequestAttributes() != null) {
41 location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.getId())
44 location = URI.create("/");
48 // Get entity representation
49 final Object entity = this.getEntity(resource, jsonRepresentation);
51 return ResponseEntity.created(location).body(entity);
56 * Build default 200 filtered response for resource
59 * @param jsonRepresentation
62 protected ResponseEntity<Object> getResponse(final Object resource, final JsonRepresentation jsonRepresentation) {
64 // Get entity representation
65 final Object entity = this.getEntity(resource, jsonRepresentation);
67 return ResponseEntity.ok(entity);
74 * Build default 206 filtered partial response for resource
77 * @param jsonRepresentation
80 protected ResponseEntity<Object> getPartialResponse(final Object resource,
81 final JsonRepresentation jsonRepresentation) {
83 // Get entity representation
84 final Object entity = this.getEntity(resource, jsonRepresentation);
86 return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(entity);
92 * Build default 200 filtered response for resource collection
95 * @param jsonRepresentation
99 protected ResponseEntity<Object> findResponse(final List<?> resources, final JsonRepresentation jsonRepresentation,
100 HttpHeaders headers) {
102 // Get entities representation
103 final Object entities = this.getEntities(resources, jsonRepresentation);
105 return ResponseEntity.ok().headers(headers).body(entities);
111 * Build 204 Empty response
115 protected ResponseEntity<Object> deleteResponse() {
117 return ResponseEntity.noContent().build();
121 * Get entity, as resource or jacksonNode depending fields value
124 * @param jsonRepresentation
127 protected Object getEntity(final Object resource, JsonRepresentation jsonRepresentation) {
131 Set<String> attributes = jsonRepresentation.getAttributes();
133 if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
136 entity = JacksonFilter.createNode(resource, jsonRepresentation);
143 * Get entities, as resource list or jacksonNode depending fields value
146 * @param jsonRepresentation
149 protected Object getEntities(final List<?> resources, JsonRepresentation jsonRepresentation) {
153 Set<String> attributes = jsonRepresentation.getAttributes();
155 if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
156 entities = resources;
158 entities = JacksonFilter.createNodes(resources, jsonRepresentation);