de8c5bc50163f590e8aa37dc088cb6ab59fcf23b
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / jaxrs / JaxbContextResolver.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.rest.doc.jaxrs;
9
10 import com.fasterxml.jackson.annotation.JsonInclude;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
13 import javax.ws.rs.Consumes;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.ext.ContextResolver;
17 import javax.ws.rs.ext.Provider;
18 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
19
20 @Provider
21 @Consumes(MediaType.APPLICATION_JSON)
22 @Produces(MediaType.APPLICATION_JSON)
23 public class JaxbContextResolver implements ContextResolver<ObjectMapper> {
24
25     private final ObjectMapper ctx;
26
27     public JaxbContextResolver() {
28         ctx = new ObjectMapper();
29         ctx.registerModule(new JsonOrgModule());
30         ctx.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.ALWAYS);
31     }
32
33     @Override
34     public ObjectMapper getContext(final Class<?> klass) {
35         if (ApiDeclaration.class.isAssignableFrom(klass)) {
36             return ctx;
37         }
38
39         return null; // must return null so that JAX-RS can continue context search
40     }
41 }