Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Labazin committed Jun 25, 2024
1 parent 3c3951d commit 1577990
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 63 deletions.
65 changes: 30 additions & 35 deletions feign-form-spring/src/test/java/feign/form/feign/spring/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.util.Map;

import lombok.SneakyThrows;
import lombok.val;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
Expand All @@ -39,9 +36,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -53,55 +51,53 @@
@SuppressWarnings("checkstyle:DesignForExtension")
public class Server {

@RequestMapping(
@PostMapping(
path = "/multipart/upload1/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
@SneakyThrows
public String upload1 (@PathVariable("folder") String folder,
@RequestPart("file") MultipartFile file,
@RequestParam(value = "message", required = false) String message
) {
public String upload1 (
@PathVariable("folder") String folder,
@RequestPart("file") MultipartFile file,
@RequestParam(value = "message", required = false) String message
) throws IOException {
return new String(file.getBytes()) + ':' + message + ':' + folder;
}

@RequestMapping(
@PostMapping(
path = "/multipart/upload2/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
@SneakyThrows
public String upload2 (@RequestBody MultipartFile file,
@PathVariable("folder") String folder,
@RequestParam(value = "message", required = false) String message
) {
public String upload2 (
@RequestBody MultipartFile file,
@PathVariable("folder") String folder,
@RequestParam(value = "message", required = false) String message
) throws IOException {
return new String(file.getBytes()) + ':' + message + ':' + folder;
}

@RequestMapping(
@PostMapping(
path = "/multipart/upload3/{folder}",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public String upload3 (@RequestBody MultipartFile file,
@PathVariable("folder") String folder,
@RequestParam(value = "message", required = false) String message
public String upload3 (
@RequestBody MultipartFile file,
@PathVariable("folder") String folder,
@RequestParam(value = "message", required = false) String message
) {
return file.getOriginalFilename() + ':' + file.getContentType() + ':' + folder;
}

@RequestMapping(path = "/multipart/upload4/{id}", method = POST)
public String upload4 (@PathVariable("id") String id,
@RequestBody Map<String, Object> map,
@RequestParam String userName
@PostMapping("/multipart/upload4/{id}")
public String upload4 (
@PathVariable("id") String id,
@RequestBody Map<String, Object> map,
@RequestParam String userName
) {
return userName + ':' + id + ':' + map.size();
}

@RequestMapping(
@PostMapping(
path = "/multipart/upload5",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
void upload5 (Dto dto) throws IOException {
Expand All @@ -111,13 +107,13 @@ void upload5 (Dto dto) throws IOException {
assert "Hello world".equals(new String(dto.getFile().getBytes(), UTF_8));
}

@RequestMapping(
@PostMapping(
path = "/multipart/upload6",
method = POST,
consumes = MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa1,
@RequestParam("popa2") MultipartFile popa2
public ResponseEntity<String> upload6 (
@RequestParam("popa1") MultipartFile popa1,
@RequestParam("popa2") MultipartFile popa2
) throws Exception {
HttpStatus status = I_AM_A_TEAPOT;
String result = "";
Expand All @@ -128,9 +124,8 @@ public ResponseEntity<String> upload6 (@RequestParam("popa1") MultipartFile popa
return ResponseEntity.status(status).body(result);
}

@RequestMapping(
@GetMapping(
path = "/multipart/download/{fileId}",
method = GET,
produces = MULTIPART_FORM_DATA_VALUE
)
public MultiValueMap<String, Object> download (@PathVariable("fileId") String fileId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.net.URLConnection;

import lombok.SneakyThrows;
import lombok.val;

import feign.codec.EncodeException;
Expand Down Expand Up @@ -63,7 +62,6 @@ protected void write (Output output, String key, Object value) throws EncodeExce
* @param fileName file name.
* @param contentType type of file content. May be the {@code null}, in that case it will be determined by file name.
*/
@SneakyThrows
protected void writeFileMetadata (Output output, String name, String fileName, String contentType) {
val contentDespositionBuilder = new StringBuilder()
.append("Content-Disposition: form-data; name=\"").append(name).append("\"");
Expand Down
1 change: 0 additions & 1 deletion feign-form/src/main/java/feign/form/multipart/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public Output write (byte[] bytes) {
*
* @return this output
*/
@SneakyThrows
public Output write (byte[] bytes, int offset, int length) {
outputStream.write(bytes, offset, length);
return this;
Expand Down
72 changes: 47 additions & 25 deletions feign-form/src/test/java/feign/form/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.util.Collection;
Expand All @@ -36,10 +35,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -51,21 +50,23 @@
@SuppressWarnings("checkstyle:DesignForExtension")
public class Server {

@RequestMapping(path = "/form", method = POST)
public ResponseEntity<Void> form (@RequestParam("key1") String key1,
@RequestParam("key2") String key2
@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);
}

@RequestMapping(path = "/upload/{id}", method = POST)
@PostMapping("/upload/{id}")
@ResponseStatus(OK)
public ResponseEntity<Long> upload (@PathVariable("id") Integer id,
@RequestParam("public") Boolean isPublic,
@RequestParam("file") MultipartFile file
public ResponseEntity<Long> upload (
@PathVariable("id") Integer id,
@RequestParam("public") Boolean isPublic,
@RequestParam("file") MultipartFile file
) {
HttpStatus status;
if (id == null || id != 10) {
Expand All @@ -82,7 +83,7 @@ public ResponseEntity<Long> upload (@PathVariable("id") Integer id,
return ResponseEntity.status(status).body(file.getSize());
}

@RequestMapping(path = "/upload", method = POST)
@PostMapping("/upload")
public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
HttpStatus status;
if (file.getSize() == 0) {
Expand All @@ -95,7 +96,7 @@ public ResponseEntity<Long> upload (@RequestParam("file") MultipartFile file) {
return ResponseEntity.status(status).body(file.getSize());
}

@RequestMapping(path = "/upload/files", method = POST)
@PostMapping("/upload/files")
public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files) {
HttpStatus status;
if (files[0].getSize() == 0 || files[1].getSize() == 0) {
Expand All @@ -109,7 +110,7 @@ public ResponseEntity<Long> upload (@RequestParam("files") MultipartFile[] files
return ResponseEntity.status(status).body(files[0].getSize() + files[1].getSize());
}

@RequestMapping(path = "/json", method = POST, consumes = APPLICATION_JSON_VALUE)
@PostMapping(path = "/json", consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<String> json (@RequestBody Dto dto) {
HttpStatus status;
if (!dto.getName().equals("Artem")) {
Expand All @@ -122,24 +123,31 @@ public ResponseEntity<String> json (@RequestBody Dto dto) {
return ResponseEntity.status(status).body("ok");
}

@RequestMapping("/query_map")
public ResponseEntity<Integer> queryMap (@RequestParam("filter") List<String> filters) {
@GetMapping("/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());
}

@RequestMapping(path = "/wild-card-map", method = POST, consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Integer> wildCardMap (@RequestParam("key1") String key1, @RequestParam("key2") String key2) {
@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
public ResponseEntity<Long> uploadWithDto (
Dto dto,
@RequestPart("file") MultipartFile file
) throws IOException {
val status = dto != null && dto.getName().equals("Artem")
? OK
Expand All @@ -148,7 +156,9 @@ public ResponseEntity<Long> uploadWithDto (Dto dto, @RequestPart("file") Multipa
}

@PostMapping(path = "/upload/byte_array", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadByteArray (@RequestPart("file") MultipartFile file) {
public ResponseEntity<String> uploadByteArray (
@RequestPart("file") MultipartFile file
) {
val status = file != null
? OK
: I_AM_A_TEAPOT;
Expand All @@ -159,31 +169,39 @@ public ResponseEntity<String> uploadByteArray (@RequestPart("file") MultipartFil
// 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) {
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) {
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) {
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 = "/submit/url", consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> submitRepeatableQueryParam (@RequestParam("names") String[] names) {
public ResponseEntity<String> submitRepeatableQueryParam (
@RequestParam("names") String[] names
) {
val response = new StringBuilder();
if (names != null && names.length == 2) {
response
Expand All @@ -199,7 +217,9 @@ public ResponseEntity<String> submitRepeatableQueryParam (@RequestParam("names")
}

@PostMapping(path = "/submit/form", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> submitRepeatableFormParam (@RequestParam("names") Collection<String> names) {
public ResponseEntity<String> submitRepeatableFormParam (
@RequestParam("names") Collection<String> names
) {
val response = new StringBuilder();
if (names != null && names.size() == 2) {
val iterator = names.iterator();
Expand All @@ -216,8 +236,10 @@ public ResponseEntity<String> submitRepeatableFormParam (@RequestParam("names")
}

@PostMapping(path = "/form-data", consumes = APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> submitPostData (@RequestParam("f_name") String firstName,
@RequestParam("age") Integer age) {
public ResponseEntity<String> submitPostData (
@RequestParam("f_name") String firstName,
@RequestParam("age") Integer age
) {
val response = new StringBuilder();
if (firstName != null && age != null) {
response
Expand Down

0 comments on commit 1577990

Please sign in to comment.