blob: bf9b42c5ab86c42b1750a3fd21bf248e28ed6cc2 [file] [log] [blame]
Sean Condone4042d32018-05-21 17:53:22 +01001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.restconf.api;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import javax.ws.rs.Produces;
22import javax.ws.rs.core.MediaType;
23import javax.ws.rs.core.MultivaluedMap;
24import javax.ws.rs.ext.MessageBodyWriter;
25import javax.ws.rs.ext.Provider;
26import java.io.IOException;
27import java.io.OutputStream;
28import java.lang.annotation.Annotation;
29import java.lang.reflect.Type;
30
31@Provider
32@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
33public class RestconfJsonBodyWriter implements MessageBodyWriter<ObjectNode> {
34 private ObjectMapper mapper = new ObjectMapper();
35
36 @Override
37 public boolean isWriteable(Class<?> type, Type genericType,
38 Annotation[] annotations, MediaType mediaType) {
39 return type == ObjectNode.class;
40 }
41
42 @Override
43 public void writeTo(ObjectNode node, Class<?> type, Type genericType,
44 Annotation[] annotations, MediaType mediaType,
45 MultivaluedMap<String, Object> httpHeaders,
46 OutputStream entityStream) throws IOException {
47 mapper.writer().writeValue(entityStream, node);
48 entityStream.flush();
49 }
50}