Skip to content

Commit 72684cb

Browse files
committed
more cleanup around file upload and body
1 parent 1633c0c commit 72684cb

19 files changed

Lines changed: 249 additions & 69 deletions

File tree

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</dependency>
2727
<dependency>
2828
<groupId>io.jooby</groupId>
29-
<artifactId>jooby-netty</artifactId>
29+
<artifactId>jooby-utow</artifactId>
3030
<version>${jooby.version}</version>
3131
</dependency>
3232
<dependency>

examples/src/main/java/examples/HelloApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ public Message(String message) {
9797
});
9898

9999
post("/user", ctx -> {
100+
System.out.println(Thread.currentThread());
100101
User user = ctx.multipart(User.class);
101102
return user.pic.toString();
102103
});
103104

104105
post("/docx", ctx -> {
106+
System.out.println(Thread.currentThread());
105107
byte[] bytes = ctx.body().bytes();
106108
return bytes.length;
107109
});

jooby/src/main/java/io/jooby/Body.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.nio.channels.ReadableByteChannel;
2626
import java.nio.charset.Charset;
27+
import java.nio.file.Path;
2728

2829
public interface Body extends Value {
2930

@@ -58,7 +59,7 @@ static Body of(@Nonnull byte[] bytes) {
5859
return new ByteArrayBody(bytes);
5960
}
6061

61-
static Body of(@Nonnull File file) {
62+
static Body of(@Nonnull Path file) {
6263
return new FileBody(file);
6364
}
6465
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.jooby;
1717

1818
import javax.annotation.Nonnull;
19+
import java.io.InputStream;
1920
import java.nio.charset.Charset;
2021
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.Path;
@@ -51,6 +52,8 @@ public interface FileUpload extends Value {
5152
return Map.of(name(), List.of(filename()));
5253
}
5354

55+
InputStream stream();
56+
5457
byte[] bytes();
5558

5659
Path path();

jooby/src/main/java/io/jooby/Server.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,12 @@ protected void addShutdownHook() {
9595
@Nonnull Server gzip(boolean enabled);
9696

9797
static boolean connectionLost(Throwable cause) {
98-
if (cause instanceof ClosedChannelException) {
99-
return true;
100-
}
10198
if (cause instanceof IOException) {
10299
String message = cause.getMessage();
103100
if (message != null) {
104101
return message.toLowerCase().contains("connection reset by peer");
105102
}
106103
}
107-
return false;
104+
return (cause instanceof ClosedChannelException);
108105
}
109106
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@
2828
import java.nio.channels.ReadableByteChannel;
2929
import java.nio.charset.StandardCharsets;
3030
import java.nio.file.Files;
31+
import java.nio.file.Path;
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Map;
3435

3536
public class FileBody implements Body {
36-
private File file;
37+
private Path file;
3738

38-
public FileBody(File file) {
39+
public FileBody(Path file) {
3940
this.file = file;
4041
}
4142

4243
@Override public long length() {
43-
return file.length();
44+
try {
45+
return Files.size(file);
46+
} catch (IOException x) {
47+
throw Throwing.sneakyThrow(x);
48+
}
4449
}
4550

4651
@Override public boolean isInMemory() {
@@ -49,23 +54,23 @@ public FileBody(File file) {
4954

5055
@Override public ReadableByteChannel channel() {
5156
try {
52-
return Files.newByteChannel(file.toPath());
57+
return Files.newByteChannel(file);
5358
} catch (IOException x) {
5459
throw Throwing.sneakyThrow(x);
5560
}
5661
}
5762

5863
@Override public InputStream stream() {
5964
try {
60-
return new FileInputStream(file);
65+
return Files.newInputStream(file);
6166
} catch (IOException x) {
6267
throw Throwing.sneakyThrow(x);
6368
}
6469
}
6570

6671
@Override public byte[] bytes() {
6772
try {
68-
return Files.readAllBytes(file.toPath());
73+
return Files.readAllBytes(file);
6974
} catch (IOException x) {
7075
throw Throwing.sneakyThrow(x);
7176
}

modules/server/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyFileUpload.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import io.jooby.Throwing;
2121

2222
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.nio.file.Files;
2325
import java.nio.file.Path;
2426

2527
public class JettyFileUpload implements FileUpload {
@@ -40,7 +42,23 @@ public JettyFileUpload(String name, MultiPartFormInputStream.MultiPart upload) {
4042
}
4143

4244
@Override public byte[] bytes() {
43-
return upload.getBytes();
45+
try {
46+
byte[] bytes = upload.getBytes();
47+
if (bytes == null) {
48+
return Files.readAllBytes(upload.getFile().toPath());
49+
}
50+
return bytes;
51+
} catch (IOException x) {
52+
throw Throwing.sneakyThrow(x);
53+
}
54+
}
55+
56+
@Override public InputStream stream() {
57+
try {
58+
return upload.getInputStream();
59+
} catch (IOException x) {
60+
throw Throwing.sneakyThrow(x);
61+
}
4462
}
4563

4664
@Override public String contentType() {

modules/server/jooby-jetty/src/main/java/io/jooby/internal/jetty/LimitedInputStream.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
116
package io.jooby.internal.jetty;
217

318
import io.jooby.Err;

modules/server/jooby-netty/src/main/java/io/jooby/internal/netty/HttpRawPostRequestDecoder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
116
package io.jooby.internal.netty;
217

318
import io.netty.handler.codec.http.HttpContent;

modules/server/jooby-netty/src/main/java/io/jooby/internal/netty/NettyBody.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
116
package io.jooby.internal.netty;
217

318
import io.jooby.Body;

0 commit comments

Comments
 (0)