Skip to content

Commit 0a8625e

Browse files
committed
Download file not working properly actframework#562
1 parent a7c1b64 commit 0a8625e

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ActFramework Change Log
22

33
**1.8.2**
4+
* Download file not working properly #562
45
* Error with sending large response: UT000043: Data is already being sent. You must wait for the completion callback to be be invoked before calling send() again #560
56
* ResourceLoader: support loading json file into Map or other POJO #559
67
* Support `@DefaultValue` with `@Configuration` #558

src/main/java/act/inject/util/ResourceLoader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ protected static Object _load(String resourcePath, BeanSpec spec, boolean ignore
141141
return null;
142142
}
143143
boolean isJson = resourcePath.endsWith(".json");
144-
if (isJson) {
145-
return JSON.parseObject(IO.readContentAsString(url), spec.type());
146-
}
147144
Class<?> rawType = spec.rawType();
148145
if (URL.class == rawType) {
149146
return url;
@@ -201,6 +198,9 @@ protected static Object _load(String resourcePath, BeanSpec spec, boolean ignore
201198
} else if (Reader.class == rawType) {
202199
return new InputStreamReader(IO.is(url));
203200
}
201+
if (isJson) {
202+
return JSON.parseObject(IO.readContentAsString(url), spec.type());
203+
}
204204
try {
205205
return Act.app().resolverManager().resolve(IO.readContentAsString(url), rawType);
206206
} catch (RuntimeException e) {

src/main/java/act/xio/undertow/UndertowResponse.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.nio.channels.FileChannel;
4848
import java.nio.charset.StandardCharsets;
4949
import java.util.Locale;
50+
import java.util.concurrent.atomic.AtomicBoolean;
5051
import java.util.concurrent.locks.Condition;
5152
import java.util.concurrent.locks.ReentrantLock;
5253

@@ -141,13 +142,20 @@ private void clear() {
141142
private boolean endAsync;
142143
private Sender sender;
143144
private ReentrantLock lock;
145+
private boolean isPartialMode;
146+
private AtomicBoolean partialSent = new AtomicBoolean();
147+
private AtomicBoolean closeExchange = new AtomicBoolean(false);
144148
private IoCallback ioCallback = new DefaultIoCallback() {
145149
@Override
146150
public void onComplete(HttpServerExchange exchange, Sender sender) {
147151
if (null != lock) {
148152
lock.lock();
149153
buffer.partSent();
150154
lock.unlock();
155+
} else if (closeExchange.get()) {
156+
exchange.endExchange();
157+
} else {
158+
partialSent.set(true);
151159
}
152160
}
153161

@@ -218,8 +226,10 @@ public void writeContentPart(String s) {
218226
}
219227

220228
public void writeContentPart(ByteBuffer buffer) {
229+
isPartialMode = true;
221230
try {
222231
if (null != buffer) {
232+
partialSent.set(false);
223233
sender().send(buffer, ioCallback);
224234
} else {
225235
this.buffer.sendOrBuf(buffer, sender());
@@ -290,8 +300,12 @@ public void commit() {
290300
} else {
291301
if (null != buffer) {
292302
buffer.sendThroughFinalPart(sender());
293-
} else {
294-
sender().close(IoCallback.END_EXCHANGE);
303+
} else if (isPartialMode) {
304+
if (partialSent.get()) {
305+
hse.endExchange();
306+
} else {
307+
closeExchange.set(true);
308+
}
295309
}
296310
}
297311
markClosed();

0 commit comments

Comments
 (0)