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.producer;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.lang.annotation.Annotation;
26  import java.lang.reflect.Type;
27  
28  import javax.ws.rs.Produces;
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.MessageBodyWriter;
33  import javax.ws.rs.ext.Provider;
34  
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.rest.Constants;
37  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
38  
39  /**
40   * An adapter between Jersey and ProtobufMessageHandler implementors. Hooks up
41   * protobuf output producing methods to the Jersey content handling framework.
42   * Jersey will first call getSize() to learn the number of bytes that will be
43   * sent, then writeTo to perform the actual I/O.
44   */
45  @Provider
46  @Produces({Constants.MIMETYPE_PROTOBUF, Constants.MIMETYPE_PROTOBUF_IETF})
47  @InterfaceAudience.Private
48  public class ProtobufMessageBodyProducer
49    implements MessageBodyWriter<ProtobufMessageHandler> {
50  
51    private ThreadLocal<byte[]> buffer = new ThreadLocal<byte[]>();
52  
53    @Override
54    public boolean isWriteable(Class<?> type, Type genericType, 
55      Annotation[] annotations, MediaType mediaType) {
56        return ProtobufMessageHandler.class.isAssignableFrom(type);
57    }
58  
59    @Override
60    public long getSize(ProtobufMessageHandler m, Class<?> type, Type genericType,
61        Annotation[] annotations, MediaType mediaType) {
62      ByteArrayOutputStream baos = new ByteArrayOutputStream();
63      try {
64        baos.write(m.createProtobufOutput());
65      } catch (IOException e) {
66        return -1;
67      }
68      byte[] bytes = baos.toByteArray();
69      buffer.set(bytes);
70      return bytes.length;
71    }
72  
73    public void writeTo(ProtobufMessageHandler m, Class<?> type, Type genericType,
74        Annotation[] annotations, MediaType mediaType, 
75        MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) 
76        throws IOException, WebApplicationException {
77      byte[] bytes = buffer.get();
78      entityStream.write(bytes);
79      buffer.remove();
80    }
81  }