4

I've been trying to subclass Wicket's WebSession so I can implement a basic authentication system. I have followed the guidelines in the Wicket reference library. When I try the following in my WebPage, I get a ClassCastException:

((AppSession)Session.get()).setUid()

Here is the full error:

java.lang.ClassCastException: org.apache.wicket.protocol.http.WebSession cannot be cast to com.example.webapp.AppSession

I've been searching the web for hours and tried everything I could. I would really appreciate some help. Also, please let me know if there is a better way of doing this. I'm really new to wicket.

Thank you.

AppSession.java

public final class AppSession extends WebSession {
    private Integer uid;

    public AppSession(Request request) {
        super(request);
    }

    public final Integer getUid() {
        return uid;
    }

    public final void setUid(Integer uid) {
        this.uid = uid;
    }

    public static AppSession get() {
        return (AppSession)Session.get();
    }
}

App.java

public class App extends WebApplication {
    public App() {
        super();
        new AnnotatedMountScanner().scanPackage("com.example.webapp").mount(this);
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new AppSession(request);     
    }

    @Override
    public Class getHomePage() {
        return null;
    }
}
Eonasdan
  • 7,563
  • 8
  • 55
  • 82

1 Answers1

1

I too have my own session class, descended from WebSession. I do a few things differently from you.

Add the AppSession method:

/**
 * Gets the session for the calling thread.
 * @return
 *   The session for the calling thread.
 */
public static AppSession get()
{
  return (AppSession)Session.get();
}

You might want to try

AppSession ssnSession = AppSession.get();
ssnSession.setUid(...);

instead of your one-line call.

Have you checked the packages of your (App)Session classes?

(I personally do not return null for your App.getHomePage().)

Does any of this help?

Ian Marshall
  • 760
  • 4
  • 14