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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.io.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.nio.file.Files;
33  import java.nio.file.Path;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * A self contained, repeatable entity that obtains its content from a path.
42   * <p>
43   * This class contains {@link ThreadingBehavior#IMMUTABLE_CONDITIONAL immutable attributes} but subclasses may contain
44   * additional immutable or mutable attributes.
45   * </p>
46   */
47  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
48  public class PathEntity extends AbstractHttpEntity {
49  
50      private final Path path;
51  
52      /**
53       * Constructs a new instance with the given attributes kept as immutable.
54       * <p>
55       * The new instance:
56       * </p>
57       * <ul>
58       * <li>is not chunked.</li>
59       * </ul>
60       *
61       * @param path            The message body contents will be set from this path.
62       * @param contentType     The content-type, may be null.
63       * @param contentEncoding The content encoding string, may be null.
64       */
65      public PathEntity(final Path path, final ContentType contentType, final String contentEncoding) {
66          super(contentType, contentEncoding);
67          this.path = Args.notNull(path, "Path");
68      }
69  
70      /**
71       * Constructs a new instance with the given attributes kept as immutable.
72       * <p>
73       * The new instance:
74       * </p>
75       * <ul>
76       * <li>is not chunked.</li>
77       * <li>does not define a content encoding.</li>
78       * </ul>
79       *
80       * @param path            The message body contents will be set from this path.
81       * @param contentType     The content-type, may be null.
82       */
83      public PathEntity(final Path path, final ContentType contentType) {
84          super(contentType, null);
85          this.path = Args.notNull(path, "Path");
86      }
87  
88      /**
89       * {@inheritDoc}
90       * <p>
91       * This implementation always returns {@code false}.
92       * </p>
93       */
94      @Override
95      public final boolean isRepeatable() {
96          return true;
97      }
98  
99      @Override
100     public final long getContentLength() {
101         try {
102             return Files.size(this.path);
103         } catch (final IOException e) {
104             throw new IllegalStateException(e);
105         }
106     }
107 
108     @Override
109     public final InputStream getContent() throws IOException {
110         return Files.newInputStream(path);
111     }
112 
113     /**
114      * {@inheritDoc}
115      * <p>
116      * This implementation always returns {@code false}.
117      * </p>
118      */
119     @Override
120     public final boolean isStreaming() {
121         return false;
122     }
123 
124     /**
125      * {@inheritDoc}
126      * <p>
127      * This implementation is a no-op.
128      * </p>
129      */
130     @Override
131     public final void close() throws IOException {
132         // do nothing
133     }
134 
135 }