1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29 private final LongSupplier contentLengthDefault;
30
31
32
33
34 private final Function<String, String> contentLengthString;
35
36
37
38
39 private final T request;
40
41
42
43
44
45
46
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
56
57
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
74
75
76
77 @Override
78 public String toString() {
79 return String.format("%s [ContentLength=%s, ContentType=%s]", getClass().getSimpleName(), getContentLength(), getContentType());
80 }
81
82 }