1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.rest.resource;
1920import com.sun.jersey.api.json.JSONJAXBContext;
2122import org.apache.hadoop.chukwa.rest.bean.CatalogBean;
23import org.apache.hadoop.chukwa.rest.bean.CategoryBean;
2425import java.util.Arrays;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.Map;
29import java.util.Set;
3031import javax.ws.rs.ext.ContextResolver;
32import javax.ws.rs.ext.Provider;
33import javax.xml.bind.JAXBContext;
3435 @Provider
36publicclassWidgetContextResolverimplements ContextResolver<JAXBContext> {
37private JAXBContext context;
38private Set<Class<?>> types;
39protected Class<?>[] classTypes = new Class[] {CatalogBean.class, CategoryBean.class};
40protected Set<String> jsonArray = new HashSet<String>(1) {
41 {
42 add("children");
43 }
44 };
4546publicWidgetContextResolver() 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);
51this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
52this.context = new JSONJAXBContext(classTypes, props);
53 }
5455public JAXBContext getContext(Class<?> objectType) {
56return (types.contains(objectType)) ? context : null;
57 }
58 }
59