de4e0a966f2f534500b63febb05c297c0d3c07a8
[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 javax.ws.rs.Consumes;
11 import javax.ws.rs.Produces;
12 import javax.ws.rs.core.MediaType;
13 import javax.ws.rs.ext.ContextResolver;
14 import javax.ws.rs.ext.Provider;
15
16 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
17
18 import com.fasterxml.jackson.annotation.JsonInclude;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
21
22 @Provider
23 @Consumes(MediaType.APPLICATION_JSON)
24 @Produces(MediaType.APPLICATION_JSON)
25 public class JaxbContextResolver implements ContextResolver<ObjectMapper> {
26
27     private final ObjectMapper ctx;
28
29     public JaxbContextResolver() {
30         ctx = new ObjectMapper();
31         ctx.registerModule(new JsonOrgModule());
32         ctx.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.ALWAYS);
33     }
34
35     @Override
36     public ObjectMapper getContext(Class<?> klass) {
37         if (ApiDeclaration.class.isAssignableFrom(klass)) {
38             return ctx;
39         }
40
41         return null; // must return null so that JAX-RS can continue context search
42     }
43 }