blob: 19635b7e2701f0714f4c3b8e95a6ffb345dd58cc [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
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;
Ray Milkey3f025692015-01-26 11:15:41 -080025import javax.ws.rs.ext.Provider;
26
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080027import java.io.IOException;
28import java.io.OutputStream;
29import java.lang.annotation.Annotation;
30import java.lang.reflect.Type;
31
32/**
33 * JAX-RS Response message body writer.
34 */
Ray Milkey3f025692015-01-26 11:15:41 -080035@Provider
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080036@Produces("application/json")
37public class JsonBodyWriter implements MessageBodyWriter<ObjectNode> {
38
39 private ObjectMapper mapper = new ObjectMapper();
40
41 @Override
42 public boolean isWriteable(Class<?> type, Type genericType,
43 Annotation[] annotations, MediaType mediaType) {
44 return type == ObjectNode.class;
45 }
46
47 @Override
48 public long getSize(ObjectNode node, Class<?> type, Type genericType,
49 Annotation[] annotations, MediaType mediaType) {
50 return -1;
51 }
52
53 @Override
54 public void writeTo(ObjectNode node, Class<?> type, Type genericType,
55 Annotation[] annotations, MediaType mediaType,
56 MultivaluedMap<String, Object> httpHeaders,
57 OutputStream entityStream) throws IOException {
58 mapper.writer().writeValue(entityStream, node);
59 entityStream.flush();
60 }
61}