-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathBody.java
More file actions
189 lines (164 loc) · 4.17 KB
/
Copy pathBody.java
File metadata and controls
189 lines (164 loc) · 4.17 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
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.jspecify.annotations.Nullable;
import io.jooby.exception.MissingValueException;
import io.jooby.internal.ByteArrayBody;
import io.jooby.internal.FileBody;
import io.jooby.internal.InputStreamBody;
import io.jooby.value.Value;
/**
* HTTP body value. Allows to access HTTP body as string, byte[], stream, etc..
*
* <p>HTTP body can be read it only once per request. Attempt to read more than one resulted in
* unexpected behaviour.
*
* @author edgar
* @since 2.0.0
*/
public interface Body extends Value {
/**
* HTTP body as string.
*
* @param charset Charset.
* @return Body as string.
*/
default String value(Charset charset) {
byte[] bytes = bytes();
if (bytes.length == 0) {
throw new MissingValueException("body");
}
return new String(bytes, charset);
}
@Override
default int size() {
return 1;
}
@Override
default Value get(int index) {
return get(Integer.toString(index));
}
@Override
default Iterator<Value> iterator() {
return List.of((Value) this).iterator();
}
/**
* HTTP body as byte array.
*
* @return Body as byte array.
*/
byte[] bytes();
/**
* True if body is on memory. False, indicates body is on file system. Body larger than {@link
* ServerOptions#getMaxRequestSize()} will be dump to disk.
*
* @return True if body is on memory. False, indicates body is on file system.
*/
boolean isInMemory();
/**
* Size in bytes. This is the same as <code>Content-Length</code> header.
*
* @return Size in bytes. This is the same as <code>Content-Length</code> header.
*/
long getSize();
/**
* Body as a readable channel.
*
* @return Body as a readable channel.
*/
ReadableByteChannel channel();
/**
* Body as input stream.
*
* @return Body as input stream.
*/
InputStream stream();
@Override
default <T> List<T> toList(Class<T> type) {
return to(Reified.list(type).getType());
}
default @Override List<String> toList() {
return List.of(value());
}
default @Override Set<String> toSet() {
return Set.of(value());
}
@Override
default <T> T to(Class<T> type) {
return to((Type) type);
}
default @Nullable @Override <T> T toNullable(Class<T> type) {
return toNullable((Type) type);
}
/**
* Convert this body into the given type.
*
* @param type Type to use.
* @param <T> Generic type.
* @return Converted value.
*/
<T> T to(Type type);
/**
* Convert this body into the given type.
*
* @param type Type to use.
* @param <T> Generic type.
* @return Converted value or <code>null</code>.
*/
@Nullable <T> T toNullable(Type type);
/* **********************************************************************************************
* Factory methods:
* **********************************************************************************************
*/
/**
* Empty body.
*
* @param ctx Current context.
* @return Empty body.
*/
static Body empty(Context ctx) {
return ByteArrayBody.empty(ctx);
}
/**
* Creates a HTTP body from input stream.
*
* @param ctx Current context.
* @param stream Input stream.
* @param size Size in bytes or <code>-1</code>.
* @return A new body.
*/
static Body of(Context ctx, InputStream stream, long size) {
return new InputStreamBody(ctx, stream, size);
}
/**
* Creates a HTTP body from byte array.
*
* @param ctx Current context.
* @param bytes byte array.
* @return A new body.
*/
static Body of(Context ctx, byte[] bytes) {
return new ByteArrayBody(ctx, bytes);
}
/**
* Creates a HTTP body from file.
*
* @param ctx Current context.
* @param file File.
* @return A new body.
*/
static Body of(Context ctx, Path file) {
return new FileBody(ctx, file);
}
}