io.micronaut.spring.web.reactive.MicronautServerHttpRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-spring-web Show documentation
Show all versions of micronaut-spring-web Show documentation
Extensions to integrate Micronaut and Spring
/*
* Copyright 2017-2020 original 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
*
* https://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 io.micronaut.spring.web.reactive;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.net.ssl.SSLSession;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.cookie.Cookies;
import io.micronaut.http.server.HttpServerConfiguration;
import io.micronaut.http.server.netty.NettyHttpRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.ssl.SslHandler;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.AbstractServerHttpRequest;
import org.springframework.http.server.reactive.SslInfo;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Flux;
/**
* Micronaut implementation of {@link org.springframework.http.server.reactive.ServerHttpRequest}.
*
* @author graemerocher
* @since 1.0
*/
public class MicronautServerHttpRequest extends AbstractServerHttpRequest {
private final HttpRequest> request;
private final ChannelResolver channelResolver;
private final HttpServerConfiguration serverConfiguration;
/**
* Default constructor.
*
* @param request The request to adapt
* @param channelResolver The channel resolver
* @param serverConfiguration The server configuration
*/
public MicronautServerHttpRequest(
HttpRequest> request,
ChannelResolver channelResolver, HttpServerConfiguration serverConfiguration) {
super(HttpMethod.valueOf(request.getMethod().name()), request.getUri(), null, initHeaders(request));
this.request = request;
this.channelResolver = channelResolver;
this.serverConfiguration = serverConfiguration;
}
private static HttpHeaders initHeaders(HttpRequest> request) {
final HttpHeaders httpHeaders = new HttpHeaders();
final io.micronaut.http.HttpHeaders micronautHeaders = request.getHeaders();
micronautHeaders.forEach(entry -> {
final String key = entry.getKey();
final List value = entry.getValue();
httpHeaders.addAll(key, value);
});
return httpHeaders;
}
@Override
protected MultiValueMap initCookies() {
final Cookies cookies = request.getCookies();
MultiValueMap cookieMultiValueMap = new LinkedMultiValueMap<>();
cookies.forEach((s, cookie) -> {
final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
cookieMultiValueMap.set(s, httpCookie);
});
return cookieMultiValueMap;
}
@Override
protected SslInfo initSslInfo() {
final Optional channel = channelResolver.resolveChannel(request);
if (channel.isPresent()) {
SslHandler sslHandler = channel.get().pipeline().get(SslHandler.class);
if (sslHandler != null) {
SSLSession session = sslHandler.engine().getSession();
return new DefaultSslInfo(session);
}
}
return null;
}
@Override
public T getNativeRequest() {
return (T) request;
}
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(request.getMethod().name());
}
@SuppressWarnings("SubscriberImplementation")
@Override
public reactor.core.publisher.Flux getBody() {
final Optional opt = channelResolver.resolveChannel(request);
if (opt.isPresent()) {
final Channel channel = opt.get();
final NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(channel.alloc());
NettyHttpRequest> nettyRequest = ((NettyHttpRequest>) request);
try {
return Flux.from(nettyRequest.byteBody().toByteBufferPublisher())
.map(b -> nettyDataBufferFactory.wrap((ByteBuf) b.asNativeBuffer()));
} catch (Throwable e) {
return Flux.error(e);
}
}
return Flux.empty();
}
/**
* Default implementation of {@link SslInfo}.
*
* @author Rossen Stoyanchev
* @since 5.0.2
*/
final class DefaultSslInfo implements SslInfo {
@Nullable
private final String sessionId;
@Nullable
private final X509Certificate[] peerCertificates;
/**
* Default constructor.
* @param session The SSLSession
*/
DefaultSslInfo(SSLSession session) {
Assert.notNull(session, "SSLSession is required");
this.sessionId = initSessionId(session);
this.peerCertificates = initCertificates(session);
}
@Override
@Nullable
public String getSessionId() {
return this.sessionId;
}
@Override
@Nullable
public X509Certificate[] getPeerCertificates() {
return this.peerCertificates;
}
@Nullable
private String initSessionId(SSLSession session) {
byte [] bytes = session.getId();
if (bytes == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
String digit = Integer.toHexString(b);
if (digit.length() < 2) {
sb.append('0');
}
if (digit.length() > 2) {
digit = digit.substring(digit.length() - 2);
}
sb.append(digit);
}
return sb.toString();
}
@Nullable
private X509Certificate[] initCertificates(SSLSession session) {
Certificate[] certificates;
try {
certificates = session.getPeerCertificates();
} catch (Throwable ex) {
return null;
}
List result = new ArrayList<>(certificates.length);
for (Certificate certificate : certificates) {
if (certificate instanceof X509Certificate) {
result.add((X509Certificate) certificate);
}
}
return (!result.isEmpty() ? result.toArray(new X509Certificate[0]) : null);
}
}
}