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