NPE on running PageContext methods from within websockets

Bit of background - I’m fooling around with tomcat 8’s built-in websocket
support using POJO ServerEndpoints. Really cool btw… Anyways, I thought
it would be a neat experiment to throw the Lucee jars into the app and try
to access Lucee info from the websocket endpoint.

In general, I’m following this
example: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup

I realized I could use the configurator to throw in stuff to the config’s
userproperties, so that seemed a natural place to put this stuff. I just
can’t seem to get it to…work. To the code example:

package websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;

public class WebSocketBinderConfig extends
ServerEndpointConfig.Configurator {

@Override
public void modifyHandshake(ServerEndpointConfig config, 

HandshakeRequest request, HandshakeResponse response) {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PageContext pc = engine.getThreadPageContext(); //so far so good
try {

String appname = pc.applicationScope().get(“ApplicationName”); // Throws
NPE - any ideas as to why? I do not get any stack trace info past this
line.

String appname = pc.getConfig().getId(); // for kicks and grins, i tried
to just use the config id instead of the above line, but even just the call
to getConfig() throws a NPE

config.getUserProperties().put(“lcappname”, appname);

} catch (PageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

package websocket;

import java.io.IOException;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/websocket/{channel-id}",
configurator=WebSocketBinderConfig.class)
public class WebSocketBinder {
public WebSocketBinder() {
}
@OnOpen
public void onSessionStart(Session session, EndpointConfig config) throws
PageException {
session.addMessageHandler(new PongHandler(session,config));
try {
session.getBasicRemote().sendText("welcome aboard!
"+config.getUserProperties().get(“lcappname”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnClose
public void onSessionClose() {
}
}

I’d appreciate any input, thanks!

Ok… I’ve found out why the NPE - engine.getThreadPageContext() is
returning null.

Would it be just getPageContext? (From memory)

Mark Drew

  • Sent by typing with my thumbs.> On 28 Aug 2015, at 18:38, Jesse Shaffer <@Jesse_Shaffer> wrote:

Ok… I’ve found out why the NPE - engine.getThreadPageContext() is returning null.

See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/

You received this message because you are subscribed to the Google Groups “Lucee” group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/8a9bddf2-b2b5-4fc3-aa38-e3ac42c4e927%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hey Jesse, did you ever get something working with tomcat’s websocket
support? I would be very interested in checking it out and possibly
collaborating on it with you.On Thursday, August 27, 2015 at 11:57:10 PM UTC-6, Jesse Shaffer wrote:

Bit of background - I’m fooling around with tomcat 8’s built-in websocket
support using POJO ServerEndpoints. Really cool btw… Anyways, I thought
it would be a neat experiment to throw the Lucee jars into the app and try
to access Lucee info from the websocket endpoint.

In general, I’m following this example:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup

I realized I could use the configurator to throw in stuff to the config’s
userproperties, so that seemed a natural place to put this stuff. I just
can’t seem to get it to…work. To the code example:

package websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;

public class WebSocketBinderConfig extends
ServerEndpointConfig.Configurator {

@Override
public void modifyHandshake(ServerEndpointConfig config, 

HandshakeRequest request, HandshakeResponse response) {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PageContext pc = engine.getThreadPageContext(); //so far so good
try {

String appname = pc.applicationScope().get(“ApplicationName”); // Throws
NPE - any ideas as to why? I do not get any stack trace info past this
line.

String appname = pc.getConfig().getId(); // for kicks and grins, i
tried to just use the config id instead of the above line, but even just
the call to getConfig() throws a NPE

config.getUserProperties().put(“lcappname”, appname);

} catch (PageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

package websocket;

import java.io.IOException;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/websocket/{channel-id}",
configurator=WebSocketBinderConfig.class)
public class WebSocketBinder {
public WebSocketBinder() {
}
@OnOpen
public void onSessionStart(Session session, EndpointConfig config) throws
PageException {
session.addMessageHandler(new PongHandler(session,config));
try {
session.getBasicRemote().sendText("welcome aboard!
"+config.getUserProperties().get(“lcappname”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnClose
public void onSessionClose() {
}
}

I’d appreciate any input, thanks!

|PageContext pc = engine.getThreadPageContext(); //so far so good|

Are you sure about “so far so good”?

IMO pc is null and therefore any subsequent call to pc.anything() throws
an NPE. The problem is that this thread doesn’t go through Lucee, so
the PageContext is not registered for the thread in the CFMLEngine.

Igal Sapir
Lucee Core Developer
Lucee.org http://lucee.org/On 9/23/2016 8:49 PM, JP wrote:

Hey Jesse, did you ever get something working with tomcat’s websocket
support? I would be very interested in checking it out and possibly
collaborating on it with you.

On Thursday, August 27, 2015 at 11:57:10 PM UTC-6, Jesse Shaffer wrote:

Bit of background - I'm fooling around with tomcat 8's built-in
websocket support using POJO ServerEndpoints.  Really cool btw...
Anyways, I thought it would be a neat experiment to throw the
Lucee jars into the app and try to access Lucee info from the
websocket endpoint.

In general, I'm following this example:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup
<http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup>

I realized I could use the configurator to throw in stuff to the
config's userproperties, so that seemed a natural place to put
this stuff.  I just can't seem to get it to...work.  To the code
example:

|
package websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;

public class WebSocketBinderConfig extends
ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request, HandshakeResponse response) {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PageContext pc = engine.getThreadPageContext(); //so far so good
try {

String appname = pc.applicationScope().get("ApplicationName"); //
*Throws NPE - any ideas as to why? I do not get any stack trace
info past this line.*
String appname = pc.getConfig().getId(); // *for kicks and grins,
i tried to just use the config id instead of the above line, but
even just the call to getConfig() throws a NPE*
config.getUserProperties().put("lcappname", appname);

} catch (PageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
}



package websocket;


import java.io.IOException;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint(value="/websocket/{channel-id}",
configurator=WebSocketBinderConfig.class)
public class WebSocketBinder {
public WebSocketBinder() {
}
@OnOpen
public void onSessionStart(Session session, EndpointConfig config)
throws PageException {
session.addMessageHandler(new PongHandler(session,config));
try {
session.getBasicRemote().sendText("welcome aboard!
"+config.getUserProperties().get("lcappname"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnClose
public void onSessionClose() {
}
}

|


I'd appreciate any input, thanks!


Get 10% off of the regular price for this years CFCamp in Munich,
Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp.
189€ instead of 210€. Visit
https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp

You received this message because you are subscribed to the Google
Groups “Lucee” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to lucee+unsubscribe@googlegroups.com
mailto:lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com
mailto:lucee@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/lucee/e65a96e2-8bda-46da-b6d8-18504891824a%40googlegroups.com
https://groups.google.com/d/msgid/lucee/e65a96e2-8bda-46da-b6d8-18504891824a%40googlegroups.com?utm_medium=email&utm_source=footer.
For more options, visit https://groups.google.com/d/optout.

I ran into the same problem. Referring to this
article http://docs.lucee.org/guides/working-with-source/java-using-lucee-in-java.html,
and if I’m reading it correctly, you would get the page context if Lucee
loaded your .jar at startup time. If not, there is another way to do it by
simulating a request, see
: http://docs.lucee.org/guides/working-with-source/java-using-lucee-in-java.html#when-lucee-does-not-load-your-java-code

But again, this article is super lightweight and only includes some
snippets of code that don’t run without having to dig to figure out what
classes to include, etc. I would love to see this article expanded with
some working code examples because it seems like a very powerful way to
integrate Java libraries into your Lucee apps.On Thursday, August 27, 2015 at 11:57:10 PM UTC-6, Jesse Shaffer wrote:

Bit of background - I’m fooling around with tomcat 8’s built-in websocket
support using POJO ServerEndpoints. Really cool btw… Anyways, I thought
it would be a neat experiment to throw the Lucee jars into the app and try
to access Lucee info from the websocket endpoint.

In general, I’m following this example:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup

I realized I could use the configurator to throw in stuff to the config’s
userproperties, so that seemed a natural place to put this stuff. I just
can’t seem to get it to…work. To the code example:

package websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;

public class WebSocketBinderConfig extends
ServerEndpointConfig.Configurator {

@Override
public void modifyHandshake(ServerEndpointConfig config, 

HandshakeRequest request, HandshakeResponse response) {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PageContext pc = engine.getThreadPageContext(); //so far so good
try {

String appname = pc.applicationScope().get(“ApplicationName”); // Throws
NPE - any ideas as to why? I do not get any stack trace info past this
line.

String appname = pc.getConfig().getId(); // for kicks and grins, i
tried to just use the config id instead of the above line, but even just
the call to getConfig() throws a NPE

config.getUserProperties().put(“lcappname”, appname);

} catch (PageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

package websocket;

import java.io.IOException;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/websocket/{channel-id}",
configurator=WebSocketBinderConfig.class)
public class WebSocketBinder {
public WebSocketBinder() {
}
@OnOpen
public void onSessionStart(Session session, EndpointConfig config) throws
PageException {
session.addMessageHandler(new PongHandler(session,config));
try {
session.getBasicRemote().sendText("welcome aboard!
"+config.getUserProperties().get(“lcappname”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnClose
public void onSessionClose() {
}
}

I’d appreciate any input, thanks!

Igal, I did realize that. I never got past it though. I think the engine wasn’t initialized by that point, and I couldn’t initialize it because websockets run outside the normal servlet context AFAICT.

Right. The Websocket request runs outside of the Lucee servlet context
and that’s the problem.

It’d be great if we’d have access to scopes like the Session scope from
outside of the Lucee servlet, but I don’t think that it’s possible ATM.
There are security implications, of course, if we allow external
processes to access the Session manager.

We’ll have to see what Micha thinks about exposing the Session manager.

In the meantime, I posted this question on StackOverflow –


– perhaps someone with deeper knowledge of JSR-356 will have an idea.

Igal Sapir
Lucee Core Developer
Lucee.org http://lucee.org/On 9/25/2016 10:26 PM, Jesse Shaffer wrote:

Igal, I did realize that. I never got past it though. I think the engine wasn’t initialized by that point, and I couldn’t initialize it because websockets run outside the normal servlet context AFAICT.

You can create a PageContext, yes, but it is not connected to the
correct CFML Application, and therefore you can not access the
Application’s Session manager, etc., which is probably what you want to do.

Igal Sapir
Lucee Core Developer
Lucee.org http://lucee.org/On 9/26/2016 8:33 AM, JP wrote:

I ran into the same problem. Referring to this
article http://docs.lucee.org/guides/working-with-source/java-using-lucee-in-java.html,
and if I’m reading it correctly, you would get the page context if
Lucee loaded your .jar at startup time. If not, there is another way
to do it by simulating a request, see
: http://docs.lucee.org/guides/working-with-source/java-using-lucee-in-java.html#when-lucee-does-not-load-your-java-code

But again, this article is super lightweight and only includes some
snippets of code that don’t run without having to dig to figure out
what classes to include, etc. I would love to see this article
expanded with some working code examples because it seems like a very
powerful way to integrate Java libraries into your Lucee apps.

On Thursday, August 27, 2015 at 11:57:10 PM UTC-6, Jesse Shaffer wrote:

Bit of background - I'm fooling around with tomcat 8's built-in
websocket support using POJO ServerEndpoints.  Really cool btw...
Anyways, I thought it would be a neat experiment to throw the
Lucee jars into the app and try to access Lucee info from the
websocket endpoint.

In general, I'm following this example:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup
<http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?view=markup>

I realized I could use the configurator to throw in stuff to the
config's userproperties, so that seemed a natural place to put
this stuff.  I just can't seem to get it to...work.  To the code
example:

|
package websocket;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;

public class WebSocketBinderConfig extends
ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request, HandshakeResponse response) {
CFMLEngine engine = CFMLEngineFactory.getInstance();
PageContext pc = engine.getThreadPageContext(); //so far so good
try {

String appname = pc.applicationScope().get("ApplicationName"); //
*Throws NPE - any ideas as to why? I do not get any stack trace
info past this line.*
String appname = pc.getConfig().getId(); // *for kicks and grins,
i tried to just use the config id instead of the above line, but
even just the call to getConfig() throws a NPE*
config.getUserProperties().put("lcappname", appname);

} catch (PageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
}



package websocket;


import java.io.IOException;

import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint(value="/websocket/{channel-id}",
configurator=WebSocketBinderConfig.class)
public class WebSocketBinder {
public WebSocketBinder() {
}
@OnOpen
public void onSessionStart(Session session, EndpointConfig config)
throws PageException {
session.addMessageHandler(new PongHandler(session,config));
try {
session.getBasicRemote().sendText("welcome aboard!
"+config.getUserProperties().get("lcappname"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnClose
public void onSessionClose() {
}
}

|


I'd appreciate any input, thanks!


Get 10% off of the regular price for this years CFCamp in Munich,
Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp.
189€ instead of 210€. Visit
https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp

You received this message because you are subscribed to the Google
Groups “Lucee” group.
To unsubscribe from this group and stop receiving emails from it, send
an email to lucee+unsubscribe@googlegroups.com
mailto:lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com
mailto:lucee@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/lucee/0a5665bc-8bd9-49ee-a57a-140356355711%40googlegroups.com
https://groups.google.com/d/msgid/lucee/0a5665bc-8bd9-49ee-a57a-140356355711%40googlegroups.com?utm_medium=email&utm_source=footer.
For more options, visit https://groups.google.com/d/optout.