001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http.classic.resource;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.io.*;
022import org.apache.http.*;
023import org.apache.juneau.http.UnmodifiableBean;
024import org.apache.juneau.http.classic.entity.*;
025import org.apache.juneau.http.classic.header.*;
026
027/**
028 * A streamed, non-repeatable resource that obtains its content from an {@link InputStream}.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
032 * </ul>
033 */
034public class StreamResource extends BasicResource<StreamResource> {
035
036   /**
037    * Constructor.
038    */
039   public StreamResource() {
040      super(new StreamEntity());
041   }
042
043   /**
044    * Constructor.
045    *
046    * @param contentType The entity content type.  Can be <jk>null</jk> to omit an explicit <c>Content-Type</c> header.
047    * @param contents The entity contents.  Can be <jk>null</jk>, in which case a {@link NullPointerException} is thrown when the resource's content is read.
048    */
049   public StreamResource(ContentType contentType, InputStream contents) {
050      super(new StreamEntity(contentType, contents));
051   }
052
053   /**
054    * Copy constructor.
055    *
056    * @param copyFrom The bean being copied.  Must not be <jk>null</jk>.
057    */
058   protected StreamResource(StreamResource copyFrom) {
059      super(copyFrom);
060   }
061
062   /**
063    * Constructor.
064    *
065    * <p>
066    * This is the constructor used when converting an HTTP response into a resource.
067    *
068    * @param response The HTTP response to copy from.  Must not be <jk>null</jk>.
069    * @throws IOException Rethrown from {@link HttpEntity#getContent()}.
070    */
071   public StreamResource(HttpResponse response) throws IOException {
072      super(response);
073   }
074
075   @Override /* Overridden from BasicResource */
076   public StreamResource copy() {
077      return new StreamResource(this);
078   }
079
080   @Override /* Overridden from BasicResource */
081   public StreamResource unmodifiable() {
082      return this instanceof UnmodifiableBean ? this : new Unmodifiable(this);
083   }
084
085   /**
086    * An unmodifiable snapshot of a {@link StreamResource}.
087    */
088   public static class Unmodifiable extends StreamResource implements UnmodifiableBean {
089
090      /**
091       * Constructor.
092       *
093       * @param copyFrom The bean to snapshot-copy.  Must not be <jk>null</jk>.
094       */
095      @SuppressWarnings({
096         "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans.
097      })
098      protected Unmodifiable(StreamResource copyFrom) {
099         super(copyFrom);
100         freeze();
101      }
102
103      @Override /* Overridden from BasicResource */
104      protected StreamResource modify(Runnable mutation) {
105         throw uoex("Bean is unmodifiable.");
106      }
107   }
108}