How to get perspective session info in customized module?

I created a module to get local files via server, but errors happen to
PerspectiveSessionCollection psc = (PerspectiveSessionCollection) sess.getAttribute("perspective-session");

as below:
java.lang.ClassCastException: class com.inductiveautomation.perspective.gateway.session.PerspectiveSessionCollection cannot be cast to class com.inductiveautomation.perspective.gateway.session.PerspectiveSessionCollection (com.inductiveautomation.perspective.gateway.session.PerspectiveSessionCollection is in unnamed module of loader com.inductiveautomation.ignition.gateway.modules.ModuleClassLoader @7855ca07; com.inductiveautomation.perspective.gateway.session.PerspectiveSessionCollection is in unnamed module of loader com.inductiveautomation.ignition.gateway.modules.ModuleClassLoader @4d028b45)

How to use perspective session in proper way, any examples would be appreciated!

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	String pathInfo = req.getPathInfo();

	String project = "demo";
	HttpSession sess = req.getSession(false);


	sLogger.infof("140 %s", "sLogger.debugf(\"140 %s\", sess.getAttribute(\"perspective-session\"));");
	sLogger.infof("140 %s", sess.getAttribute("perspective-session")==null);

	Enumeration<String> keys = sess.getAttributeNames();
	while (keys.hasMoreElements()) {
		String s = keys.nextElement();
		sLogger.info("144" + s);
	}
				
	PerspectiveSessionCollection psc = (PerspectiveSessionCollection) sess.getAttribute("perspective-session");
	InternalSession isess = psc.getOrCreateSession(project);

	WebAuthUser wau = isess.getWebAuthStatus().getUser().get();
	sLogger.infof("154 %s", isess.getSessionInfo());

Ignition server version 8.1.3, perspective-gateway version: 2.1.3

Any ideas?

Is your module marked as depending on Perspective in your module.xml? Without that, you'll be loaded in an isolated class loader without automatic access to any other module's classes, though you can ask the module manager to resolve any class by name.

Yes, I am using Maven and marked perspective as depending perspective as below, how do I fix this issue?

    <dependencies>
		<dependency>
		    <groupId>com.inductiveautomation.perspective</groupId>
		    <artifactId>perspective-gateway</artifactId>
		    <version>2.1.3</version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>com.inductiveautomation.ignition</groupId>
		    <artifactId>gateway-api</artifactId>
		    <version>8.1.3</version>
		</dependency>
    </dependencies>

Your SDK dependencies need to be provided scope as well.

@Kevin.Herron

I import classes like:

import com.inductiveautomation.perspective.gateway.session.InternalSession;
import com.inductiveautomation.perspective.gateway.session.PerspectiveSessionCollection;
<dependencies>
		<dependency>
		    <groupId>com.inductiveautomation.perspective</groupId>
		    <artifactId>perspective-gateway</artifactId>
		    <version>2.1.3</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>
		<dependency>
		    <groupId>com.inductiveautomation.ignition</groupId>
		    <artifactId>gateway-api</artifactId>
		    <version>8.1.3</version>
			<scope>provided</scope>
		</dependency>
    </dependencies>

It cann't find class if it is provided scope.
java.lang.NoClassDefFoundError: com/inductiveautomation/perspective/gateway/session/PerspectiveSessionCollection

It sounds like your module isn’t declaring its dependency on the Perspective module.

Any examples or links could help me out?

for Maven looks something like this (change module ID):

1 Like