Browse Source
Latest reactor-net doesn't depend on reactor-stream anymore (neither reactor-codec and reactor-bus, it only depends on reactor-core).pull/1111/head
Stephane Maldini
9 years ago
committed by
Sebastien Deleuze
8 changed files with 204 additions and 102 deletions
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
/* |
||||
* Copyright (c) 2011-2015 Pivotal Software Inc, All Rights Reserved. |
||||
* |
||||
* 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.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.reactive.web.http.ServerHttpRequest; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Stephane Maldini |
||||
*/ |
||||
public class PublisherReactorServerHttpRequest implements ServerHttpRequest { |
||||
|
||||
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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
/* |
||||
* Copyright (c) 2011-2015 Pivotal Software Inc, All Rights Reserved. |
||||
* |
||||
* 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.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.reactive.web.http.ServerHttpResponse; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Stephane Maldini |
||||
*/ |
||||
public class PublisherReactorServerHttpResponse implements ServerHttpResponse { |
||||
|
||||
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> writeWith(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