Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 20 | import org.onlab.rest.BaseResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.CodecService; |
| 23 | import org.onosproject.codec.JsonCodec; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Abstract REST resource. |
| 27 | */ |
| 28 | public class AbstractWebResource extends BaseResource implements CodecContext { |
| 29 | |
| 30 | @Override |
| 31 | public ObjectMapper mapper() { |
| 32 | return new ObjectMapper(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns the JSON codec for the specified entity class. |
| 37 | * |
| 38 | * @param entityClass entity class |
| 39 | * @param <T> entity type |
| 40 | * @return JSON codec |
| 41 | */ |
| 42 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 43 | return get(CodecService.class).getCodec(entityClass); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns JSON object wrapping the array encoding of the specified |
| 48 | * collection of items. |
| 49 | * |
| 50 | * @param codecClass codec item class |
| 51 | * @param field field holding the array |
| 52 | * @param items collection of items to be encoded into array |
| 53 | * @param <T> item type |
| 54 | * @return JSON object |
| 55 | */ |
| 56 | protected <T> ObjectNode encodeArray(Class<T> codecClass, String field, |
| 57 | Iterable<T> items) { |
| 58 | ObjectNode result = mapper().createObjectNode(); |
| 59 | result.set(field, codec(codecClass).encode(items, this)); |
| 60 | return result; |
| 61 | } |
| 62 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 63 | @Override |
| 64 | public <T> T getService(Class<T> serviceClass) { |
| 65 | return get(serviceClass); |
| 66 | } |
| 67 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 68 | } |