This repository was archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathServer.java
More file actions
269 lines (245 loc) · 9.37 KB
/
Server.java
File metadata and controls
269 lines (245 loc) · 9.37 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright 2024 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
*
* http://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 feign.form;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT;
import static org.springframework.http.HttpStatus.LOCKED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import lombok.val;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
@SpringBootApplication
@SuppressWarnings("checkstyle:DesignForExtension")
public class Server {
@PostMapping("/form")
public ResponseEntity<Void> form (
@RequestParam("key1") String key1,
@RequestParam("key2") String key2
) {
val status = !key1.equals(key2)
? BAD_REQUEST
: OK;
return ResponseEntity.status(status).body(null);
}
@PostMapping("/upload/{id}")
@ResponseStatus(OK)
public ResponseEntity<Long> upload (
@PathVariable("id") Integer id,
@RequestParam("public") Boolean isPublic,
@RequestParam("file") MultipartFile file
) {
HttpStatus status;
if (id == null || id != 10) {
status = LOCKED;
} else if (isPublic == null || !isPublic) {
status = FORBIDDEN;
} else if (file.getSize() == 0) {
status = I_AM_A_TEAPOT;
} else if (file.getOriginalFilename() == null || file.getOriginalFilename().trim().isEmpty()) {
status = CONFLICT;
} else {
status = OK;
}
return ResponseEntity.status(status).body(file.getSize());
}
@PostMapping("/upload")
public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
HttpStatus status;
if (file.getSize() == 0) {
status = I_AM_A_TEAPOT;
} else if (file.getOriginalFilename() == null || file.getOriginalFilename().trim().isEmpty()) {
status = CONFLICT;
} else {
status = OK;
}
return ResponseEntity.status(status).body(file.getSize());
}
@PostMapping("/upload/files")
public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files) {
HttpStatus status;
if (files[0].getSize() == 0 || files[1].getSize() == 0) {
status = I_AM_A_TEAPOT;
} else if (files[0].getOriginalFilename() == null || files[0].getOriginalFilename().trim().isEmpty() ||
files[1].getOriginalFilename() == null || files[1].getOriginalFilename().trim().isEmpty()) {
status = CONFLICT;
} else {
status = OK;
}
return ResponseEntity.status(status).body(files[0].getSize() + files[1].getSize());
}
@PostMapping(path = "/json", consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<String> json (@RequestBody Dto dto) {
HttpStatus status;
if (!dto.getName().equals("Artem")) {
status = CONFLICT;
} else if (!dto.getAge().equals(11)) {
status = I_AM_A_TEAPOT;
} else {
status = OK;
}
return ResponseEntity.status(status).body("ok");
}
@PostMapping("/query_map")
public ResponseEntity<Integer> queryMap (
@RequestParam("filter") List<String> filters
) {
val status = filters != null && !filters.isEmpty()
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(filters.size());
}
@PostMapping(path = "/wild-card-map", consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Integer> wildCardMap (
@RequestParam("key1") String key1,
@RequestParam("key2") String key2
) {
val status = key1.equals(key2)
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(null);
}
@PostMapping(path = "/upload/with_dto", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Long> uploadWithDto (
Dto dto,
@RequestPart("file") MultipartFile file
) throws IOException {
val status = dto != null && dto.getName().equals("Artem")
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(file.getSize());
}
@PostMapping(path = "/upload/byte_array", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadByteArray (
@RequestPart("file") MultipartFile file
) {
val status = file != null
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(file.getOriginalFilename());
}
@PostMapping(path = "/upload/byte_array_parameter", consumes = MULTIPART_FORM_DATA_VALUE)
// We just want the request because when there's a filename part of the Content-Disposition header spring
// will treat it as a file (available through getFile()) and when it doesn't have the filename part it's
// available in the parameter (getParameter())
public ResponseEntity<String> uploadByteArrayParameter (
MultipartHttpServletRequest request
) {
val status = request.getFile("file") == null && request.getParameter("file") != null
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).build();
}
@PostMapping(path = "/upload/unknown_type", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadUnknownType (
@RequestPart("file") MultipartFile file
) {
val status = file != null
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(file.getContentType());
}
@PostMapping(path = "/upload/form_data", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadFormData (
@RequestPart("file") MultipartFile file
) {
val status = file != null
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(file.getOriginalFilename() + ':' + file.getContentType());
}
@PostMapping(path = "/upload/many_form_data", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadFormData (
@RequestPart("file[]") List<MultipartFile> files
) {
val status = files != null
? OK
: I_AM_A_TEAPOT;
val response = files.stream()
.map(file -> file.getOriginalFilename() + ':' + file.getContentType())
.collect(Collectors.joining(","));
return ResponseEntity.status(status).body(response);
}
@PostMapping(path = "/submit/url", consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> submitRepeatableQueryParam (
@RequestParam("names") String[] names
) {
val response = new StringBuilder();
if (names != null && names.length == 2) {
response
.append(names[0])
.append(" and ")
.append(names[1]);
}
val status = response.length() > 0
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(response.toString());
}
@PostMapping(path = "/submit/form", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> submitRepeatableFormParam (
@RequestParam("names") Collection<String> names
) {
val response = new StringBuilder();
if (names != null && names.size() == 2) {
val iterator = names.iterator();
response
.append(iterator.next())
.append(" and ")
.append(iterator.next());
}
val status = response.length() > 0
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(response.toString());
}
@PostMapping(path = "/form-data", consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> submitPostData (
@RequestParam("f_name") String firstName,
@RequestParam("age") Integer age
) {
val response = new StringBuilder();
if (firstName != null && age != null) {
response
.append(firstName)
.append("=")
.append(age);
}
val status = response.length() > 0
? OK
: I_AM_A_TEAPOT;
return ResponseEntity.status(status).body(response.toString());
}
}