1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.rest.resource;
19
20 import com.sun.jersey.api.json.JSONJAXBContext;
21
22 import org.apache.hadoop.chukwa.rest.bean.CatalogBean;
23 import org.apache.hadoop.chukwa.rest.bean.CategoryBean;
24
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30
31 import javax.ws.rs.ext.ContextResolver;
32 import javax.ws.rs.ext.Provider;
33 import javax.xml.bind.JAXBContext;
34
35 @Provider
36 public class WidgetContextResolver implements ContextResolver<JAXBContext> {
37 private JAXBContext context;
38 private Set<Class<?>> types;
39 protected Class<?>[] classTypes = new Class[] {CatalogBean.class, CategoryBean.class};
40 protected Set<String> jsonArray = new HashSet<String>(1) {
41 {
42 add("children");
43 }
44 };
45
46 public WidgetContextResolver() throws Exception {
47 Map props = new HashMap<String, Object>();
48 props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
49 props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
50 props.put(JSONJAXBContext.JSON_ARRAYS, jsonArray);
51 this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
52 this.context = new JSONJAXBContext(classTypes, props);
53 }
54
55 public JAXBContext getContext(Class<?> objectType) {
56 return (types.contains(objectType)) ? context : null;
57 }
58 }
59