Skip to content

Commit 98f9f7f

Browse files
committed
Value API: remove FileUpload from it
- FileUpload no longer implements the Value contract - simplify usage and extension of both
1 parent 6f683f2 commit 98f9f7f

File tree

20 files changed

+194
-219
lines changed

20 files changed

+194
-219
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.nio.charset.StandardCharsets;
2525
import java.nio.file.Path;
2626
import java.time.Instant;
27-
import java.util.ArrayList;
28-
import java.util.Collections;
2927
import java.util.Date;
3028
import java.util.List;
3129
import java.util.Map;
@@ -259,38 +257,15 @@ public interface DefaultContext extends Context {
259257
}
260258

261259
@Override @Nonnull default List<FileUpload> files() {
262-
ValueNode multipart = multipart();
263-
List<FileUpload> result = new ArrayList<>();
264-
for (ValueNode value : multipart) {
265-
if (value.isUpload()) {
266-
result.add((FileUpload) value);
267-
}
268-
}
269-
return result;
260+
return multipart().files();
270261
}
271262

272263
@Override @Nonnull default List<FileUpload> files(@Nonnull String name) {
273-
ValueNode multipart = multipart(name);
274-
if (multipart instanceof FileUpload) {
275-
return Collections.singletonList((FileUpload) multipart);
276-
}
277-
List<FileUpload> result = new ArrayList<>();
278-
for (ValueNode value : multipart) {
279-
if (value instanceof FileUpload) {
280-
result.add((FileUpload) value);
281-
} else {
282-
throw new TypeMismatchException(name, FileUpload.class);
283-
}
284-
}
285-
return result;
264+
return multipart().files(name);
286265
}
287266

288267
@Override @Nonnull default FileUpload file(@Nonnull String name) {
289-
ValueNode value = multipart(name);
290-
if (value instanceof FileUpload) {
291-
return (FileUpload) value;
292-
}
293-
throw new TypeMismatchException(name, FileUpload.class);
268+
return multipart().file(name);
294269
}
295270

