This project has retired. For details please refer to its Attic page.
ViewContextResolver 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  import org.apache.hadoop.chukwa.rest.bean.ViewBean;
22  
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.ws.rs.ext.ContextResolver;
30  import javax.ws.rs.ext.Provider;
31  import javax.xml.bind.JAXBContext;
32  
33    @Provider
34    public class ViewContextResolver implements ContextResolver<JAXBContext> {
35        private JAXBContext context;
36        private Set<Class<?>> types;
37        protected Class<?>[] classTypes = new Class[] {ViewBean.class};
38        protected 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        };
48  
49        public ViewContextResolver() 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);
54            this.types = new HashSet<Class<?>>(Arrays.asList(classTypes));
55            this.context = new JSONJAXBContext(classTypes, props);
56        }
57  
58        public JAXBContext getContext(Class<?> objectType) {
59            return (types.contains(objectType)) ? context : null;
60        }
61  
62  //    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  //  }
71  
72    }
73