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.NamespacesInstanceModel;
35  import org.apache.hadoop.hbase.rest.model.NamespacesModel;
36  import org.apache.hadoop.hbase.rest.model.RowModel;
37  import org.apache.hadoop.hbase.rest.model.ScannerModel;
38  import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
39  import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
40  import org.apache.hadoop.hbase.rest.model.TableInfoModel;
41  import org.apache.hadoop.hbase.rest.model.TableListModel;
42  import org.apache.hadoop.hbase.rest.model.TableModel;
43  import org.apache.hadoop.hbase.rest.model.TableRegionModel;
44  import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
45  import org.apache.hadoop.hbase.rest.model.VersionModel;
46  
47  import com.sun.jersey.api.json.JSONConfiguration;
48  import com.sun.jersey.api.json.JSONJAXBContext;
49  
50  /**
51   * Plumbing for hooking up Jersey's JSON entity body encoding and decoding
52   * support to JAXB. Modify how the context is created (by using e.g. a 
53   * different configuration builder) to control how JSON is processed and
54   * created.
55   */
56  @Provider
57  @InterfaceAudience.Private
58  public class JAXBContextResolver implements ContextResolver<JAXBContext> {
59  
60    private final JAXBContext context;
61  
62    private final Set<Class<?>> types;
63  
64    private final Class<?>[] cTypes = {
65      CellModel.class,
66      CellSetModel.class,
67      ColumnSchemaModel.class,
68      NamespacesModel.class,
69      NamespacesInstanceModel.class,
70      RowModel.class,
71      ScannerModel.class,
72      StorageClusterStatusModel.class,
73      StorageClusterVersionModel.class,
74      TableInfoModel.class,
75      TableListModel.class,
76      TableModel.class,
77      TableRegionModel.class,
78      TableSchemaModel.class,
79      VersionModel.class
80    };
81  
82    @SuppressWarnings("unchecked")
83    public JAXBContextResolver() throws Exception {
84      this.types = new HashSet(Arrays.asList(cTypes));
85      this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
86        cTypes);
87    }
88  
89    @Override
90    public JAXBContext getContext(Class<?> objectType) {
91      return (types.contains(objectType)) ? context : null;
92    }
93  }