Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / 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.controller.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 org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
14
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.ext.ContextResolver;
19 import javax.ws.rs.ext.Provider;
20
21 @Provider
22 @Consumes(MediaType.APPLICATION_JSON)
23 @Produces(MediaType.APPLICATION_JSON)
24 public class JaxbContextResolver implements ContextResolver<ObjectMapper> {
25
26   private ObjectMapper ctx;
27
28   public JaxbContextResolver(){
29     ctx = new ObjectMapper();
30     ctx.registerModule(new JsonOrgModule());
31     ctx.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.ALWAYS);
32   }
33
34   @Override
35   public ObjectMapper getContext(Class<?> aClass) {
36
37     if (ApiDeclaration.class.isAssignableFrom(aClass)){
38       return ctx;
39     }
40
41     return null;//must return null so that jax-rs can continue context search
42   }
43 }