Skip to content

Commit

Permalink
Support for Jetty 10
Browse files Browse the repository at this point in the history
Closes gh-26123
  • Loading branch information
rstoyanchev committed Jan 21, 2021
1 parent e537844 commit aa7584d
Show file tree
Hide file tree
Showing 11 changed files with 1,006 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
Expand All @@ -16,11 +16,13 @@

package org.springframework.http.client.reactive;

import java.lang.reflect.Method;
import java.net.HttpCookie;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.reactive.client.ReactiveResponse;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
Expand All @@ -30,9 +32,11 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ReflectionUtils;

/**
* {@link ClientHttpResponse} implementation for the Jetty ReactiveStreams HTTP client.
Expand All @@ -46,6 +50,11 @@ class JettyClientHttpResponse implements ClientHttpResponse {

private static final Pattern SAMESITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*");

private static final ClassLoader loader = JettyClientHttpResponse.class.getClassLoader();

private static final boolean jetty10Present = ClassUtils.isPresent(
"org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer", loader);


private final ReactiveResponse reactiveResponse;

Expand All @@ -58,8 +67,11 @@ public JettyClientHttpResponse(ReactiveResponse reactiveResponse, Publisher<Data
this.reactiveResponse = reactiveResponse;
this.content = Flux.from(content);

MultiValueMap<String, String> adapter = new JettyHeadersAdapter(reactiveResponse.getHeaders());
this.headers = HttpHeaders.readOnlyHttpHeaders(adapter);
MultiValueMap<String, String> headers = (jetty10Present ?
Jetty10HttpFieldsHelper.getHttpHeaders(reactiveResponse) :
new JettyHeadersAdapter(reactiveResponse.getHeaders()));

this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
}


Expand Down Expand Up @@ -110,4 +122,38 @@ public HttpHeaders getHeaders() {
return this.headers;
}


private static class Jetty10HttpFieldsHelper {

private static final Method getHeadersMethod;

private static final Method getNameMethod;

private static final Method getValueMethod;

static {
try {
getHeadersMethod = Response.class.getMethod("getHeaders");
Class<?> type = loader.loadClass("org.eclipse.jetty.http.HttpField");
getNameMethod = type.getMethod("getName");
getValueMethod = type.getMethod("getValue");
}
catch (ClassNotFoundException | NoSuchMethodException ex) {
throw new IllegalStateException("No compatible Jetty version found", ex);
}
}

public static HttpHeaders getHttpHeaders(ReactiveResponse response) {
HttpHeaders headers = new HttpHeaders();
Iterable<?> iterator = (Iterable<?>)
ReflectionUtils.invokeMethod(getHeadersMethod, response.getResponse());
for (Object field : iterator) {
headers.add(
(String) ReflectionUtils.invokeMethod(getNameMethod, field),
(String) ReflectionUtils.invokeMethod(getValueMethod, field));
}
return headers;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -36,6 +36,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;

/**
Expand All @@ -49,6 +50,10 @@
*/
public class JettyHttpHandlerAdapter extends ServletHttpHandlerAdapter {

private static final boolean jetty10Present = ClassUtils.isPresent(
"org.eclipse.jetty.http.CookieCutter", JettyHttpHandlerAdapter.class.getClassLoader());


public JettyHttpHandlerAdapter(HttpHandler httpHandler) {
super(httpHandler);
}
Expand All @@ -58,16 +63,29 @@ public JettyHttpHandlerAdapter(HttpHandler httpHandler) {
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
throws IOException, URISyntaxException {

// TODO: need to compile against Jetty 10 to use HttpFields (class->interface)
if (jetty10Present) {
return super.createRequest(request, context);
}

Assert.notNull(getServletPath(), "Servlet path is not initialized");
return new JettyServerHttpRequest(request, context, getServletPath(), getDataBufferFactory(), getBufferSize());
return new JettyServerHttpRequest(
request, context, getServletPath(), getDataBufferFactory(), getBufferSize());
}

@Override
protected ServletServerHttpResponse createResponse(HttpServletResponse response,
AsyncContext context, ServletServerHttpRequest request) throws IOException {

return new JettyServerHttpResponse(
response, context, getDataBufferFactory(), getBufferSize(), request);
// TODO: need to compile against Jetty 10 to use HttpFields (class->interface)
if (jetty10Present) {
return new BaseJettyServerHttpResponse(
response, context, getDataBufferFactory(), getBufferSize(), request);
}
else {
return new JettyServerHttpResponse(
response, context, getDataBufferFactory(), getBufferSize(), request);
}
}


Expand All @@ -87,7 +105,34 @@ private static MultiValueMap<String, String> createHeaders(HttpServletRequest re
}


private static final class JettyServerHttpResponse extends ServletServerHttpResponse {
private static class BaseJettyServerHttpResponse extends ServletServerHttpResponse {

BaseJettyServerHttpResponse(HttpServletResponse response, AsyncContext asyncContext,
DataBufferFactory bufferFactory, int bufferSize, ServletServerHttpRequest request)
throws IOException {

super(response, asyncContext, bufferFactory, bufferSize, request);
}

BaseJettyServerHttpResponse(HttpHeaders headers, HttpServletResponse response, AsyncContext asyncContext,
DataBufferFactory bufferFactory, int bufferSize, ServletServerHttpRequest request)
throws IOException {

super(headers, response, asyncContext, bufferFactory, bufferSize, request);
}

@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
ByteBuffer input = dataBuffer.asByteBuffer();
int len = input.remaining();
ServletResponse response = getNativeResponse();
((HttpOutput) response.getOutputStream()).write(input);
return len;
}
}


private static final class JettyServerHttpResponse extends BaseJettyServerHttpResponse {

JettyServerHttpResponse(HttpServletResponse response, AsyncContext asyncContext,
DataBufferFactory bufferFactory, int bufferSize, ServletServerHttpRequest request)
Expand Down Expand Up @@ -124,15 +169,6 @@ protected void applyHeaders() {
response.setContentLengthLong(contentLength);
}
}

@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
ByteBuffer input = dataBuffer.asByteBuffer();
int len = input.remaining();
ServletResponse response = getNativeResponse();
((HttpOutput) response.getOutputStream()).write(input);
return len;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2002-2021 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
*
* 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 org.springframework.web.reactive.socket.adapter;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.extensions.Frame;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.reactive.socket.CloseStatus;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketMessage.Type;
import org.springframework.web.reactive.socket.WebSocketSession;

/**
* Identical to {@link JettyWebSocketHandlerAdapter}, only excluding the
* {@code onWebSocketFrame} method, since the {@link Frame} argument has moved
* to a different package in Jetty 10.
*
* @author Rossen Stoyanchev
* @since 5.3.4
*/
@WebSocket
public class Jetty10WebSocketHandlerAdapter {

private static final ByteBuffer EMPTY_PAYLOAD = ByteBuffer.wrap(new byte[0]);


private final WebSocketHandler delegateHandler;

private final Function<Session, JettyWebSocketSession> sessionFactory;

@Nullable
private JettyWebSocketSession delegateSession;


public Jetty10WebSocketHandlerAdapter(WebSocketHandler handler,
Function<Session, JettyWebSocketSession> sessionFactory) {

Assert.notNull(handler, "WebSocketHandler is required");
Assert.notNull(sessionFactory, "'sessionFactory' is required");
this.delegateHandler = handler;
this.sessionFactory = sessionFactory;
}


@OnWebSocketConnect
public void onWebSocketConnect(Session session) {
this.delegateSession = this.sessionFactory.apply(session);
this.delegateHandler.handle(this.delegateSession)
.checkpoint(session.getUpgradeRequest().getRequestURI() + " [JettyWebSocketHandlerAdapter]")
.subscribe(this.delegateSession);
}

@OnWebSocketMessage
public void onWebSocketText(String message) {
if (this.delegateSession != null) {
WebSocketMessage webSocketMessage = toMessage(Type.TEXT, message);
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
}
}

@OnWebSocketMessage
public void onWebSocketBinary(byte[] message, int offset, int length) {
if (this.delegateSession != null) {
ByteBuffer buffer = ByteBuffer.wrap(message, offset, length);
WebSocketMessage webSocketMessage = toMessage(Type.BINARY, buffer);
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
}
}

// TODO: onWebSocketFrame can't be declared without compiling against Jetty 10
// Jetty 10: org.eclipse.jetty.websocket.api.Frame
// Jetty 9: org.eclipse.jetty.websocket.api.extensions.Frame

// @OnWebSocketFrame
// public void onWebSocketFrame(Frame frame) {
// if (this.delegateSession != null) {
// if (OpCode.PONG == frame.getOpCode()) {
// ByteBuffer buffer = (frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD);
// WebSocketMessage webSocketMessage = toMessage(Type.PONG, buffer);
// this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
// }
// }
// }

private <T> WebSocketMessage toMessage(Type type, T message) {
WebSocketSession session = this.delegateSession;
Assert.state(session != null, "Cannot create message without a session");
if (Type.TEXT.equals(type)) {
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = session.bufferFactory().wrap(bytes);
return new WebSocketMessage(Type.TEXT, buffer);
}
else if (Type.BINARY.equals(type)) {
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
return new WebSocketMessage(Type.BINARY, buffer);
}
else if (Type.PONG.equals(type)) {
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
return new WebSocketMessage(Type.PONG, buffer);
}
else {
throw new IllegalArgumentException("Unexpected message type: " + message);
}
}

@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
if (this.delegateSession != null) {
this.delegateSession.handleClose(CloseStatus.create(statusCode, reason));
}
}

@OnWebSocketError
public void onWebSocketError(Throwable cause) {
if (this.delegateSession != null) {
this.delegateSession.handleError(cause);
}
}

}

0 comments on commit aa7584d

Please sign in to comment.