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