summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/nbi/commons/ResourceManagement.java
blob: 67c0e3eb11a320beda7d943ab1e21a78fbba3a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.onap.nbi.commons;

import java.net.URI;
import java.util.List;
import java.util.Set;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

public class ResourceManagement<T extends Resource> {

    /**
     * Build default 201 filtered response for resource
     *
     * @param resource
     * @param jsonRepresentation
     * @return
     */
    protected ResponseEntity<Object> createResponse(final Resource resource,
            final JsonRepresentation jsonRepresentation) {

        URI location = null;
        if (RequestContextHolder.getRequestAttributes() != null) {
            location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(resource.getId())
                    .toUri();
        } else {
            location = URI.create("/");
        }


        // Get entity representation
        final Object entity = this.getEntity(resource, jsonRepresentation);

        return ResponseEntity.created(location).body(entity);

    }

    /**
     * Build default 200 filtered response for resource
     *
     * @param resource
     * @param jsonRepresentation
     * @return
     */
    protected ResponseEntity<Object> getResponse(final Object resource, final JsonRepresentation jsonRepresentation) {

        // Get entity representation
        final Object entity = this.getEntity(resource, jsonRepresentation);

        return ResponseEntity.ok(entity);

    }



    /**
     * Build default 206 filtered partial response for resource
     *
     * @param resource
     * @param jsonRepresentation
     * @return
     */
    protected ResponseEntity<Object> getPartialResponse(final Object resource,
            final JsonRepresentation jsonRepresentation) {

        // Get entity representation
        final Object entity = this.getEntity(resource, jsonRepresentation);

        return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(entity);

    }


    /**
     * Build default 200 filtered response for resource collection
     *
     * @param resources
     * @param jsonRepresentation
     * @param headers
     * @return
     */
    protected ResponseEntity<Object> findResponse(final List<?> resources, final JsonRepresentation jsonRepresentation,
            HttpHeaders headers) {

        // Get entities representation
        final Object entities = this.getEntities(resources, jsonRepresentation);

        return ResponseEntity.ok().headers(headers).body(entities);

    }


    /**
     * Build 204 Empty response
     *
     * @return
     */
    protected ResponseEntity<Object> deleteResponse() {

        return ResponseEntity.noContent().build();
    }

    /**
     * Get entity, as resource or jacksonNode depending fields value
     *
     * @param resource
     * @param jsonRepresentation
     * @return
     */
    protected Object getEntity(final Object resource, JsonRepresentation jsonRepresentation) {

        Object entity;

        Set<String> attributes = jsonRepresentation.getAttributes();

        if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
            entity = resource;
        } else {
            entity = JacksonFilter.createNode(resource, jsonRepresentation);
        }

        return entity;
    }

    /**
     * Get entities, as resource list or jacksonNode depending fields value
     *
     * @param resources
     * @param jsonRepresentation
     * @return
     */
    protected Object getEntities(final List<?> resources, JsonRepresentation jsonRepresentation) {

        Object entities;

        Set<String> attributes = jsonRepresentation.getAttributes();

        if (attributes == null || attributes.isEmpty() || attributes.contains(ReservedKeys.ALL_FIELDS)) {
            entities = resources;
        } else {
            entities = JacksonFilter.createNodes(resources, jsonRepresentation);
        }

        return entities;
    }

}