001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest;
014
015import java.io.*;
016import java.util.*;
017
018import org.apache.juneau.http.annotation.*;
019
020/**
021 * Instance of a static file sent as an HTTP response.
022 */
023@Response
024public class StaticFile {
025
026   private final byte[] contents;
027   private final String mediaType;
028   private final Map<String,Object> headers;
029
030   /**
031    * Constructor.
032    *
033    * @param contents Contents of the file, or <jk>null</jk> if file does not exist.
034    * @param mediaType The media type of the file.
035    * @param headers Arbitrary response headers to set when sending this file as an HTTP response.
036    */
037   public StaticFile(byte[] contents, String mediaType, Map<String,Object> headers) {
038      this.contents = contents;
039      this.mediaType = mediaType;
040      this.headers = headers;
041   }
042
043   /**
044    * Does this file exist?
045    *
046    * @return <jk>true</jk> if this file exists.
047    */
048   public boolean exists() {
049      return contents != null;
050   }
051
052   /**
053    * Get the HTTP response headers.
054    *
055    * @return
056    *    The HTTP response headers.
057    *    <br>An unmodifiable map.
058    *    <br>Never <jk>null</jk>.
059    */
060   @ResponseHeader("*")
061   public Map<String,Object> getHeaders() {
062      return headers;
063   }
064
065   /**
066    * Returns the contents of this static file as an input stream.
067    *
068    * @return This file as an input stream.
069    * @throws IOException Should never happen.
070    */
071   @ResponseBody
072   public InputStream getInputStream() throws IOException {
073      return new ByteArrayInputStream(contents);
074   }
075
076   /**
077    * Returns the content type for this static file.
078    *
079    * @return The content type for this static file.
080    */
081   @ResponseHeader("Content-Type")
082   public String getContentType() {
083      return mediaType == null ? null : mediaType.toString();
084   }
085}