Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.rest; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 21 | import org.onlab.rest.BaseResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.CodecService; |
| 24 | import org.onosproject.codec.JsonCodec; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Abstract REST resource. |
| 28 | */ |
| 29 | public class AbstractWebResource extends BaseResource implements CodecContext { |
| 30 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 31 | private final ObjectMapper mapper = new ObjectMapper(); |
| 32 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 33 | @Override |
| 34 | public ObjectMapper mapper() { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 35 | return mapper; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 36 | } |
| 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 Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public <T> T getService(Class<T> serviceClass) { |
| 68 | return get(serviceClass); |
| 69 | } |
| 70 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 71 | /** |
| 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 Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 98 | } |