/
DefaultParts.java
212 lines (172 loc) · 5.29 KB
/
DefaultParts.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2002-2020 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 java.nio.file.Path;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
/**
* Default implementations of {@link Part} and subtypes.
*
* @author Arjen Poutsma
* @since 5.3
*/
abstract class DefaultParts {
/**
* Create a new {@link FormFieldPart} with the given parameters.
* @param headers the part headers
* @param value the form field value
* @return the created part
*/
public static FormFieldPart formFieldPart(HttpHeaders headers, String value) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(value, "Value must not be null");
return new DefaultFormFieldPart(headers, value);
}
/**
* Create a new {@link Part} or {@link FilePart} with the given parameters.
* Returns {@link FilePart} if the {@code Content-Disposition} of the given
* headers contains a filename, or a "normal" {@link Part} otherwise
* @param headers the part headers
* @param content the content of the part
* @return {@link Part} or {@link FilePart}, depending on {@link HttpHeaders#getContentDisposition()}
*/
public static Part part(HttpHeaders headers, Content content) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(content, "Content must not be null");
String filename = headers.getContentDisposition().getFilename();
if (filename != null) {
return new DefaultFilePart(headers, content);
}
else {
return new DefaultPart(headers, content);
}
}
/**
* Abstract base class.
*/
private static abstract class AbstractPart implements Part {
private final HttpHeaders headers;
protected AbstractPart(HttpHeaders headers) {
Assert.notNull(headers, "HttpHeaders is required");
this.headers = headers;
}
@Override
public String name() {
String name = headers().getContentDisposition().getName();
Assert.state(name != null, "No name available");
return name;
}
@Override
public HttpHeaders headers() {
return this.headers;
}
}
/**
* Default implementation of {@link FormFieldPart}.
*/
private static class DefaultFormFieldPart extends AbstractPart implements FormFieldPart {
private final String value;
public DefaultFormFieldPart(HttpHeaders headers, String value) {
super(headers);
this.value = value;
}
@Override
public Flux<DataBuffer> content() {
return Flux.defer(() -> {
byte[] bytes = this.value.getBytes(MultipartUtils.charset(headers()));
return Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
});
}
@Override
public String value() {
return this.value;
}
@Override
public String toString() {
String name = headers().getContentDisposition().getName();
if (name != null) {
return "DefaultFormFieldPart{" + name() + "}";
}
else {
return "DefaultFormFieldPart";
}
}
}
/**
* Default implementation of {@link Part}.
*/
private static class DefaultPart extends AbstractPart {
private final Content content;
public DefaultPart(HttpHeaders headers, Content content) {
super(headers);
this.content = content;
}
@Override
public Flux<DataBuffer> content() {
return this.content.content();
}
@Override
public Mono<Void> delete() {
return this.content.delete();
}
@Override
public String toString() {
String name = headers().getContentDisposition().getName();
if (name != null) {
return "DefaultPart{" + name + "}";
}
else {
return "DefaultPart";
}
}
}
/**
* Default implementation of {@link FilePart}.
*/
private static final class DefaultFilePart extends DefaultPart implements FilePart {
public DefaultFilePart(HttpHeaders headers, Content content) {
super(headers, content);
}
@Override
public String filename() {
String filename = this.headers().getContentDisposition().getFilename();
Assert.state(filename != null, "No filename found");
return filename;
}
@Override
public Mono<Void> transferTo(Path dest) {
return DataBufferUtils.write(content(), dest);
}
@Override
public String toString() {
ContentDisposition contentDisposition = headers().getContentDisposition();
String name = contentDisposition.getName();
String filename = contentDisposition.getFilename();
if (name != null) {
return "DefaultFilePart{" + name() + " (" + filename + ")}";
}
else {
return "DefaultFilePart{(" + filename + ")}";
}
}
}
}