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.impl;
29  
30  import java.util.Collections;
31  import java.util.Set;
32  
33  import org.apache.hc.core5.http.EntityDetails;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpHeaders;
36  import org.apache.hc.core5.http.MessageHeaders;
37  import org.apache.hc.core5.http.message.MessageSupport;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * HTTP message entity details.
42   *
43   * @since 5.0
44   */
45  public class IncomingEntityDetails implements EntityDetails {
46  
47      private final MessageHeaders message;
48      private final long contentLength;
49  
50      public IncomingEntityDetails(final MessageHeaders message, final long contentLength) {
51          this.message = Args.notNull(message, "Message");
52          this.contentLength = contentLength;
53      }
54  
55      public IncomingEntityDetails(final MessageHeaders message) {
56          this(message, -1);
57      }
58  
59      @Override
60      public long getContentLength() {
61          return contentLength;
62      }
63  
64      @Override
65      public String getContentType() {
66          final Header h = message.getFirstHeader(HttpHeaders.CONTENT_TYPE);
67          return h != null ? h.getValue() : null;
68      }
69  
70      @Override
71      public String getContentEncoding() {
72          final Header h = message.getFirstHeader(HttpHeaders.CONTENT_ENCODING);
73          return h != null ? h.getValue() : null;
74      }
75  
76      @Override
77      public boolean isChunked() {
78          return contentLength < 0;
79      }
80  
81      @Override
82      public Set<String> getTrailerNames() {
83          final Header h = message.getFirstHeader(HttpHeaders.TRAILER);
84          if (h == null) {
85              return Collections.emptySet();
86          }
87          return MessageSupport.parseTokens(h);
88      }
89  
90  }