View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.provider.consumer;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.lang.annotation.Annotation;
26  import java.lang.reflect.Type;
27  
28  import javax.ws.rs.Consumes;
29  import javax.ws.rs.WebApplicationException;
30  import javax.ws.rs.core.MediaType;
31  import javax.ws.rs.core.MultivaluedMap;
32  import javax.ws.rs.ext.MessageBodyReader;
33  import javax.ws.rs.ext.Provider;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.rest.Constants;
39  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
40  
41  /**
42   * Adapter for hooking up Jersey content processing dispatch to
43   * ProtobufMessageHandler interface capable handlers for decoding protobuf input.
44   */
45  @Provider
46  @Consumes({Constants.MIMETYPE_PROTOBUF, Constants.MIMETYPE_PROTOBUF_IETF})
47  @InterfaceAudience.Private
48  public class ProtobufMessageBodyConsumer
49      implements MessageBodyReader<ProtobufMessageHandler> {
50    private static final Log LOG =
51      LogFactory.getLog(ProtobufMessageBodyConsumer.class);
52  
53    @Override
54    public boolean isReadable(Class<?> type, Type genericType,
55        Annotation[] annotations, MediaType mediaType) {
56      return ProtobufMessageHandler.class.isAssignableFrom(type);
57    }
58  
59    @Override
60    public ProtobufMessageHandler readFrom(Class<ProtobufMessageHandler> type, Type genericType,
61        Annotation[] annotations, MediaType mediaType,
62        MultivaluedMap<String, String> httpHeaders, InputStream inputStream)
63        throws IOException, WebApplicationException {
64      ProtobufMessageHandler obj = null;
65      try {
66        obj = type.newInstance();
67        ByteArrayOutputStream baos = new ByteArrayOutputStream();
68        byte[] buffer = new byte[4096];
69        int read;
70        do {
71          read = inputStream.read(buffer, 0, buffer.length);
72          if (read > 0) {
73            baos.write(buffer, 0, read);
74          }
75        } while (read > 0);
76        if (LOG.isTraceEnabled()) {
77          LOG.trace(getClass() + ": read " + baos.size() + " bytes from " +
78            inputStream);
79        }
80        obj = obj.getObjectFromMessage(baos.toByteArray());
81      } catch (InstantiationException e) {
82        throw new WebApplicationException(e);
83      } catch (IllegalAccessException e) {
84        throw new WebApplicationException(e);
85      }
86      return obj;
87    }
88  }