1
+ /*
2
+ * Copyright 2013 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package ratpack .core .form ;
18
+
19
+ import ratpack .core .handling .Context ;
20
+ import ratpack .core .parse .Parse ;
21
+ import ratpack .func .Nullable ;
22
+ import ratpack .func .MultiValueMap ;
23
+
24
+ import java .util .List ;
25
+
26
+ /**
27
+ * An uploaded form.
28
+ * <p>
29
+ * The form is modelled as a {@link MultiValueMap}, with extra methods for dealing with file uploads.
30
+ * That is, uploaded files are not visible via the methods provided by {@link MultiValueMap}.
31
+ * <p>
32
+ * All instances of this type are <b>immutable</b>.
33
+ * Calling any mutative method of {@link MultiValueMap} will result in an {@link UnsupportedOperationException}.
34
+ * <h3>Example usage:</h3>
35
+ * <pre class="java">{@code
36
+ * import ratpack.core.handling.Handler;
37
+ * import ratpack.core.handling.Context;
38
+ * import ratpack.core.form.Form;
39
+ * import ratpack.core.form.UploadedFile;
40
+ *
41
+ * import java.util.List;
42
+ *
43
+ * public class Example {
44
+ * public static class FormHandler implements Handler {
45
+ * public void handle(Context context) throws Exception {
46
+ * context.parse(Form.class).then(form -> {
47
+ * UploadedFile file = form.file("someFile.txt");
48
+ * String param = form.get("param");
49
+ * List<String> multi = form.getAll("multi");
50
+ * context.render("form uploaded!");
51
+ * });
52
+ * }
53
+ * }
54
+ * }
55
+ * }</pre>
56
+ *
57
+ * <p>
58
+ * To include the query parameters from the request in the parsed form, use {@link Form#form(boolean)}.
59
+ * This can be useful if you want to support both {@code GET} and {@code PUT} submission with a single handler.
60
+ */
61
+ public interface Form extends MultiValueMap <String , String > {
62
+
63
+ /**
64
+ * Return the first uploaded file with the given name.
65
+ *
66
+ * @param name The name of the uploaded file in the form
67
+ * @return The uploaded file, or {@code null} if no file was uploaded by that name
68
+ */
69
+ @ Nullable
70
+ UploadedFile file (String name );
71
+
72
+ /**
73
+ * Return all of the uploaded files with the given name.
74
+ *
75
+ * @param name The name of the uploaded files in the form
76
+ * @return The uploaded files, or an empty list if no files were uploaded by that name
77
+ */
78
+ List <UploadedFile > files (String name );
79
+
80
+ /**
81
+ * Returns all of the uploaded files.
82
+ *
83
+ * @return all of the uploaded files.
84
+ */
85
+ MultiValueMap <String , UploadedFile > files ();
86
+
87
+ /**
88
+ * Creates a {@link Context#parse parseable object} to parse a request body into a {@link Form}.
89
+ * <p>
90
+ * Default options will be used (no query parameters included).
91
+ *
92
+ * @return a parse object
93
+ */
94
+ static Parse <Form , FormParseOpts > form () {
95
+ return null ;
96
+ }
97
+
98
+ /**
99
+ * Creates a {@link Context#parse parseable object} to parse a request body into a {@link Form}.
100
+ *
101
+ * @param includeQueryParams whether to include the query parameters from the request in the parsed form
102
+ * @return a parse object
103
+ */
104
+ static Parse <Form , FormParseOpts > form (boolean includeQueryParams ) {
105
+ return null ;
106
+ }
107
+
108
+ }
0 commit comments