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.consumer; 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.lang.annotation.Annotation; 023import java.lang.reflect.InvocationTargetException; 024import java.lang.reflect.Type; 025import org.apache.hadoop.hbase.rest.Constants; 026import org.apache.hadoop.hbase.rest.ProtobufMessageHandler; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import org.apache.hbase.thirdparty.javax.ws.rs.Consumes; 032import org.apache.hbase.thirdparty.javax.ws.rs.WebApplicationException; 033import org.apache.hbase.thirdparty.javax.ws.rs.core.MediaType; 034import org.apache.hbase.thirdparty.javax.ws.rs.core.MultivaluedMap; 035import org.apache.hbase.thirdparty.javax.ws.rs.ext.MessageBodyReader; 036import org.apache.hbase.thirdparty.javax.ws.rs.ext.Provider; 037 038/** 039 * Adapter for hooking up Jersey content processing dispatch to ProtobufMessageHandler interface 040 * capable handlers for decoding protobuf input. 041 */ 042@Provider 043@Consumes({ Constants.MIMETYPE_PROTOBUF, Constants.MIMETYPE_PROTOBUF_IETF }) 044@InterfaceAudience.Private 045public class ProtobufMessageBodyConsumer implements MessageBodyReader<ProtobufMessageHandler> { 046 private static final Logger LOG = LoggerFactory.getLogger(ProtobufMessageBodyConsumer.class); 047 048 @Override 049 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, 050 MediaType mediaType) { 051 return ProtobufMessageHandler.class.isAssignableFrom(type); 052 } 053 054 @Override 055 public ProtobufMessageHandler readFrom(Class<ProtobufMessageHandler> type, Type genericType, 056 Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, 057 InputStream inputStream) throws IOException, WebApplicationException { 058 ProtobufMessageHandler obj = null; 059 try { 060 obj = type.getDeclaredConstructor().newInstance(); 061 return obj.getObjectFromMessage(inputStream); 062 } catch (InstantiationException | NoSuchMethodException | InvocationTargetException 063 | IllegalAccessException e) { 064 throw new WebApplicationException(e); 065 } 066 } 067}