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;
21import org.apache.hadoop.chukwa.rest.bean.ViewBean;
2223import java.util.Arrays;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Map;
27import java.util.Set;
2829import javax.ws.rs.ext.ContextResolver;
30import javax.ws.rs.ext.Provider;
31import javax.xml.bind.JAXBContext;
3233 @Provider
34publicclassViewContextResolverimplements ContextResolver<JAXBContext> {
35private JAXBContext context;
36private Set<Class<?>> types;
37protected Class<?>[] classTypes = new Class[] {ViewBean.class};
38protected Set<String> jsonArray = new HashSet<String>(5) {
39 {
40 add("pages");
41 add("layout");
42 add("colSize");
43 add("widgets");
44 add("parameters");
45 add("options");
46 }
47 };
4849publicViewContextResolver() throws Exception {
50 Map props = new HashMap<String, Object>();
51 props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
52 props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
53 props.put(JSONJAXBContext.JSON_ARRAYS, jsonArray);
54this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
55this.context = new JSONJAXBContext(classTypes, props);
56 }
5758public JAXBContext getContext(Class<?> objectType) {
59return (types.contains(objectType)) ? context : null;
60 }
6162// private final JAXBContext context;63//64// public ViewContextResolver() throws Exception {65// this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), "package.of.your.model");66// }67//68// public JAXBContext getContext(Class<?> objectType) {69// return context;70// }7172 }
73