forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBody.java
More file actions
157 lines (138 loc) · 3.82 KB
/
Body.java
File metadata and controls
157 lines (138 loc) · 3.82 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.exception.MissingValueException;
import io.jooby.internal.ByteArrayBody;
import io.jooby.internal.FileBody;
import io.jooby.internal.InputStreamBody;
import javax.annotation.Nonnull;
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.Collections;
import java.util.List;
import java.util.Set;
/**
* HTTP body value. Allows to access HTTP body as string, byte[], stream, etc..
*
* 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 ValueNode {
/**
* HTTP body as string.
*
* @param charset Charset.
* @return Body as string.
*/
default @Nonnull String value(@Nonnull Charset charset) {
byte[] bytes = bytes();
if (bytes.length == 0) {
throw new MissingValueException("body");
}
return new String(bytes, charset);
}
/**
* HTTP body as byte array.
*
* @return Body as byte array.
*/
@Nonnull 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 readable channel.
*
* @return Body as readable channel.
*/
@Nonnull ReadableByteChannel channel();
/**
* Body as input stream.
*
* @return Body as input stream.
*/
@Nonnull InputStream stream();
@Nonnull @Override default <T> List<T> toList(@Nonnull Class<T> type) {
return to(Reified.list(type).getType());
}
default @Nonnull @Override List<String> toList() {
return Collections.singletonList(value());
}
default @Nonnull @Override Set<String> toSet() {
return Collections.singleton(value());
}
@Override default @Nonnull <T> T to(@Nonnull Class<T> type) {
return to((Type) type);
}
/**
* Convert this body into the given type.
*
* @param type Type to use.
* @param <T> Generic type.
* @return Converted value.
*/
@Nonnull <T> T to(@Nonnull Type type);
/* **********************************************************************************************
* Factory methods:
* **********************************************************************************************
*/
/**
* Empty body.
*
* @param ctx Current context.
* @return Empty body.
*/
static @Nonnull Body empty(@Nonnull 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 @Nonnull Body of(@Nonnull Context ctx, @Nonnull 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 @Nonnull Body of(@Nonnull Context ctx, @Nonnull 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 @Nonnull Body of(@Nonnull Context ctx, @Nonnull Path file) {
return new FileBody(ctx, file);
}
}