blob: de631ab33ee577f7ffd696f124374aca13794549 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.rest;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070021import org.onlab.rest.BaseResource;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.CodecService;
24import org.onosproject.codec.JsonCodec;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080025
26/**
27 * Abstract REST resource.
28 */
29public class AbstractWebResource extends BaseResource implements CodecContext {
30
Thomas Vachuska96d55b12015-05-11 08:52:03 -070031 private final ObjectMapper mapper = new ObjectMapper();
32
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033 @Override
34 public ObjectMapper mapper() {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070035 return mapper;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080036 }
37
38 /**
39 * Returns the JSON codec for the specified entity class.
40 *
41 * @param entityClass entity class
42 * @param <T> entity type
43 * @return JSON codec
44 */
45 public <T> JsonCodec<T> codec(Class<T> entityClass) {
46 return get(CodecService.class).getCodec(entityClass);
47 }
48
49 /**
50 * Returns JSON object wrapping the array encoding of the specified
51 * collection of items.
52 *
53 * @param codecClass codec item class
54 * @param field field holding the array
55 * @param items collection of items to be encoded into array
56 * @param <T> item type
57 * @return JSON object
58 */
59 protected <T> ObjectNode encodeArray(Class<T> codecClass, String field,
60 Iterable<T> items) {
61 ObjectNode result = mapper().createObjectNode();
62 result.set(field, codec(codecClass).encode(items, this));
63 return result;
64 }
65
Ray Milkey3078fc02015-05-06 16:14:14 -070066 @Override
67 public <T> T getService(Class<T> serviceClass) {
68 return get(serviceClass);
69 }
70
Thomas Vachuska96d55b12015-05-11 08:52:03 -070071 /**
72 * Creates and returns a new child object within the specified parent and
73 * bound to the given key.
74 *
75 * @param parent parent object
76 * @param key key for the new child object
77 * @return child object
78 */
79 public ObjectNode newObject(ObjectNode parent, String key) {
80 ObjectNode node = mapper.createObjectNode();
81 parent.set(key, node);
82 return node;
83 }
84
85 /**
86 * Creates and returns a new child array within the specified parent and
87 * bound to the given key.
88 *
89 * @param parent parent object
90 * @param key key for the new child array
91 * @return child array
92 */
93 public ArrayNode newArray(ObjectNode parent, String key) {
94 ArrayNode node = mapper.createArrayNode();
95 parent.set(key, node);
96 return node;
97 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080098}