001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.provider;
019
020import java.util.Arrays;
021import java.util.HashSet;
022import java.util.Set;
023import javax.xml.bind.JAXBContext;
024import org.apache.hadoop.hbase.rest.model.CellModel;
025import org.apache.hadoop.hbase.rest.model.CellSetModel;
026import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
027import org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel;
028import org.apache.hadoop.hbase.rest.model.NamespacesModel;
029import org.apache.hadoop.hbase.rest.model.RowModel;
030import org.apache.hadoop.hbase.rest.model.ScannerModel;
031import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
032import org.apache.hadoop.hbase.rest.model.StorageClusterVersionModel;
033import org.apache.hadoop.hbase.rest.model.TableInfoModel;
034import org.apache.hadoop.hbase.rest.model.TableListModel;
035import org.apache.hadoop.hbase.rest.model.TableModel;
036import org.apache.hadoop.hbase.rest.model.TableRegionModel;
037import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
038import org.apache.hadoop.hbase.rest.model.VersionModel;
039import org.apache.yetus.audience.InterfaceAudience;
040
041import org.apache.hbase.thirdparty.javax.ws.rs.ext.ContextResolver;
042import org.apache.hbase.thirdparty.javax.ws.rs.ext.Provider;
043
044/**
045 * Plumbing for hooking up Jersey's JSON entity body encoding and decoding support to JAXB. Modify
046 * how the context is created (by using e.g. a different configuration builder) to control how JSON
047 * is processed and created.
048 */
049@Provider
050@InterfaceAudience.Private
051public class JAXBContextResolver implements ContextResolver<JAXBContext> {
052
053  private final JAXBContext context;
054
055  private final Set<Class<?>> types;
056
057  private final Class<?>[] cTypes = { CellModel.class, CellSetModel.class, ColumnSchemaModel.class,
058    NamespacesModel.class, NamespacesInstanceModel.class, RowModel.class, ScannerModel.class,
059    StorageClusterStatusModel.class, StorageClusterVersionModel.class, TableInfoModel.class,
060    TableListModel.class, TableModel.class, TableRegionModel.class, TableSchemaModel.class,
061    VersionModel.class };
062
063  @SuppressWarnings("unchecked")
064  public JAXBContextResolver() throws Exception {
065    this.types = new HashSet(Arrays.asList(cTypes));
066    context = JAXBContext.newInstance(cTypes);
067  }
068
069  @Override
070  public JAXBContext getContext(Class<?> objectType) {
071    return (types.contains(objectType)) ? context : null;
072  }
073}