diff --git a/spring-web-reactive/src/main/java/org/springframework/reactive/util/Signal.java b/spring-web-reactive/src/main/java/org/springframework/reactive/util/Signal.java deleted file mode 100644 index a0cb153e6ed..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/reactive/util/Signal.java +++ /dev/null @@ -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 { - - boolean isOnNext(); - - T next(); - - boolean isOnError(); - - Throwable error(); - - boolean isComplete(); -} diff --git a/spring-web-reactive/src/main/java/org/springframework/reactive/web/http/servlet/RequestBodyPublisher.java b/spring-web-reactive/src/main/java/org/springframework/reactive/web/http/servlet/RequestBodyPublisher.java index 84769f9943b..35cc27fdcbe 100644 --- a/spring-web-reactive/src/main/java/org/springframework/reactive/web/http/servlet/RequestBodyPublisher.java +++ b/spring-web-reactive/src/main/java/org/springframework/reactive/web/http/servlet/RequestBodyPublisher.java @@ -49,19 +49,30 @@ public class RequestBodyPublisher implements ReadListener, Publisher { private boolean stalled; + private boolean cancelled; + public RequestBodyPublisher(AsyncContextSynchronizer synchronizer, int bufferSize) { this.synchronizer = synchronizer; this.buffer = new byte[bufferSize]; } @Override - public void subscribe(Subscriber s) { - this.subscriber = s; + public void subscribe(Subscriber subscriber) { + 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()); } @Override public void onDataAvailable() throws IOException { + if (cancelled) { + return; + } ServletInputStream input = this.synchronizer.getInputStream(); logger.debug("onDataAvailable: " + input); @@ -100,6 +111,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher { @Override public void onAllDataRead() throws IOException { + if (cancelled) { + return; + } logger.debug("All data read"); this.synchronizer.readComplete(); if (this.subscriber != null) { @@ -109,7 +123,11 @@ public class RequestBodyPublisher implements ReadListener, Publisher { @Override public void onError(Throwable t) { + if (cancelled) { + return; + } logger.error("RequestBodyPublisher Error", t); + this.synchronizer.readComplete(); if (this.subscriber != null) { this.subscriber.onError(t); } @@ -119,6 +137,9 @@ public class RequestBodyPublisher implements ReadListener, Publisher { @Override public void request(long n) { + if (cancelled) { + return; + } logger.debug("Updating demand " + demand + " by " + n); demand.increase(n); @@ -138,6 +159,10 @@ public class RequestBodyPublisher implements ReadListener, Publisher { @Override public void cancel() { + if (cancelled) { + return; + } + cancelled = true; synchronizer.readComplete(); demand.reset(); } diff --git a/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XMLHandler.java b/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XMLHandler.java new file mode 100644 index 00000000000..391f6acbffc --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XMLHandler.java @@ -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 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; + } + } +} diff --git a/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XmlHandlerIntegrationTests.java b/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XmlHandlerIntegrationTests.java new file mode 100644 index 00000000000..d703582ea01 --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/reactive/web/http/XmlHandlerIntegrationTests.java @@ -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 request = RequestEntity.post(new URI("http://localhost:" + port)).body( + johnDoe); + ResponseEntity 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; + } + } +}