-
Notifications
You must be signed in to change notification settings - Fork 55
/
ApiController.java
85 lines (72 loc) · 2.94 KB
/
ApiController.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
package com.itranswarp.summer.web.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itranswarp.summer.annotation.GetMapping;
import com.itranswarp.summer.annotation.PathVariable;
import com.itranswarp.summer.annotation.PostMapping;
import com.itranswarp.summer.annotation.RequestBody;
import com.itranswarp.summer.annotation.RequestParam;
import com.itranswarp.summer.annotation.ResponseBody;
import com.itranswarp.summer.annotation.RestController;
import com.itranswarp.summer.web.utils.JsonUtils;
import jakarta.servlet.http.HttpServletResponse;
@RestController
public class ApiController {
final Logger logger = LoggerFactory.getLogger(getClass());
@GetMapping("/api/hello/{name}")
@ResponseBody
String hello(@PathVariable("name") String name) {
return JsonUtils.writeJson(Map.of("name", name));
}
@GetMapping("/api/greeting")
Map<String, Object> greeting(@RequestParam(value = "action", defaultValue = "Hello") String action, @RequestParam("name") String name) {
return Map.of("action", Map.of("name", name));
}
@GetMapping("/api/download/{file}")
FileObj download(@PathVariable("file") String file, @RequestParam("time") Float downloadTime, @RequestParam("md5") String md5,
@RequestParam("length") int length, @RequestParam("hasChecksum") boolean checksum) {
var f = new FileObj();
f.file = file;
f.length = length;
f.downloadTime = downloadTime;
f.md5 = md5;
f.content = "A".repeat(length).getBytes(StandardCharsets.UTF_8);
return f;
}
@GetMapping("/api/download-part")
void downloadPart(@RequestParam("file") String file, @RequestParam("time") Float downloadTime, @RequestParam("md5") String md5,
@RequestParam("length") int length, @RequestParam("hasChecksum") boolean checksum, HttpServletResponse resp) throws IOException {
var f = new FileObj();
f.file = file;
f.length = length;
f.downloadTime = downloadTime;
f.md5 = md5;
f.content = "A".repeat(length).getBytes(StandardCharsets.UTF_8);
resp.setContentType("application/json");
PrintWriter pw = resp.getWriter();
JsonUtils.writeJson(pw, f);
pw.flush();
}
@PostMapping("/api/register")
void register(@RequestBody SigninObj signin, HttpServletResponse resp) throws IOException {
resp.setContentType("application/json");
PrintWriter pw = resp.getWriter();
pw.write("[\"" + signin.name + "\",true,12345]");
pw.flush();
}
public static class FileObj {
public String file;
public int length;
public Float downloadTime;
public String md5;
public byte[] content;
}
public static class SigninObj {
public String name;
public String password;
}
}