Skip to content

Commit 0eed792

Browse files
Added support to extract all files/uploads on the request
1 parent cfa09d6 commit 0eed792

File tree

7 files changed

+81
-29
lines changed

7 files changed

+81
-29
lines changed

jooby/src/main/java/org/jooby/Request.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,13 @@ public List<Upload> files(final String name) throws IOException {
440440
return req.files(name);
441441
}
442442

443-
@Override
443+
@Nonnull
444+
@Override
445+
public List<Upload> files() throws IOException {
446+
return req.files();
447+
}
448+
449+
@Override
444450
public Mutant header(final String name) {
445451
return req.header(name);
446452
}
@@ -1085,6 +1091,16 @@ default Optional<Upload> ifFile(final String name) throws IOException {
10851091
@Nonnull
10861092
List<Upload> files(final String name) throws IOException;
10871093

1094+
/**
1095+
* Get a list of files {@link Upload} that were uploaded in the request. The request must be a POST with
1096+
* <code>multipart/form-data</code> content-type.
1097+
*
1098+
* @return A list of {@link Upload}.
1099+
* @throws IOException
1100+
*/
1101+
@Nonnull
1102+
List<Upload> files() throws IOException;
1103+
10881104
/**
10891105
* Get a HTTP header.
10901106
*

jooby/src/main/java/org/jooby/internal/RequestImpl.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,7 @@
207207
import com.google.inject.Injector;
208208
import com.google.inject.Key;
209209
import com.typesafe.config.Config;
210-
import static java.util.Objects.requireNonNull;
211-
import org.jooby.Cookie;
212-
import org.jooby.Env;
213-
import org.jooby.Err;
214-
import org.jooby.MediaType;
215-
import org.jooby.Mutant;
216-
import org.jooby.Parser;
217-
import org.jooby.Request;
218-
import org.jooby.Response;
219-
import org.jooby.Route;
220-
import org.jooby.Session;
221-
import org.jooby.Status;
222-
import org.jooby.Upload;
210+
import org.jooby.*;
223211
import org.jooby.funzy.Try;
224212
import org.jooby.internal.parser.ParserExecutor;
225213
import org.jooby.spi.NativeRequest;
@@ -228,20 +216,15 @@
228216
import java.io.File;
229217
import java.io.IOException;
230218
import java.nio.charset.Charset;
231-
import java.util.ArrayList;
232-
import java.util.Collections;
233-
import java.util.HashMap;
234-
import java.util.LinkedHashMap;
235-
import java.util.List;
236-
import java.util.Locale;
219+
import java.util.*;
237220
import java.util.Locale.LanguageRange;
238-
import java.util.Map;
239-
import java.util.Optional;
240221
import java.util.function.BiFunction;
241222
import java.util.function.Function;
242223
import java.util.function.Supplier;
243224
import java.util.stream.Collectors;
244225

226+
import static java.util.Objects.requireNonNull;
227+
245228
public class RequestImpl implements Request {
246229

247230
private final Map<String, Mutant> params = new HashMap<>();
@@ -399,6 +382,13 @@ public List<Upload> files(final String name) throws IOException {
399382
return uploads;
400383
}
401384

385+
public List<Upload> files() throws IOException {
386+
return req.files()
387+
.stream()
388+
.map(upload -> new UploadImpl(injector, upload))
389+
.collect(Collectors.toList());
390+
}
391+
402392
private Mutant _param(final String name, final Function<String, String> xss) {
403393
Mutant param = this.params.get(name);
404394
if (param == null) {
@@ -469,8 +459,7 @@ public Mutant body() throws Exception {
469459
Integer.toHexString(System.identityHashCode(this)));
470460
files.add(fbody);
471461
int bufferSize = conf.getBytes("server.http.RequestBufferSize").intValue();
472-
Parser.BodyReference body = new BodyReferenceImpl(length, charset(), fbody, req.in(),
473-
bufferSize);
462+
Parser.BodyReference body = new BodyReferenceImpl(length, charset(), fbody, req.in(), bufferSize);
474463
return new MutantImpl(require(ParserExecutor.class), type, body);
475464
}
476465
return new MutantImpl(require(ParserExecutor.class), type, new EmptyBodyReference());
@@ -670,5 +659,4 @@ private Session setSession(final SessionManager sm, final Response rsp, final Se
670659
private void destroySession() {
671660
this.reqSession = Optional.empty();
672661
}
673-
674662
}

jooby/src/main/java/org/jooby/spi/NativeRequest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ default Map<String, Object> attributes() {
300300
*/
301301
List<NativeUpload> files(String name) throws IOException;
302302

303+
/**
304+
* Get all the files or an empty list.
305+
*
306+
* @return All the files or an empty list.
307+
* @throws IOException If file parsing fails.
308+
*/
309+
List<NativeUpload> files() throws IOException;
310+
303311
/**
304312
* Input stream that represent the body.
305313
*

jooby/src/test/java/org/jooby/RequestTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.google.inject.Key;
2323
import com.google.inject.TypeLiteral;
2424

25+
import javax.annotation.Nonnull;
26+
2527
public class RequestTest {
2628
public class RequestMock implements Request {
2729

@@ -241,6 +243,11 @@ public List<Upload> files(final String name) throws IOException {
241243
throw new UnsupportedOperationException();
242244
}
243245

246+
@Nonnull
247+
@Override
248+
public List<Upload> files() throws IOException {
249+
throw new UnsupportedOperationException();
250+
}
244251
}
245252

246253
@Test

modules/jooby-netty/src/main/java/org/jooby/internal/netty/NettyRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ public List<NativeUpload> files(final String name) throws IOException {
370370
return ImmutableList.copyOf(this.files.get(name));
371371
}
372372

373+
@Override
374+
public List<NativeUpload> files() throws IOException {
375+
decodeParams();
376+
return ImmutableList.copyOf(this.files.values());
377+
}
378+
373379
@Override
374380
public InputStream in() {
375381
ByteBuf content = ((HttpContent) req).content();

modules/jooby-servlet/src/main/java/org/jooby/servlet/ServletServletRequest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,20 @@ public List<NativeUpload> files(final String name) throws IOException {
369369
}
370370
}
371371

372+
@Override
373+
public List<NativeUpload> files() throws IOException {
374+
try {
375+
if (multipart) {
376+
return req.getParts().stream()
377+
.map(part -> new ServletUpload(part, tmpdir))
378+
.collect(Collectors.toList());
379+
}
380+
return Collections.emptyList();
381+
} catch (ServletException ex) {
382+
throw new IOException("Unable to get files", ex);
383+
}
384+
}
385+
372386
@Override
373387
public InputStream in() throws IOException {
374388
return req.getInputStream();

modules/jooby-undertow/src/main/java/org/jooby/internal/undertow/UndertowRequest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,10 @@
208208
import java.io.InputStream;
209209
import java.net.InetAddress;
210210
import java.net.URLDecoder;
211-
import java.util.Collections;
212-
import java.util.Deque;
213-
import java.util.List;
214-
import java.util.Optional;
211+
import java.util.*;
215212
import java.util.concurrent.Executor;
216213
import java.util.stream.Collectors;
214+
import java.util.stream.StreamSupport;
217215

218216
import io.undertow.server.handlers.form.*;
219217
import org.jooby.Cookie;
@@ -354,6 +352,21 @@ public List<NativeUpload> files(final String name) {
354352
return builder.build();
355353
}
356354

355+
@Override
356+
public List<NativeUpload> files() throws IOException {
357+
FormData formData = parseForm();
358+
Iterator<String> keyIterator = formData.iterator();
359+
Iterable<String> iterableKeys = () -> keyIterator;
360+
List<NativeUpload> retVal = StreamSupport.stream(iterableKeys.spliterator(), true)
361+
.map(formData::get)
362+
.filter(formValues -> formValues.peekFirst().isFileItem())
363+
.flatMap(Collection::stream)
364+
.map(UndertowUpload::new)
365+
.collect(Collectors.toList());
366+
367+
return Collections.unmodifiableList(retVal);
368+
}
369+
357370
@Override
358371
public InputStream in() {
359372
blocking.get();

0 commit comments

Comments
 (0)