use of org.apache.wicket.Session in project wicket by apache.
the class WicketTesterTest method sessionBinding.
/**
* Test for WICKET-3123
*/
@Test
public void sessionBinding() {
Session session = tester.getSession();
assertTrue(session.isTemporary());
session.bind();
assertFalse(session.isTemporary());
}
use of org.apache.wicket.Session in project wicket by apache.
the class AbstractRequestLogger method requestTime.
@Override
public void requestTime(long timeTaken) {
RequestData requestdata = RequestCycle.get().getMetaData(REQUEST_DATA);
if (requestdata != null) {
if (activeRequests.get() > 0) {
requestdata.setActiveRequest(activeRequests.decrementAndGet());
}
Session session = Session.exists() ? Session.get() : null;
String sessionId = session != null ? session.getId() : "N/A";
requestdata.setSessionId(sessionId);
Object sessionInfo = getSessionInfo(session);
requestdata.setSessionInfo(sessionInfo);
long sizeInBytes = -1;
if (Application.exists() && Application.get().getRequestLoggerSettings().getRecordSessionSize()) {
try {
sizeInBytes = session != null ? session.getSizeInBytes() : -1;
} catch (Exception e) {
// log the error and let the request logging continue (this is what happens in
// the detach phase of the request cycle anyway. This provides better
// diagnostics).
LOG.error("Exception while determining the size of the session in the request logger: " + e.getMessage(), e);
}
}
requestdata.setSessionSize(sizeInBytes);
requestdata.setTimeTaken(timeTaken);
addRequest(requestdata);
SessionData sessiondata;
if (sessionId != null) {
sessiondata = liveSessions.get(sessionId);
if (sessiondata == null) {
// if the session has been destroyed during the request by
// Session#invalidateNow, retrieve the old session data from the RequestCycle.
sessiondata = RequestCycle.get().getMetaData(SESSION_DATA);
}
if (sessiondata == null) {
// passivated session or logger only started after it.
sessionCreated(sessionId);
sessiondata = liveSessions.get(sessionId);
}
if (sessiondata != null) {
sessiondata.setSessionInfo(sessionInfo);
sessiondata.setSessionSize(sizeInBytes);
sessiondata.addTimeTaken(timeTaken);
RequestCycle.get().setMetaData(SESSION_DATA, sessiondata);
}
}
}
}
use of org.apache.wicket.Session in project wicket by apache.
the class DateValidator method decorate.
@Override
protected IValidationError decorate(IValidationError error, IValidatable<Date> validatable) {
error = super.decorate(error, validatable);
if (error instanceof ValidationError) {
ValidationError ve = (ValidationError) error;
ve.setVariable("inputdate", validatable.getValue());
// format variables if format has been specified
if (format != null) {
Locale locale;
if (Session.exists()) {
Session session = Session.get();
locale = session.getLocale();
if (locale == null) {
locale = Locale.getDefault(Locale.Category.FORMAT);
}
} else {
locale = Locale.getDefault(Locale.Category.FORMAT);
}
SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
if (getMinimum() != null) {
ve.setVariable("minimum", sdf.format(getMinimum()));
}
if (getMaximum() != null) {
ve.setVariable("maximum", sdf.format(getMaximum()));
}
ve.setVariable("inputdate", sdf.format(validatable.getValue()));
}
}
return error;
}
use of org.apache.wicket.Session in project wicket by apache.
the class AbstractWebSocketProcessor method broadcastMessage.
/**
* Exports the Wicket thread locals and broadcasts the received message from the client to all
* interested components and behaviors in the page with id {@code #pageId}
* <p>
* Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
* components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
* doesn't expect response from the user.
* </p>
*
* @param message
* the message to broadcast
*/
public final void broadcastMessage(final IWebSocketMessage message) {
IKey key = getRegistryKey();
IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);
if (connection != null && (connection.isOpen() || message instanceof ClosedMessage)) {
Application oldApplication = ThreadContext.getApplication();
Session oldSession = ThreadContext.getSession();
RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();
WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
try {
WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
ThreadContext.setRequestCycle(requestCycle);
ThreadContext.setApplication(application);
Session session;
if (oldSession == null || message instanceof IWebSocketPushMessage) {
ISessionStore sessionStore = application.getSessionStore();
session = sessionStore.lookup(webRequest);
ThreadContext.setSession(session);
} else {
session = oldSession;
}
IPageManager pageManager = session.getPageManager();
Page page = getPage(pageManager);
if (page != null) {
WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);
WebSocketPayload payload = createEventPayload(message, requestHandler);
if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage || message instanceof AbortedMessage)) {
requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
}
IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
requestMapper.setHandler(broadcastingHandler);
requestCycle.processRequestAndDetach();
} else {
LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId);
}
} catch (Exception x) {
LOG.error("An error occurred during processing of a WebSocket message", x);
} finally {
try {
webResponse.close();
} finally {
ThreadContext.setApplication(oldApplication);
ThreadContext.setRequestCycle(oldRequestCycle);
ThreadContext.setSession(oldSession);
}
}
} else {
LOG.debug("Either there is no connection({}) or it is closed.", connection);
}
}
use of org.apache.wicket.Session in project wicket by apache.
the class AbstractInjectorTest method before.
@Before
public void before() {
app.setServletContext(new MockServletContext(app, null));
ThreadContext.setApplication(app);
app.setName(getClass().getName());
app.initApplication();
Session session = new WebSession(new MockWebRequest(Url.parse("/")));
app.getSessionStore().bind(null, session);
ThreadContext.setSession(session);
GuiceComponentInjector injector = new GuiceComponentInjector(app, new Module() {
@Override
public void configure(final Binder binder) {
binder.bind(ITestService.class).to(TestService.class);
binder.bind(ITestService.class).annotatedWith(Red.class).to(TestServiceRed.class);
binder.bind(ITestService.class).annotatedWith(Blue.class).to(TestServiceBlue.class);
binder.bind(new TypeLiteral<Map<String, String>>() {
}).toProvider(new Provider<Map<String, String>>() {
@Override
public Map<String, String> get() {
Map<String, String> strings = new HashMap<>();
strings.put(ITestService.RESULT, ITestService.RESULT);
return strings;
}
});
binder.bind(String.class).annotatedWith(Names.named("named1")).toInstance("NAMED_1");
binder.bind(String.class).annotatedWith(Names.named("named2")).toInstance("NAMED_2");
binder.bind(String.class).annotatedWith(new Jsr330Named("named1")).toInstance("NAMED_1");
binder.bind(String.class).annotatedWith(new Jsr330Named("named2")).toInstance("NAMED_2");
binder.bind(EvilTestService.class).toInstance(new EvilTestService("evil123", 5));
}
});
app.getComponentInstantiationListeners().add(injector);
}
Aggregations