View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.provider;
21  
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import javax.ws.rs.ext.ContextResolver;
27  import javax.ws.rs.ext.Provider;
28  import javax.xml.bind.JAXBContext;
29  
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.rest.model.CellModel;
32  import org.apache.hadoop.hbase.rest.model.CellSetModel;
33  import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
34  import org.apache.hadoop.hbase.rest.model.RowModel;
35  import org.apache.hadoop.hbase.rest.model.ScannerModel;
36  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
37  import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
38  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
39  import org.apache.hadoop.hbase.rest.model.TableListModel;
40  import org.apache.hadoop.hbase.rest.model.TableModel;
41  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
42  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
43  import org.apache.hadoop.hbase.rest.model.VersionModel;
44  
45  import com.sun.jersey.api.json.JSONConfiguration;
46  import com.sun.jersey.api.json.JSONJAXBContext;
47  
48  /**
49   * Plumbing for hooking up Jersey's JSON entity body encoding and decoding
50   * support to JAXB. Modify how the context is created (by using e.g. a 
51   * different configuration builder) to control how JSON is processed and
52   * created.
53   */
54  @Provider
55  @InterfaceAudience.Private
56  public class JAXBContextResolver implements ContextResolver<JAXBContext> {
57  
58    private final JAXBContext context;
59  
60    private final Set<Class<?>> types;
61  
62    private final Class<?>[] cTypes = {
63      CellModel.class,
64      CellSetModel.class,
65      ColumnSchemaModel.class,
66      RowModel.class,
67      ScannerModel.class,
68      StorageClusterStatusModel.class,
69      StorageClusterVersionModel.class,
70      TableInfoModel.class,
71      TableListModel.class,
72      TableModel.class,
73      TableRegionModel.class,
74      TableSchemaModel.class,
75      VersionModel.class
76    };
77  
78    @SuppressWarnings("unchecked")
79    public JAXBContextResolver() throws Exception {
80      this.types = new HashSet(Arrays.asList(cTypes));
81      this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
82        cTypes);
83    }
84  
85    @Override
86    public JAXBContext getContext(Class<?> objectType) {
87      return (types.contains(objectType)) ? context : null;
88    }
89  }