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.juneau.http.UnmodifiableBean;
023import org.apache.juneau.http.classic.entity.*;
024import org.apache.juneau.http.classic.header.*;
025
026/**
027 * A repeatable resource that obtains its content from a {@link File}.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
031 * </ul>
032 */
033public class FileResource extends BasicResource<FileResource> {
034
035   /**
036    * Constructor.
037    */
038   public FileResource() {
039      super(new FileEntity());
040   }
041
042   /**
043    * Constructor.
044    *
045    * @param contentType The entity content type.  Can be <jk>null</jk> to omit an explicit <c>Content-Type</c> header.
046    * @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.
047    */
048   public FileResource(ContentType contentType, File contents) {
049      super(new FileEntity(contentType, contents));
050   }
051
052   /**
053    * Copy constructor.
054    *
055    * @param copyFrom The bean being copied.  Must not be <jk>null</jk>.
056    */
057   protected FileResource(FileResource copyFrom) {
058      super(copyFrom);
059   }
060
061   @Override /* Overridden from BasicResource */
062   public FileResource copy() {
063      return new FileResource(this);
064   }
065
066   @Override /* Overridden from BasicResource */
067   public FileResource unmodifiable() {
068      return this instanceof UnmodifiableBean ? this : new Unmodifiable(this);
069   }
070
071   /**
072    * An unmodifiable snapshot of a {@link FileResource}.
073    */
074   public static class Unmodifiable extends FileResource implements UnmodifiableBean {
075
076      /**
077       * Constructor.
078       *
079       * @param copyFrom The bean to snapshot-copy.  Must not be <jk>null</jk>.
080       */
081      @SuppressWarnings({
082         "java:S1699" // Paradigm intentionally calls the overridable freeze() from the ctor to deep-freeze sub-beans.
083      })
084      protected Unmodifiable(FileResource copyFrom) {
085         super(copyFrom);
086         freeze();
087      }
088
089      @Override /* Overridden from BasicResource */
090      protected FileResource modify(Runnable mutation) {
091         throw uoex("Bean is unmodifiable.");
092      }
093   }
094}