forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultPartEvents.java
176 lines (137 loc) · 4.55 KB
/
DefaultPartEvents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec.multipart;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
/**
* Default implementations of {@link PartEvent} and subtypes.
*
* @author Arjen Poutsma
* @since 6.0
*/
abstract class DefaultPartEvents {
public static FormPartEvent form(HttpHeaders headers) {
Assert.notNull(headers, "Headers must not be null");
return new DefaultFormFieldPartEvent(headers);
}
public static FormPartEvent form(HttpHeaders headers, String value) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(value, "Value must not be null");
return new DefaultFormFieldPartEvent(headers, value);
}
public static FilePartEvent file(HttpHeaders headers, DataBuffer dataBuffer, boolean isLast) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(dataBuffer, "DataBuffer must not be null");
return new DefaultFilePartEvent(headers, dataBuffer, isLast);
}
public static FilePartEvent file(HttpHeaders headers) {
Assert.notNull(headers, "Headers must not be null");
return new DefaultFilePartEvent(headers);
}
public static PartEvent create(HttpHeaders headers, DataBuffer dataBuffer, boolean isLast) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(dataBuffer, "DataBuffer must not be null");
if (headers.getContentDisposition().getFilename() != null) {
return file(headers, dataBuffer, isLast);
}
else {
return new DefaultPartEvent(headers, dataBuffer, isLast);
}
}
public static PartEvent create(HttpHeaders headers) {
Assert.notNull(headers, "Headers must not be null");
if (headers.getContentDisposition().getFilename() != null) {
return file(headers);
}
else {
return new DefaultPartEvent(headers);
}
}
private static abstract class AbstractPartEvent implements PartEvent {
private final HttpHeaders headers;
protected AbstractPartEvent(HttpHeaders headers) {
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
}
@Override
public HttpHeaders headers() {
return this.headers;
}
}
/**
* Default implementation of {@link PartEvent}.
*/
private static class DefaultPartEvent extends AbstractPartEvent {
private static final DataBuffer EMPTY = DefaultDataBufferFactory.sharedInstance.allocateBuffer(0);
private final DataBuffer content;
private final boolean last;
public DefaultPartEvent(HttpHeaders headers) {
this(headers, EMPTY, true);
}
public DefaultPartEvent(HttpHeaders headers, DataBuffer content, boolean last) {
super(headers);
this.content = content;
this.last = last;
}
@Override
public DataBuffer content() {
return this.content;
}
@Override
public boolean isLast() {
return this.last;
}
}
/**
* Default implementation of {@link FormPartEvent}.
*/
private static final class DefaultFormFieldPartEvent extends AbstractPartEvent implements FormPartEvent {
private static final String EMPTY = "";
private final String value;
public DefaultFormFieldPartEvent(HttpHeaders headers) {
this(headers, EMPTY);
}
public DefaultFormFieldPartEvent(HttpHeaders headers, String value) {
super(headers);
this.value = value;
}
@Override
public String value() {
return this.value;
}
@Override
public DataBuffer content() {
byte[] bytes = this.value.getBytes(MultipartUtils.charset(headers()));
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
}
@Override
public boolean isLast() {
return true;
}
}
/**
* Default implementation of {@link FilePartEvent}.
*/
private static class DefaultFilePartEvent extends DefaultPartEvent implements FilePartEvent {
public DefaultFilePartEvent(HttpHeaders headers) {
super(headers);
}
public DefaultFilePartEvent(HttpHeaders headers, DataBuffer content, boolean last) {
super(headers, content, last);
}
}
}