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.
17 package org.onap.nbi.commons;
20 import java.util.List;
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;
28 public class ResourceManagement {
31 * Build default 201 filtered response for resource
34 * @param jsonRepresentation
37 protected ResponseEntity<Object> createResponse(final Resource resource,
38 final JsonRepresentation jsonRepresentation) {
41 if (RequestContextHolder.getRequestAttributes() != null) {
42 location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.getId())
45 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);
72 * Build default 206 filtered partial response for resource
75 * @param jsonRepresentation
78 protected ResponseEntity<Object> getPartialResponse(final Object resource,
79 final JsonRepresentation jsonRepresentation) {
81 // Get entity representation
82 final Object entity = this.getEntity(resource, jsonRepresentation);
84 return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(entity);
89 * Build default 200 filtered response for resource collection
92 * @param jsonRepresentation
96 protected ResponseEntity<Object> findResponse(final List<?> resources, final JsonRepresentation jsonRepresentation,
97 HttpHeaders headers) {
99 // Get entities representation
100 final Object entities = this.getEntities(resources, jsonRepresentation);
102 return ResponseEntity.ok().headers(headers).body(entities);
107 * Build 204 Empty response
111 protected ResponseEntity<Object> deleteResponse() {
113 return ResponseEntity.noContent().build();
117 * Get entity, as resource or jacksonNode depending fields value
120 * @param jsonRepresentation
123 protected Object getEntity(final Object resource, JsonRepresentation jsonRepresentation) {
127 Set<String> attributes = jsonRepresentation.getAttributes();
129 if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
132 entity = JacksonFilter.createNode(resource, jsonRepresentation);
139 * Get entities, as resource list or jacksonNode depending fields value
142 * @param jsonRepresentation
145 protected Object getEntities(final List<?> resources, JsonRepresentation jsonRepresentation) {
149 Set<String> attributes = jsonRepresentation.getAttributes();
151 if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
152 entities = resources;
154 entities = JacksonFilter.createNodes(resources, jsonRepresentation);