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.hdfs.server.datanode.web.resources;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.lang.annotation.Annotation;
023import java.lang.reflect.Type;
024
025import javax.ws.rs.core.MediaType;
026import javax.ws.rs.core.MultivaluedMap;
027import javax.ws.rs.ext.MessageBodyWriter;
028import javax.ws.rs.ext.Provider;
029
030import org.apache.hadoop.hdfs.DFSClient;
031import org.apache.hadoop.hdfs.client.HdfsDataInputStream;
032import org.apache.hadoop.io.IOUtils;
033
034/**
035 * A response entity for a HdfsDataInputStream.
036 */
037public class OpenEntity {
038  private final HdfsDataInputStream in;
039  private final long length;
040  private final int outBufferSize;
041  private final DFSClient dfsclient;
042  
043  OpenEntity(final HdfsDataInputStream in, final long length,
044      final int outBufferSize, final DFSClient dfsclient) {
045    this.in = in;
046    this.length = length;
047    this.outBufferSize = outBufferSize;
048    this.dfsclient = dfsclient;
049  }
050  
051  /**
052   * A {@link MessageBodyWriter} for {@link OpenEntity}.
053   */
054  @Provider
055  public static class Writer implements MessageBodyWriter<OpenEntity> {
056
057    @Override
058    public boolean isWriteable(Class<?> clazz, Type genericType,
059        Annotation[] annotations, MediaType mediaType) {
060      return clazz == OpenEntity.class
061          && MediaType.APPLICATION_OCTET_STREAM_TYPE.isCompatible(mediaType);
062    }
063
064    @Override
065    public long getSize(OpenEntity e, Class<?> type, Type genericType,
066        Annotation[] annotations, MediaType mediaType) {
067      return e.length;
068    }
069
070    @Override
071    public void writeTo(OpenEntity e, Class<?> type, Type genericType,
072        Annotation[] annotations, MediaType mediaType,
073        MultivaluedMap<String, Object> httpHeaders, OutputStream out
074        ) throws IOException {
075      try {
076        byte[] buf = new byte[e.outBufferSize];
077        long remaining = e.length;
078        while (remaining > 0) {
079          int read = e.in.read(buf, 0, (int)Math.min(buf.length, remaining));
080          if (read == -1) { // EOF
081            break;
082          }
083          out.write(buf, 0, read);
084          out.flush();
085          remaining -= read;
086        }
087      } finally {
088        IOUtils.cleanup(DatanodeWebHdfsMethods.LOG, e.in);
089        IOUtils.cleanup(DatanodeWebHdfsMethods.LOG, e.dfsclient);
090      }
091    }
092  }
093}