下面列出了怎么用com.vaadin.server.VaadinService的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
}
public static UserDetails getCurrentUser() {
final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession()
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
Authentication authentication = context.getAuthentication();
if (authentication instanceof OAuth2AuthenticationToken) {
OidcUser oidcUser = (OidcUser) authentication.getPrincipal();
Object details = authentication.getDetails();
String tenant = "DEFAULT";
if (details instanceof TenantAwareAuthenticationDetails) {
tenant = ((TenantAwareAuthenticationDetails) details).getTenant();
}
return new UserPrincipal(oidcUser.getPreferredUsername(), "***", oidcUser.getGivenName(),
oidcUser.getFamilyName(), oidcUser.getPreferredUsername(), oidcUser.getEmail(), tenant,
oidcUser.getAuthorities());
} else {
return (UserDetails) authentication.getPrincipal();
}
}
@Override
public final void buttonClick(final ClickEvent event) {
final ServiceResponse response = getApplicationManager().service(logoutRequest);
if (ServiceResult.SUCCESS == response.getResult()) {
UI.getCurrent().getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
UI.getCurrent().getSession().close();
VaadinService.getCurrentRequest().getWrappedSession().invalidate();
} else {
showNotification(LOGOUT_FAILED,
ERROR_MESSAGE,
Notification.Type.WARNING_MESSAGE);
LOGGER.info(LOG_MSG_LOGOUT_FAILURE,logoutRequest.getSessionId());
}
}
/**
* Test if current request is an UIDL request
* @return true if in UIDL request, false otherwise
*/
private boolean isUidlRequest() {
VaadinRequest request = VaadinService.getCurrentRequest();
if (request == null)
return false;
String pathInfo = request.getPathInfo();
if (pathInfo == null) {
return false;
}
if (pathInfo.startsWith("/" + ApplicationConstants.UIDL_PATH)) {
return true;
}
return false;
}
protected WebBrowser getWebBrowserDetails() {
// timezone info is passed only on VaadinSession creation
WebBrowser webBrowser = VaadinSession.getCurrent().getBrowser();
VaadinRequest currentRequest = VaadinService.getCurrentRequest();
// update web browser instance if current request is not null
// it can be null in case of background/async processing of login request
if (currentRequest != null) {
webBrowser.updateRequestDetails(currentRequest);
}
return webBrowser;
}
@SuppressWarnings("unchecked")
@Override
@Nullable
public List<String> getUrls(String selectorId) {
VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
if (vaadinRequest != null)
return (List) vaadinRequest.getWrappedSession().getAttribute(getAttributeName(selectorId));
else {
HttpSession httpSession = getHttpSession();
return httpSession != null ? (List<String>) httpSession.getAttribute(getAttributeName(selectorId)) : null;
}
}
@Override
public void setUrls(String selectorId, List<String> urls) {
VaadinRequest vaadinRequest = VaadinService.getCurrentRequest();
if (vaadinRequest != null)
vaadinRequest.getWrappedSession().setAttribute(getAttributeName(selectorId), urls);
else {
HttpSession httpSession = getHttpSession();
if (httpSession != null) {
httpSession.setAttribute(getAttributeName(selectorId), urls);
}
}
}
private void redirectToMainView() {
// Close the VaadinServiceSession
getUI().getSession().close();
// Invalidate underlying session instead if login info is stored there
VaadinService.getCurrentRequest().getWrappedSession().invalidate();
getUI().getPage().setLocation( "/VAADIN" );
}
/**
* Exit application
*/
public static void exit() {
VaadinSession.getCurrent().getSession().removeAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
UI.getCurrent().close();
VaadinSession.getCurrent().close();
Page page = Page.getCurrent();
page.setLocation(VaadinService.getCurrentRequest().getContextPath() + "/logout");
}
private static void sendRefreshAndDisconnect(PushSocket socket) {
sendNotificationAndDisconnect(socket, VaadinService
.createCriticalNotificationJSON(null, null, null, null));
}
static String getUINotFoundErrorJSON(VaadinService service, VaadinRequest vaadinRequest) {
return UidlRequestHandler.getUINotFoundErrorJSON(service, vaadinRequest);
}
@Nullable
protected String getUserRemoteAddress() {
VaadinRequest currentRequest = VaadinService.getCurrentRequest();
return currentRequest != null ? currentRequest.getRemoteAddr() : null;
}
@Override
public VaadinService getService() {
return null;
}
private static String removePathEnd() {
return VaadinService.getCurrent().getBaseDirectory().getAbsolutePath().replace(getPathEnd(), "");
}
/**
* @return
*/
private String getApplicationUrl() {
return VaadinService.getCurrentRequest().getContextPath() + successUrl;
}
/**
* Instantiates a new RDF unit demo session.
*
* @param service the service
*/
public RDFUnitDemoSession(VaadinService service) {
super(service);
}