This project has retired. For details please refer to its Attic page.
WidgetContextResolver xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
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