Browse Source
This change also removes reactor-stream variants of the request and response since the request and response aren't used directly by application code and get passed through reactor.Publishers anyway.pull/1111/head
13 changed files with 121 additions and 215 deletions
@ -1,81 +0,0 @@
@@ -1,81 +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.http.server.reactor; |
||||
|
||||
import java.net.URI; |
||||
import java.net.URISyntaxException; |
||||
import java.nio.ByteBuffer; |
||||
|
||||
import org.reactivestreams.Publisher; |
||||
import reactor.Publishers; |
||||
import reactor.io.buffer.Buffer; |
||||
import reactor.io.net.http.HttpChannel; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.http.server.ReactiveServerHttpRequest; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Stephane Maldini |
||||
*/ |
||||
public class PublisherReactorServerHttpRequest implements ReactiveServerHttpRequest { |
||||
|
||||
private final HttpChannel<Buffer, ?> channel; |
||||
|
||||
private HttpHeaders headers; |
||||
|
||||
|
||||
public PublisherReactorServerHttpRequest(HttpChannel<Buffer, ?> request) { |
||||
Assert.notNull("'request', request must not be null."); |
||||
this.channel = request; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public HttpHeaders getHeaders() { |
||||
if (this.headers == null) { |
||||
this.headers = new HttpHeaders(); |
||||
for (String name : this.channel.headers().names()) { |
||||
for (String value : this.channel.headers().getAll(name)) { |
||||
this.headers.add(name, value); |
||||
} |
||||
} |
||||
} |
||||
return this.headers; |
||||
} |
||||
|
||||
@Override |
||||
public HttpMethod getMethod() { |
||||
return HttpMethod.valueOf(this.channel.method().getName()); |
||||
} |
||||
|
||||
@Override |
||||
public URI getURI() { |
||||
try { |
||||
return new URI(this.channel.uri()); |
||||
} catch (URISyntaxException ex) { |
||||
throw new IllegalStateException("Could not get URI: " + ex.getMessage(), ex); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public Publisher<ByteBuffer> getBody() { |
||||
return Publishers.map(channel.input(), Buffer::byteBuffer); |
||||
} |
||||
|
||||
} |
||||
@ -1,85 +0,0 @@
@@ -1,85 +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.http.server.reactor; |
||||
|
||||
import java.nio.ByteBuffer; |
||||
|
||||
import org.reactivestreams.Publisher; |
||||
import reactor.Publishers; |
||||
import reactor.io.buffer.Buffer; |
||||
import reactor.io.net.http.HttpChannel; |
||||
import reactor.io.net.http.model.Status; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.server.ReactiveServerHttpResponse; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Stephane Maldini |
||||
*/ |
||||
public class PublisherReactorServerHttpResponse implements ReactiveServerHttpResponse { |
||||
|
||||
private final HttpChannel<?, Buffer> channel; |
||||
|
||||
private final HttpHeaders headers; |
||||
|
||||
private boolean headersWritten = false; |
||||
|
||||
|
||||
public PublisherReactorServerHttpResponse(HttpChannel<?, Buffer> response) { |
||||
Assert.notNull("'response', response must not be null."); |
||||
this.channel = response; |
||||
this.headers = new HttpHeaders(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void setStatusCode(HttpStatus status) { |
||||
this.channel.responseStatus(Status.valueOf(status.value())); |
||||
} |
||||
|
||||
@Override |
||||
public HttpHeaders getHeaders() { |
||||
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers); |
||||
} |
||||
|
||||
@Override |
||||
public Publisher<Void> writeHeaders() { |
||||
if (this.headersWritten) { |
||||
return Publishers.empty(); |
||||
} |
||||
applyHeaders(); |
||||
return this.channel.writeHeaders(); |
||||
} |
||||
|
||||
@Override |
||||
public Publisher<Void> setBody(Publisher<ByteBuffer> contentPublisher) { |
||||
applyHeaders(); |
||||
return this.channel.writeWith(Publishers.map(contentPublisher, Buffer::new)); |
||||
} |
||||
|
||||
private void applyHeaders() { |
||||
if (!this.headersWritten) { |
||||
for (String name : this.headers.keySet()) { |
||||
for (String value : this.headers.get(name)) { |
||||
this.channel.responseHeaders().add(name, value); |
||||
} |
||||
} |
||||
this.headersWritten = true; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue