restlet-framework-java icon indicating copy to clipboard operation
restlet-framework-java copied to clipboard

java.lang.ClassCastException: org.restlet.data.Parameter cannot be cast to org.restlet.data.Header

Open mastershifu opened this issue 11 years ago • 16 comments

I'm using restlet 2.3.1 with CorsService. Http calls do return content as expected, but the following messages appear in the log:

Apr 27, 2015 2:30:43 PM org.restlet.engine.adapter.ServerAdapter addResponseHeaders
WARNING: Exception intercepted while adding the response headers
java.lang.ClassCastException: org.restlet.data.Parameter cannot be cast to org.restlet.data.Header
        at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:220)
        at org.restlet.engine.header.HeaderUtils.addResponseHeaders(HeaderUtils.java:641)
        at org.restlet.engine.adapter.ServerAdapter.addResponseHeaders(ServerAdapter.java:83)
        at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:184)
        at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
        at org.restlet.ext.simple.internal.SimpleContainer.handle(SimpleContainer.java:74)
        at org.simpleframework.http.core.Dispatcher.dispatch(Dispatcher.java:121)
        at org.simpleframework.http.core.Dispatcher.run(Dispatcher.java:103)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

Can you advice?

mastershifu avatar Apr 27 '15 14:04 mastershifu

It seems to me like you have two different version of Restlet for org.restlet.jar and org.restlet.ext.simple.jar.

jlouvel avatar Apr 28 '15 04:04 jlouvel

My project is configured with the following maven deps:

org.restlet.jse org.restlet 2.3.1 org.restlet.jse org.restlet.ext.simple 2.3.1

Building the project brings the following jars into packaging:

org.restlet.ext.simple-2.3.1.jar org.restlet-2.3.1.jar simple-5.1.6.jar

Is 5.1.6 the expected version?

Running mvn dependency:tree shows:

[INFO] | +- org.restlet.jse:org.restlet:jar:2.3.1:compile [INFO] | +- org.restlet.jse:org.restlet.ext.simple:jar:2.3.1:compile [INFO] | | - org.simpleframework:simple:jar:5.1.6:compile

mastershifu avatar Apr 28 '15 19:04 mastershifu

I verified the source code, but the only possibility I see is a mix between separate version of RF JARs (such as 2.2 and 2.3). Could you double check your dependencies in the classpath?

jlouvel avatar Apr 28 '15 21:04 jlouvel

Strange... Basically this app is a WAR deployed into Tomcat instance, and the only relevant jars under WEB_INF/lib are: org.restlet.ext.simple-2.3.1.jar,org.restlet-2.3.1.jar,simple-5.1.6.jar

Can you direct me to possible points in the code where the response headers are set as Parameter? I'll try to debug and determine what may went wrong..

mastershifu avatar Apr 29 '15 07:04 mastershifu

In addition, after setting jvm flag -verbose:class, the output indicates that only classes from org.restlet.ext.simple-2.3.1.jar/org.restlet-2.3.1.jar/simple-5.1.6.jar are indeed loaded. Could it be the artifacts at http://maven.restlet.com are somehow mal-packaged?

mastershifu avatar Apr 29 '15 19:04 mastershifu

Could you try switching to the Jetty extension and see if it still occurs? @thboileau Do you know why @mastershifu is getting this error?

jlouvel avatar Apr 29 '15 21:04 jlouvel

Switched to jetty, same errors, again this is happening only with CORS requests:

Apr 30, 2015 6:59:46 AM org.restlet.engine.adapter.ServerAdapter addResponseHeaders
WARNING: Exception intercepted while adding the response headers
java.lang.ClassCastException: org.restlet.data.Parameter cannot be cast to org.restlet.data.Header
        at org.restlet.engine.header.HeaderUtils.addExtensionHeaders(HeaderUtils.java:220)
        at org.restlet.engine.header.HeaderUtils.addResponseHeaders(HeaderUtils.java:641)
        at org.restlet.engine.adapter.ServerAdapter.addResponseHeaders(ServerAdapter.java:83)
        at org.restlet.engine.adapter.ServerAdapter.commit(ServerAdapter.java:184)
        at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:144)
        at org.restlet.ext.jetty.JettyServerHelper$WrappedServer.handle(JettyServerHelper.java:273)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:313)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)
        at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:626)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:546)
        at java.lang.Thread.run(Thread.java:745)

My server is configured as follow:

Application myApp = new Application()
{
    @Override
    public org.restlet.Restlet createInboundRoot()
    {
    router.setContext(getContext());

    return router;
   };
};

Component component = new Component();

component.getDefaultHost().attach( "/", myApp );

CorsService corsService = new CorsService();         
corsService.setAllowedOrigins( new HashSet<String>(Arrays.asList("*")));
corsService.setAllowedCredentials(true);
corsService.setSkippingResourceForCorsOptions( true );

component.getServices().add( corsService );

new Server( Protocol.HTTP, port, component ).start();

mastershifu avatar Apr 30 '15 07:04 mastershifu

@thboileau Is it related as well to this bug/commit you just fixed? https://github.com/restlet/restlet-framework-java/commit/92ce087668b878eefa83f7d03342fe102d2baf9e

jlouvel avatar Apr 30 '15 17:04 jlouvel

@jlouvel , well I don't think so, but I check.

thboileau avatar Apr 30 '15 17:04 thboileau

Is there a way to push this fix to 2.3.x so I can test it? Should I downgrade to 2.2.x?

mastershifu avatar Apr 30 '15 18:04 mastershifu

Hi @mastershifu , this fix is available from the apispark-v3.8.1 tag (build from the 2.3 branch).

This issue is quite difficult to understand. I don't see source code that put Parameter instance instead of Header. Do you have any code that handles headers?

thboileau avatar Apr 30 '15 18:04 thboileau

Do you have any reproducible test case?

What do you mean by this is happening only with CORS requests? What kind of requests? Do you have any sample request?

thboileau avatar Apr 30 '15 18:04 thboileau

I'll describe the situation: I've some ServerResource serving GET request bundled into a WAR files and deployed to Tomcat instance. If I issue a request via browser or curl, the content is served as expected with 200OK. But if the same request is issued from a html page (hosted on the same tomcat instance), then content is still served, but the browser console reports 500 Server error, and the exception I attached above displayed in catalina.out, so I assumed this is something related with CORS...

I'll try to build a test case to reproduce that. On the other hand, do you have some test case that checks CORSService functionality?

mastershifu avatar Apr 30 '15 18:04 mastershifu

Sorry, there is no test case (in the org.restlet.test module) for cors, at this moment.

thboileau avatar Apr 30 '15 18:04 thboileau

Sorry, appear to be our side fault.. We implemented a CORS filter when we used 2.2.x, and forgot to remove it when switched to 2.3.1..

Sorry for the false alarm!

mastershifu avatar Apr 30 '15 19:04 mastershifu

Hi, that's fine, your problem is solved!

thboileau avatar Apr 30 '15 20:04 thboileau