296271
@Override default @Nonnull <T> T body(@Nonnull Class<T> type) {

jooby/src/main/java/io/jooby/FileUpload.java

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.internal.MissingValue;
98

109
import javax.annotation.Nonnull;
1110
import javax.annotation.Nullable;
1211
import java.io.InputStream;
13-
import java.nio.charset.Charset;
14-
import java.nio.charset.StandardCharsets;
1512
import java.nio.file.Path;
16-
import java.util.Collections;
17-
import java.util.HashMap;
18-
import java.util.Iterator;
19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.Set;
2213

2314
/**
2415
* File upload class, file upload are available when request body is encoded as
@@ -39,7 +30,7 @@
3930
* @since 2.0.0
4031
* @author edgar
4132
*/
42-
public interface FileUpload extends ValueNode {
33+
public interface FileUpload {
4334
/**
4435
* Name of file upload.
4536
* @return Name of file upload.
@@ -53,66 +44,6 @@ public interface FileUpload extends ValueNode {
5344
*/
5445
@Nullable String getContentType();
5546

56-
@Override default ValueNode get(@Nonnull int index) {
57-
return index == 0 ? this : get(Integer.toString(index));
58-
}
59-
60-
@Override default ValueNode get(@Nonnull String name) {
61-
return new MissingValue(name);
62-
}
63-
64-
@Override default int size() {
65-
return 1;
66-
}
67-
68-
@Nonnull @Override default Iterator<ValueNode> iterator() {
69-
Iterator iterator = Collections.singletonList(this).iterator();
70-
return iterator;
71-
}
72-
73-
@Override default @Nonnull String value() {
74-
return value(StandardCharsets.UTF_8);
75-
}
76-
77-
/**
78-
* File upload content as string.
79-
*
80-
* @param charset Charset.
81-
* @return Content as string.
82-
*/
83-
default @Nonnull String value(@Nonnull Charset charset) {
84-
return new String(bytes(), charset);
85-
}
86-
87-
default @Nonnull @Override <T> T to(@Nonnull Class<T> type) {
88-
if (Path.class == type) {
89-
return (T) this.path();
90-
}
91-
if (FileUpload.class == type) {
92-
return (T) this;
93-
}
94-
throw new TypeMismatchException(name(), type);
95-
}
96-
97-
default @Nonnull @Override List<String> toList() {
98-
return Collections.singletonList(value());
99-
}
100-
101-
default @Nonnull @Override Set<String> toSet() {
102-
return Collections.singleton(value());
103-
}
104-
105-
/**
106-
* Multi-value map with field name as key and file name as values.
107-
*
108-
* @return Multi-value map with field name as key and file name as values.
109-
*/
110-
@Override default @Nonnull Map<String, List<String>> toMultimap() {
111-
Map<String, List<String>> result = new HashMap<>(1);
112-
result.put(name(), Collections.singletonList(getFileName()));
113-
return result;
114-
}
115-
11647
/**
11748
* Content as input stream.
11849
*

jooby/src/main/java/io/jooby/Formdata.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,24 @@ public interface Formdata extends ValueNode {
2525
*
2626
* @param path Form name/path.
2727
* @param value Form value.
28-
* @return This formdata.
2928
*/
30-
@Nonnull Formdata put(@Nonnull String path, @Nonnull ValueNode value);
29+
@Nonnull void put(@Nonnull String path, @Nonnull ValueNode value);
3130

3231
/**
3332
* Add a form field.
3433
*
3534
* @param path Form name/path.
3635
* @param value Form value.
37-
* @return This formdata.
3836
*/
39-
@Nonnull Formdata put(@Nonnull String path, @Nonnull String value);
37+
@Nonnull void put(@Nonnull String path, @Nonnull String value);
4038

4139
/**
4240
* Add a form field.
4341
*
4442
* @param path Form name/path.
4543
* @param values Form values.
46-
* @return This formdata.
4744
*/
48-
@Nonnull Formdata put(@Nonnull String path, @Nonnull Collection<String> values);
45+
@Nonnull void put(@Nonnull String path, @Nonnull Collection<String> values);
4946

5047
/**
5148
* Creates a formdata object.

jooby/src/main/java/io/jooby/Multipart.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.internal.HashValue;
8+
import io.jooby.internal.MultipartNode;
99

1010
import javax.annotation.Nonnull;
11+
import java.util.List;
1112

1213
/**
1314
* Multipart class for direct MVC parameter provisioning.
@@ -19,13 +20,42 @@
1920
*/
2021
public interface Multipart extends Formdata {
2122

23+
void put(String name, FileUpload file);
24+
25+
/**
26+
* All file uploads. Only for <code>multipart/form-data</code> request.
27+
*
28+
* @return All file uploads.
29+
*/
30+
@Nonnull List<FileUpload> files();
31+
32+
/**
33+
* All file uploads that matches the given field name.
34+
*
35+
* Only for <code>multipart/form-data</code> request.
36+
*
37+
* @param name Field name. Please note this is the form field name, not the actual file name.
38+
* @return All file uploads.
39+
*/
40+
@Nonnull List<FileUpload> files(@Nonnull String name);
41+
42+
/**
43+
* A file upload that matches the given field name.
44+
*
45+
* Only for <code>multipart/form-data</code> request.
46+
*
47+
* @param name Field name. Please note this is the form field name, not the actual file name.
48+
* @return A file upload.
49+
*/
50+
@Nonnull FileUpload file(@Nonnull String name);
51+
2252
/**
2353
* Creates a new multipart object.
2454
*
2555
* @param ctx Current context.
2656
* @return Multipart instance.
2757
*/
2858
static @Nonnull Multipart create(@Nonnull Context ctx) {
29-
return new HashValue(ctx, null);
59+
return new MultipartNode(ctx, null);
3060
}
3161
}

jooby/src/main/java/io/jooby/Value.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,6 @@ default boolean isObject() {
335335
return this instanceof HashValue;
336336
}
337337

338-
/**
339-
* True if this is a file upload (not single, not array, not hash).
340-
*
341-
* @return True for file upload.
342-
*/
343-
default boolean isUpload() {
344-
return this instanceof FileUpload;
345-
}
346-
347338
/**
348339
* Name of this value or <code>null</code>.
349340
*
@@ -439,7 +430,8 @@ default boolean isUpload() {
439430
* @param value Value.
440431
* @return Single value.
441432
*/
442-
static @Nonnull ValueNode value(@Nonnull Context ctx, @Nonnull String name, @Nonnull String value) {
433+
static @Nonnull ValueNode value(@Nonnull Context ctx, @Nonnull String name,
434+
@Nonnull String value) {
443435
return new SingleValue(ctx, name, value);
444436
}
445437

@@ -469,7 +461,8 @@ default boolean isUpload() {
469461
* @param values Field values.
470462
* @return A value.
471463
*/
472-
static @Nonnull ValueNode create(Context ctx, @Nonnull String name, @Nullable List<String> values) {
464+
static @Nonnull ValueNode create(Context ctx, @Nonnull String name,
465+
@Nullable List<String> values) {
473466
if (values == null || values.size() == 0) {
474467
return missing(name);
475468
}
@@ -506,6 +499,8 @@ default boolean isUpload() {
506499
* @return A hash/object value.
507500
*/
508501
static @Nonnull ValueNode hash(Context ctx, @Nonnull Map<String, Collection<String>> values) {
509-
return new HashValue(ctx, null).put(values);
502+
HashValue node = new HashValue(ctx, null);
503+
node.put(values);
504+
return node;
510505
}
511506
}

jooby/src/main/java/io/jooby/internal/HashValue.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.TreeMap;
2727
import java.util.function.BiConsumer;
2828

29-
public class HashValue implements ValueNode, Multipart {
29+
public class HashValue implements ValueNode, Formdata {
3030
private static final Map<String, ValueNode> EMPTY = Collections.emptyMap();
3131

3232
private Context ctx;
@@ -49,15 +49,15 @@ protected HashValue(Context ctx) {
4949
return name;
5050
}
5151

52-
public Formdata put(String path, String value) {
53-
return put(path, Collections.singletonList(value));
52+
public void put(String path, String value) {
53+
put(path, Collections.singletonList(value));
5454
}
5555

56-
public HashValue put(String path, ValueNode upload) {
56+
public void put(String path, ValueNode node) {
5757
put(path, (name, scope) -> {
5858
ValueNode existing = scope.get(name);
5959
if (existing == null) {
60-
scope.put(name, upload);
60+
scope.put(name, node);
6161
} else {
6262
ArrayValue list;
6363
if (existing instanceof ArrayValue) {
@@ -66,13 +66,12 @@ public HashValue put(String path, ValueNode upload) {
6666
list = new ArrayValue(ctx, name).add(existing);
6767
scope.put(name, list);
6868
}
69-
list.add(upload);
69+
list.add(node);
7070
}
7171
});
72-
return this;
7372
}
7473

75-
public HashValue put(String path, Collection<String> values) {
74+
public void put(String path, Collection<String> values) {
7675
put(path, (name, scope) -> {
7776
for (String value : values) {
7877
ValueNode existing = scope.get(name);
@@ -90,7 +89,6 @@ public HashValue put(String path, Collection<String> values) {
9089
}
9190
}
9291
});
93-
return this;
9492
}
9593

9694
private void put(String path, BiConsumer<String, Map<String, ValueNode>> consumer) {
@@ -233,11 +231,9 @@ public int size() {
233231
String scope = name == null ? "" : name + ".";
234232
for (Map.Entry<String, ValueNode> entry : entries) {
235233
ValueNode value = entry.getValue();
236-
if (!value.isUpload()) {
237-
value.toMultimap().forEach((k, v) -> {
238-
result.put(scope + k, v);
239-
});
240-
}
234+
value.toMultimap().forEach((k, v) -> {
235+
result.put(scope + k, v);
236+
});
241237
}
242238
return result;
243239
}
@@ -246,11 +242,10 @@ public int size() {
246242
return hash.toString();
247243
}
248244

249-
public HashValue put(Map<String, Collection<String>> headers) {
245+
public void put(Map<String, Collection<String>> headers) {
250246
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
251247
put(entry.getKey(), entry.getValue());
252248
}
253-
return this;
254249
}
255250

256251
private <T, C extends Collection<T>> C toCollection(@Nonnull Class<T> type, C collection) {

0 commit comments

Comments
 (0)