Added cancellation logic to RequestBodyPublisher.
This commit is contained in:
parent
5bbeb9c204
commit
0b19fca73a
|
|
@ -1,33 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2015 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.reactive.util;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
*/
|
|
||||||
interface Signal<T> {
|
|
||||||
|
|
||||||
boolean isOnNext();
|
|
||||||
|
|
||||||
T next();
|
|
||||||
|
|
||||||
boolean isOnError();
|
|
||||||
|
|
||||||
Throwable error();
|
|
||||||
|
|
||||||
boolean isComplete();
|
|
||||||
}
|
|
||||||
|
|
@ -49,19 +49,30 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
|
||||||
|
|
||||||
private boolean stalled;
|
private boolean stalled;
|
||||||
|
|
||||||
|
private boolean cancelled;
|
||||||
|
|
||||||
public RequestBodyPublisher(AsyncContextSynchronizer synchronizer, int bufferSize) {
|
public RequestBodyPublisher(AsyncContextSynchronizer synchronizer, int bufferSize) {
|
||||||
this.synchronizer = synchronizer;
|
this.synchronizer = synchronizer;
|
||||||
this.buffer = new byte[bufferSize];
|
this.buffer = new byte[bufferSize];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<? super byte[]> s) {
|
public void subscribe(Subscriber<? super byte[]> subscriber) {
|
||||||
this.subscriber = s;
|
if (subscriber == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
else if (this.subscriber != null) {
|
||||||
|
subscriber.onError(new IllegalStateException("Only one subscriber allowed"));
|
||||||
|
}
|
||||||
|
this.subscriber = subscriber;
|
||||||
this.subscriber.onSubscribe(new RequestBodySubscription());
|
this.subscriber.onSubscribe(new RequestBodySubscription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDataAvailable() throws IOException {
|
public void onDataAvailable() throws IOException {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ServletInputStream input = this.synchronizer.getInputStream();
|
ServletInputStream input = this.synchronizer.getInputStream();
|
||||||
logger.debug("onDataAvailable: " + input);
|
logger.debug("onDataAvailable: " + input);
|
||||||
|
|
||||||
|
|
@ -100,6 +111,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAllDataRead() throws IOException {
|
public void onAllDataRead() throws IOException {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
logger.debug("All data read");
|
logger.debug("All data read");
|
||||||
this.synchronizer.readComplete();
|
this.synchronizer.readComplete();
|
||||||
if (this.subscriber != null) {
|
if (this.subscriber != null) {
|
||||||
|
|
@ -109,7 +123,11 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(Throwable t) {
|
public void onError(Throwable t) {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
logger.error("RequestBodyPublisher Error", t);
|
logger.error("RequestBodyPublisher Error", t);
|
||||||
|
this.synchronizer.readComplete();
|
||||||
if (this.subscriber != null) {
|
if (this.subscriber != null) {
|
||||||
this.subscriber.onError(t);
|
this.subscriber.onError(t);
|
||||||
}
|
}
|
||||||
|
|
@ -119,6 +137,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void request(long n) {
|
public void request(long n) {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
logger.debug("Updating demand " + demand + " by " + n);
|
logger.debug("Updating demand " + demand + " by " + n);
|
||||||
|
|
||||||
demand.increase(n);
|
demand.increase(n);
|
||||||
|
|
@ -138,6 +159,10 @@ public class RequestBodyPublisher implements ReadListener, Publisher<byte[]> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cancelled = true;
|
||||||
synchronizer.readComplete();
|
synchronizer.readComplete();
|
||||||
demand.reset();
|
demand.reset();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2015 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.reactive.web.http;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.reactivestreams.Publisher;
|
||||||
|
import reactor.rx.Streams;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.reactive.io.ByteArrayPublisherInputStream;
|
||||||
|
import org.springframework.reactive.io.ByteArrayPublisherOutputStream;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Arjen Poutsma
|
||||||
|
*/
|
||||||
|
public class XmlHandler implements HttpHandler {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(XmlHandler.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Publisher<Void> handle(ServerHttpRequest request,
|
||||||
|
ServerHttpResponse response) {
|
||||||
|
try {
|
||||||
|
JAXBContext jaxbContext = JAXBContext.newInstance(XmlHandlerIntegrationTests.Person.class);
|
||||||
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||||
|
|
||||||
|
Runnable r = () -> {
|
||||||
|
try {
|
||||||
|
ByteArrayPublisherInputStream bis = new ByteArrayPublisherInputStream(request.getBody());
|
||||||
|
|
||||||
|
XmlHandlerIntegrationTests.Person johnDoe =
|
||||||
|
(XmlHandlerIntegrationTests.Person) unmarshaller.unmarshal(bis);
|
||||||
|
|
||||||
|
logger.info("Read: " + johnDoe);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
logger.error(e, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Thread t = new Thread(r);
|
||||||
|
t.start();
|
||||||
|
|
||||||
|
response.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||||
|
XmlHandlerIntegrationTests.Person janeDoe = new XmlHandlerIntegrationTests.Person("Jane Doe");
|
||||||
|
ByteArrayPublisherOutputStream bos = new ByteArrayPublisherOutputStream();
|
||||||
|
marshaller.marshal(janeDoe, bos);
|
||||||
|
bos.close();
|
||||||
|
|
||||||
|
return response.writeWith(bos.toByteArrayPublisher());
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
logger.error(ex, ex);
|
||||||
|
fail(ex.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2015 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.reactive.web.http;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.http.RequestEntity;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Arjen Poutsma
|
||||||
|
*/
|
||||||
|
public class XmlHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected HttpHandler createHttpHandler() {
|
||||||
|
return new XmlHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void xml() throws Exception {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
Person johnDoe = new Person("John Doe");
|
||||||
|
|
||||||
|
RequestEntity<Person> request = RequestEntity.post(new URI("http://localhost:" + port)).body(
|
||||||
|
johnDoe);
|
||||||
|
ResponseEntity<Person> response = restTemplate.exchange(request, Person.class);
|
||||||
|
System.out.println(response.getBody());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
static class Person {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Person() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
return name.equals(((Person) o).name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return name.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue