View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.fileupload2.core;
19  
20  import java.util.Objects;
21  import java.util.function.Function;
22  import java.util.function.LongSupplier;
23  
24  public abstract class AbstractRequestContext<T> implements RequestContext {
25  
26      /**
27       * Supplies the content length default.
28       */
29      private final LongSupplier contentLengthDefault;
30  
31      /**
32       * Supplies the content length string.
33       */
34      private final Function<String, String> contentLengthString;
35  
36      /**
37       * The request.
38       */
39      private final T request;
40  
41      /**
42       * Constructs a new instance.
43       *
44       * @param contentLengthString  How to get the content length string.
45       * @param contentLengthDefault How to get the content length default.
46       * @param request              The request.
47       */
48      protected AbstractRequestContext(final Function<String, String> contentLengthString, final LongSupplier contentLengthDefault, final T request) {
49          this.contentLengthString = Objects.requireNonNull(contentLengthString, "contentLengthString");
50          this.contentLengthDefault = Objects.requireNonNull(contentLengthDefault, "contentLengthDefault");
51          this.request = Objects.requireNonNull(request, "request");
52      }
53  
54      /**
55       * Gets the content length of the request.
56       *
57       * @return The content length of the request.
58       */
59      @Override
60      public long getContentLength() {
61          try {
62              return Long.parseLong(contentLengthString.apply(AbstractFileUpload.CONTENT_LENGTH));
63          } catch (final NumberFormatException e) {
64              return contentLengthDefault.getAsLong();
65          }
66      }
67  
68      public T getRequest() {
69          return request;
70      }
71  
72      /**
73       * Returns a string representation of this object.
74       *
75       * @return a string representation of this object.
76       */
77      @Override
78      public String toString() {
79          return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType());
80      }
81  
82  }