View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest.provider;
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import javax.ws.rs.ext.ContextResolver;
28  import javax.ws.rs.ext.Provider;
29  import javax.xml.bind.JAXBContext;
30  
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  public class JAXBContextResolver implements ContextResolver<JAXBContext> {
56  
57  	private final JAXBContext context;
58  
59  	private final Set<Class<?>> types;
60  
61  	private final Class<?>[] cTypes = {
62  	  CellModel.class,
63      CellSetModel.class,
64      ColumnSchemaModel.class,
65      RowModel.class,
66      ScannerModel.class,
67      StorageClusterStatusModel.class,
68      StorageClusterVersionModel.class,
69      TableInfoModel.class,
70  	  TableListModel.class,
71  	  TableModel.class,
72  	  TableRegionModel.class,
73  	  TableSchemaModel.class,
74  	  VersionModel.class
75  	};
76  
77  	@SuppressWarnings("unchecked")
78    public JAXBContextResolver() throws Exception {
79  		this.types = new HashSet(Arrays.asList(cTypes));
80  		this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
81  		  cTypes);
82  	}
83  
84  	@Override
85  	public JAXBContext getContext(Class<?> objectType) {
86  		return (types.contains(objectType)) ? context : null;
87    }
88  }