Skip to content

Commit

Permalink
Upgrade Netty and expose Netty Context (#3983)
Browse files Browse the repository at this point in the history
* Upgrade Netty and expose Netty Context
Bumped netty version to latest
Updated API usage to match
Enhanced returned SecurityContext to include Netty information

Signed-off-by: Ian <[email protected]>
  • Loading branch information
juur authored and senivam committed Apr 10, 2019
1 parent 0ee0533 commit e2ee2e2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http2.Http2Codec;
import io.netty.handler.codec.http2.Http2MultiplexCodecBuilder;
import io.netty.handler.ssl.ApplicationProtocolNames;
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
Expand All @@ -45,7 +45,8 @@ class HttpVersionChooser extends ApplicationProtocolNegotiationHandler {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
ctx.pipeline().addLast(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
ctx.pipeline().addLast(Http2MultiplexCodecBuilder.forServer(
new JerseyHttp2ServerHandler(baseUri, container)).build());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.netty.util.concurrent.GenericFutureListener;
import org.glassfish.jersey.internal.PropertiesDelegate;
import org.glassfish.jersey.netty.connector.internal.NettyInputStream;
import org.glassfish.jersey.netty.httpserver.NettySecurityContext;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.internal.ContainerUtils;

Expand Down Expand Up @@ -121,7 +122,7 @@ private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, HttpR
URI requestUri = URI.create(baseUri + ContainerUtils.encodeUnsafeCharacters(s));

ContainerRequest requestContext = new ContainerRequest(
baseUri, requestUri, req.method().name(), getSecurityContext(),
baseUri, requestUri, req.method().name(), getSecurityContext(ctx),
new PropertiesDelegate() {

private final Map<String, Object> properties = new HashMap<>();
Expand Down Expand Up @@ -176,29 +177,8 @@ public int read() throws IOException {
return requestContext;
}

private SecurityContext getSecurityContext() {
return new SecurityContext() {

@Override
public boolean isUserInRole(final String role) {
return false;
}

@Override
public boolean isSecure() {
return false;
}

@Override
public Principal getUserPrincipal() {
return null;
}

@Override
public String getAuthenticationScheme() {
return null;
}
};
private NettySecurityContext getSecurityContext(ChannelHandlerContext ctx) {
return new NettySecurityContext(ctx);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpServerUpgradeHandler;
import io.netty.handler.codec.http2.Http2Codec;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2MultiplexCodecBuilder;
import io.netty.handler.codec.http2.Http2ServerUpgradeCodec;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.stream.ChunkedWriteHandler;
Expand Down Expand Up @@ -113,7 +113,8 @@ private void configureClearText(SocketChannel ch) {
@Override
public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
return new Http2ServerUpgradeCodec(Http2MultiplexCodecBuilder.forServer(
new JerseyHttp2ServerHandler(baseUri, container)).build());
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2018 Ian Kirk. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.netty.httpserver;

import java.security.Principal;

import javax.ws.rs.core.SecurityContext;

import io.netty.channel.ChannelHandlerContext;

public class NettySecurityContext implements SecurityContext {
private ChannelHandlerContext ctx;

public NettySecurityContext(ChannelHandlerContext ctx) {
this.ctx = ctx;
}

public ChannelHandlerContext getNettyContext() {
return this.ctx;
}

@Override
public boolean isUserInRole(final String role) {
return false;
}

@Override
public boolean isSecure() {
return false;
}

@Override
public Principal getUserPrincipal() {
return null;
}

@Override
public String getAuthenticationScheme() {
return null;
}
};
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,8 @@
<mockito.version>1.10.19</mockito.version>
<moxy.version>2.7.4</moxy.version>
<mustache.version>0.8.17</mustache.version>
<netty.version>4.1.5.Final</netty.version>
<netty.version>4.1.31.Final</netty.version>
<nexus-staging.mvn.plugin.version>1.6.7</nexus-staging.mvn.plugin.version>
<opentracing.version>0.30.0</opentracing.version>
<osgi.version>4.2.0</osgi.version>
<pax.exam.version>4.9.1</pax.exam.version>
Expand Down

0 comments on commit e2ee2e2

Please sign in to comment.