Repository navigation
Expand file tree
/
Copy pathAbstractTusExtension.java
More file actions
177 lines (157 loc) · 5.23 KB
/
Copy pathAbstractTusExtension.java
File metadata and controls
177 lines (157 loc) · 5.23 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
package me.desair.tus.server.util;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import me.desair.tus.server.HttpMethod;
import me.desair.tus.server.HttpProblemDetails;
import me.desair.tus.server.ProtocolVersion;
import me.desair.tus.server.RequestHandler;
import me.desair.tus.server.RequestValidator;
import me.desair.tus.server.TusExtension;
import me.desair.tus.server.exception.TusException;
import me.desair.tus.server.upload.UploadLockingService;
import me.desair.tus.server.upload.UploadStorageService;
/** Abstract class to implement a tus extension using validators and request handlers. */
public abstract class AbstractTusExtension implements TusExtension {
private List<RequestValidator> requestValidators = new LinkedList<>();
private List<RequestHandler> requestHandlers = new LinkedList<>();
protected AbstractTusExtension() {
initValidators(requestValidators);
initRequestHandlers(requestHandlers);
}
protected abstract void initValidators(List<RequestValidator> requestValidators);
protected abstract void initRequestHandlers(List<RequestHandler> requestHandlers);
@Override
public void validate(
HttpMethod method,
HttpServletRequest servletRequest,
UploadStorageService uploadStorageService,
String ownerKey)
throws TusException, IOException {
validate(
method, servletRequest, uploadStorageService, null, ownerKey, ProtocolVersion.TUS_1_0_0);
}
@Override
public void validate(
HttpMethod method,
HttpServletRequest servletRequest,
UploadStorageService uploadStorageService,
UploadLockingService uploadLockingService,
String ownerKey,
ProtocolVersion version)
throws TusException, IOException {
if (!isApplicable(method, version)) {
return;
}
for (RequestValidator requestValidator : requestValidators) {
if (requestValidator.supports(method, version)) {
requestValidator.validate(method, servletRequest, uploadStorageService, ownerKey);
}
}
}
@Override
public void process(
HttpMethod method,
TusServletRequest servletRequest,
TusServletResponse servletResponse,
UploadStorageService uploadStorageService,
String ownerKey)
throws IOException, TusException {
UploadLockingService lockingService = Utils.getLockingServiceOrDefault(uploadStorageService);
process(
method,
servletRequest,
servletResponse,
uploadStorageService,
lockingService,
ownerKey,
ProtocolVersion.TUS_1_0_0);
}
@Override
public void process(
HttpMethod method,
TusServletRequest servletRequest,
TusServletResponse servletResponse,
UploadStorageService uploadStorageService,
UploadLockingService uploadLockingService,
String ownerKey,
ProtocolVersion version)
throws IOException, TusException {
if (!isApplicable(method, version)) {
return;
}
UploadLockingService lockingServiceToUse =
Utils.getLockingServiceOrDefault(uploadLockingService, uploadStorageService);
for (RequestHandler requestHandler : requestHandlers) {
if (requestHandler.supports(method, version)) {
requestHandler.process(
method,
servletRequest,
servletResponse,
uploadStorageService,
lockingServiceToUse,
ownerKey,
null);
}
}
}
@Override
public void handleError(
HttpMethod method,
TusServletRequest request,
TusServletResponse response,
UploadStorageService uploadStorageService,
String ownerKey)
throws IOException, TusException {
UploadLockingService lockingService = Utils.getLockingServiceOrDefault(uploadStorageService);
HttpProblemDetails pd =
handleError(
method,
request,
response,
uploadStorageService,
lockingService,
ownerKey,
ProtocolVersion.TUS_1_0_0,
null);
if (pd != null) {
pd.writeTo(response);
}
}
@Override
public HttpProblemDetails handleError(
HttpMethod method,
TusServletRequest request,
TusServletResponse response,
UploadStorageService uploadStorageService,
UploadLockingService uploadLockingService,
String ownerKey,
ProtocolVersion version,
TusException exception)
throws IOException, TusException {
if (!isApplicable(method, version)) {
return null;
}
UploadLockingService lockingServiceToUse =
Utils.getLockingServiceOrDefault(uploadLockingService, uploadStorageService);
HttpProblemDetails problemDetails = null;
for (RequestHandler requestHandler : requestHandlers) {
if (requestHandler.supports(method, version) && requestHandler.isErrorHandler()) {
HttpProblemDetails pd =
requestHandler.process(
method,
request,
response,
uploadStorageService,
lockingServiceToUse,
ownerKey,
exception);
if (pd != null) {
problemDetails = pd;
}
}
}
return problemDetails;
}
}