下面列出了怎么用com.vaadin.server.WrappedSession的API类实例代码及写法,或者点击链接到github查看源代码。
private void configureSessionStore() {
final Registration sessionInitListenerReg = this.service.addSessionInitListener(event -> {
MessageConsumer<String> consumer = sessionExpiredHandler(vertx, msg ->
Optional.of(event.getSession().getSession())
.filter(session -> msg.body().equals(session.getId()))
.ifPresent(WrappedSession::invalidate));
AtomicReference<Registration> sessionDestroyListenerUnregister = new AtomicReference<>();
sessionDestroyListenerUnregister.set(
event.getService().addSessionDestroyListener(ev2 -> {
consumer.unregister();
sessionDestroyListenerUnregister.get().remove();
})
);
});
this.service.addServiceDestroyListener(event -> sessionInitListenerReg.remove());
}
protected void processLinkHandlerRequest(VaadinRequest request) {
WrappedSession wrappedSession = request.getWrappedSession();
//noinspection unchecked
Map<String, String> params =
(Map<String, String>) wrappedSession.getAttribute(LAST_REQUEST_PARAMS_ATTR);
params = params != null ? params : Collections.emptyMap();
try {
String action = (String) wrappedSession.getAttribute(LAST_REQUEST_ACTION_ATTR);
LinkHandler linkHandler = AppBeans.getPrototype(LinkHandler.NAME, app, action, params);
if (app.connection.isConnected() && linkHandler.canHandleLink()) {
linkHandler.handle();
} else {
app.linkHandler = linkHandler;
}
} catch (Exception e) {
error(new com.vaadin.server.ErrorEvent(e));
}
}
/**
* Called to handle the link.
*/
public void handle() {
try {
ExternalLinkContext linkContext = new ExternalLinkContext(requestParams, action, app);
for (LinkHandlerProcessor processor : processors) {
if (processor.canHandle(linkContext)) {
processor.handle(linkContext);
break;
}
}
} finally {
VaadinRequest request = VaadinService.getCurrentRequest();
WrappedSession wrappedSession = request.getWrappedSession();
wrappedSession.removeAttribute(AppUI.LAST_REQUEST_PARAMS_ATTR);
wrappedSession.removeAttribute(AppUI.LAST_REQUEST_ACTION_ATTR);
}
}
private void doDispatch(final List<TenantAwareEvent> events, final WrappedSession wrappedSession) {
final SecurityContext userContext = (SecurityContext) wrappedSession
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
final SecurityContext oldContext = SecurityContextHolder.getContext();
try {
SecurityContextHolder.setContext(userContext);
final List<EventContainer<TenantAwareEvent>> groupedEvents = groupEvents(events, userContext,
eventProvider);
vaadinUI.access(() -> {
if (vaadinSession.getState() != State.OPEN) {
return;
}
LOG.debug("UI EventBus aggregator of UI {} got lock on session.", vaadinUI.getUIId());
groupedEvents.forEach(holder -> eventBus.publish(vaadinUI, holder));
LOG.debug("UI EventBus aggregator of UI {} left lock on session.", vaadinUI.getUIId());
}).get();
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Wait for Vaadin session for UI {} interrupted!", vaadinUI.getUIId(), e);
Thread.currentThread().interrupt();
} finally {
SecurityContextHolder.setContext(oldContext);
}
}
protected boolean isLinkHandlerRequest(VaadinRequest request) {
WrappedSession wrappedSession = request.getWrappedSession();
if (wrappedSession == null) {
return false;
}
String action = (String) wrappedSession.getAttribute(LAST_REQUEST_ACTION_ATTR);
return webConfig.getLinkHandlerActions().contains(action);
}
@Override
public void run() {
LOG.debug("UI EventBus aggregator started for UI {}", vaadinUI.getUIId());
final long timestamp = System.currentTimeMillis();
final int size = queue.size();
if (size <= 0) {
LOG.debug("UI EventBus aggregator for UI {} has nothing to do.", vaadinUI.getUIId());
return;
}
final WrappedSession wrappedSession = vaadinSession.getSession();
if (wrappedSession == null) {
return;
}
final List<TenantAwareEvent> events = new ArrayList<>(size);
final int eventsSize = queue.drainTo(events);
if (events.isEmpty()) {
LOG.debug("UI EventBus aggregator for UI {} has nothing to do.", vaadinUI.getUIId());
return;
}
LOG.debug("UI EventBus aggregator dispatches {} events for session {} for UI {}", eventsSize, vaadinSession,
vaadinUI.getUIId());
doDispatch(events, wrappedSession);
LOG.debug("UI EventBus aggregator done with sending {} events in {} ms for UI {}", eventsSize,
System.currentTimeMillis() - timestamp, vaadinUI.getUIId());
}
@Override
public WrappedSession getWrappedSession() {
return getWrappedSession(true);
}
@Override
public WrappedSession getWrappedSession(boolean allowSessionCreation) {
return Optional.ofNullable(routingContext.session())
.map(ExtendedSession::adapt)
.map(VertxWrappedSession::new).orElse(null);
}
@Override
public WrappedSession getWrappedSession() {
return null;
}
@Override
public WrappedSession getWrappedSession(boolean allowSessionCreation) {
return null;
}