Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie handling #104

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

kennethjor
Copy link

@kennethjor kennethjor commented Nov 16, 2021

Implemented @CookieParam support for String and Netty's Cookie type.

@kennethjor kennethjor requested a review from chtyim November 16, 2021 11:45
@kennethjor kennethjor changed the title String-based cookie handling Cookie handling Nov 16, 2021
@kennethjor
Copy link
Author

kennethjor commented Nov 16, 2021

Updated the PR to support Netty's Cookie type as well.

For reference, I did try and add support for javax.ws.rs.core.Cookie, but got the following exception during my testing:

01:15:41.488 [test-worker-thread-0] ERROR io.cdap.http.internal.HttpDispatcher - Exception caught in channel processing.
io.cdap.http.internal.HandlerException: Error in executing request: GET /test/v1/jaxCookieParam
	at io.cdap.http.internal.HttpResourceModel.handle(HttpResourceModel.java:161) ~[classes/:na]
	at io.cdap.http.internal.HttpResourceHandler.getDestinationMethod(HttpResourceHandler.java:292) ~[classes/:na]
	at io.cdap.http.internal.RequestRouter.prepareHandleMethod(RequestRouter.java:149) ~[classes/:na]
	at io.cdap.http.internal.RequestRouter.channelRead(RequestRouter.java:88) ~[classes/:na]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.handler.codec.http.HttpServerKeepAliveHandler.channelRead(HttpServerKeepAliveHandler.java:64) ~[netty-codec-http-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) ~[netty-codec-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[netty-codec-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:438) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) ~[netty-codec-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284) ~[netty-codec-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) ~[netty-transport-4.1.16.Final.jar:4.1.16.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) ~[netty-common-4.1.16.Final.jar:4.1.16.Final]
	at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
Caused by: java.lang.ExceptionInInitializerError: null
	at io.cdap.http.internal.ParamConvertUtils$4.convert(ParamConvertUtils.java:125) ~[classes/:na]
	at io.cdap.http.internal.ParamConvertUtils$4.convert(ParamConvertUtils.java:121) ~[classes/:na]
	at io.cdap.http.internal.HttpResourceModel$ParameterInfo.convert(HttpResourceModel.java:326) ~[classes/:na]
	at io.cdap.http.internal.HttpResourceModel.getCookieParamValue(HttpResourceModel.java:215) ~[classes/:na]
	at io.cdap.http.internal.HttpResourceModel.handle(HttpResourceModel.java:148) ~[classes/:na]
	... 38 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
	at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:152) ~[javax.ws.rs-api-2.0.jar:2.0]
	at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120) ~[javax.ws.rs-api-2.0.jar:2.0]
	at javax.ws.rs.core.Cookie.<clinit>(Cookie.java:61) ~[javax.ws.rs-api-2.0.jar:2.0]
	... 43 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
	at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
	at java.base/java.lang.Class.forName(Class.java:315) ~[na:na]
	at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:114) ~[javax.ws.rs-api-2.0.jar:2.0]
	at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:207) ~[javax.ws.rs-api-2.0.jar:2.0]
	at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135) ~[javax.ws.rs-api-2.0.jar:2.0]
	... 45 common frames omitted

If you know how to fix this, I'll gladly re-add support for JAX-RS cookies as well.

@kennethjor
Copy link
Author

@chtyim Ping :)

@kennethjor
Copy link
Author

@chtyim Another gentle reminder.

@kennethjor
Copy link
Author

@chtyim Any change of getting this merged?

Copy link
Contributor

@albertshau albertshau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one actual comment, the rest are just about code style.

Thanks for the contribution!

@@ -125,6 +130,8 @@ public HttpMethodInfo handle(HttpRequest request,
if (httpMethods.contains(request.method())) {
//Setup args for reflection call
Object [] args = new Object[paramsInfo.size()];
// Parse cookies
Map<String, Cookie> cookies = cookieParser.parseCookies(request);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the problems with parsing here is that an invalid cookie will cause the request to fail, even if the method doesn't care about it. It could potentially cause backwards incompatibilities, as somebody could upgrade this library and then see their requests failing due to some headers they never looked at before.

instead of parsing all the cookies here, it would be better to only lookup the relevant header in the getCookieParamValue() method and decode it there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved it to the method so it will only be run on-demand. It does mean though that every cookie will result in parsing all the cookies. Is there a way to cache this? All the members on the HttpResourceModel are final and set in the constructor, so I wasn't sure if it would be ok to add a cookie parsing cache there, which is the approach I'd normally take.

src/test/java/io/cdap/http/internal/CookieParserTest.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/TestHandler.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/TestHandler.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/TestHandler.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/HttpServerTest.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/HttpServerTest.java Outdated Show resolved Hide resolved
src/test/java/io/cdap/http/HttpServerTest.java Outdated Show resolved Hide resolved
@kennethjor kennethjor requested review from albertshau and chtyim and removed request for chtyim and albertshau November 5, 2022 07:41
@kennethjor kennethjor requested review from albertshau and removed request for chtyim March 2, 2023 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants