下面列出了怎么用com.vaadin.server.VaadinServletService的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public String getStaticFileLocation(VaadinRequest request) {
String staticFileLocation;
// if property is defined in configurations, use that
staticFileLocation = getDeploymentConfiguration().getResourcesPath();
if (staticFileLocation != null) {
return staticFileLocation;
}
VertxVaadinRequest vertxRequest = (VertxVaadinRequest) request;
String requestedPath = vertxRequest.getRequest().path()
.substring(
Optional.ofNullable(vertxRequest.getRoutingContext().mountPoint())
.map(String::length).orElse(0)
);
return VaadinServletService.getCancelingRelativePath(requestedPath);
}
@Override
public boolean validate(String username, String password) {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
try {
Authentication auth = this.authenticationManager.authenticate(token);
if (auth.isAuthenticated()) {
// execute session authentication strategy
if (this.sessionStrategy != null)
this.sessionStrategy.onAuthentication(auth, VaadinServletService.getCurrentServletRequest(),
VaadinServletService.getCurrentResponse());
SecurityContextHolder.getContext().setAuthentication(auth);
// save request in context session
VaadinSession.getCurrent().getSession().setAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
SecurityContextHolder.getContext());
return true;
}
SecurityContextHolder.clearContext();
return false;
}
catch(AuthenticationException ae) {
SecurityContextHolder.clearContext();
return false;
}
}
@Override
protected String getServiceUrl(BootstrapContext context) {
String pathInfo = context.getRequest().getPathInfo();
if (pathInfo == null) {
return null;
} else {
/*
* Make a relative URL to the servlet by adding one ../ for each
* path segment in pathInfo (i.e. the part of the requested path
* that comes after the servlet mapping)
*/
return VaadinServletService.getCancelingRelativePath(pathInfo);
}
}
public void listen(@Observes
AnswerSavedEvent event) {
RespondentAccount respondent = (RespondentAccount) VaadinServletService.getCurrentServletRequest()
.getUserPrincipal();
Integer questionnaireId = respondent.getGrantedquestionnaireIds().iterator().next();
Answer answer = event.getAnswer();
String questionCode = event.getQuestionCode();
logger.debug("Trying to save answer: {} for questionnair identified with id = {} and questionCode = {}",
answer, questionnaireId, questionCode);
questionnaireResource.saveAnswer(answer, questionnaireId, questionCode);
}
public static String getCancelingRelativePath(String servletPath) {
return VaadinServletService.getCancelingRelativePath(servletPath);
}
@Nullable
@Override
public DeviceInfo getDeviceInfo() {
// per request cache
HttpServletRequest currentServletRequest = VaadinServletService.getCurrentServletRequest();
if (currentServletRequest == null) {
return null;
}
DeviceInfo deviceInfo = (DeviceInfo) currentServletRequest.getAttribute(DeviceInfoProvider.NAME);
if (deviceInfo != null) {
return deviceInfo;
}
Page page = Page.getCurrent();
if (page == null) {
return null;
}
WebBrowser webBrowser = page.getWebBrowser();
DeviceInfo di = new DeviceInfo();
di.setAddress(webBrowser.getAddress());
di.setBrowserApplication(webBrowser.getBrowserApplication());
di.setBrowserMajorVersion(webBrowser.getBrowserMajorVersion());
di.setBrowserMinorVersion(webBrowser.getBrowserMinorVersion());
di.setChrome(webBrowser.isChrome());
di.setChromeFrame(webBrowser.isChromeFrame());
di.setChromeFrameCapable(webBrowser.isChromeFrameCapable());
di.setEdge(webBrowser.isEdge());
di.setFirefox(webBrowser.isFirefox());
di.setOpera(webBrowser.isOpera());
di.setIE(webBrowser.isIE());
di.setSafari(webBrowser.isSafari());
if (webBrowser.isWindows()) {
di.setOperatingSystem(OperatingSystem.WINDOWS);
} else if (webBrowser.isAndroid()) {
di.setOperatingSystem(OperatingSystem.ANDROID);
} else if (webBrowser.isIOS()) {
di.setOperatingSystem(OperatingSystem.IOS);
} else if (webBrowser.isMacOSX()) {
di.setOperatingSystem(OperatingSystem.MACOSX);
} else if (webBrowser.isLinux()) {
di.setOperatingSystem(OperatingSystem.LINUX);
}
di.setIPad(webBrowser.isIPad());
di.setIPhone(webBrowser.isIPhone());
di.setWindowsPhone(webBrowser.isWindowsPhone());
di.setSecureConnection(webBrowser.isSecureConnection());
di.setLocale(webBrowser.getLocale());
di.setScreenHeight(webBrowser.getScreenHeight());
di.setScreenWidth(webBrowser.getScreenWidth());
currentServletRequest.setAttribute(DeviceInfoProvider.NAME, di);
return di;
}
public TestVaadinSession(WebBrowser webBrowser, Locale locale) {
super(new VaadinServletService(){});
this.webBrowser = webBrowser;
setLocale(locale);
}
@Override
public void enter(ViewChangeEvent event) {
logger.debug("Entering {} view ", QuestionnaireView.NAME);
addStyleName(Reindeer.LAYOUT_BLUE);
addStyleName("questionnaires");
WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
Integer screenWidth = webBrowser.getScreenWidth();
Integer heightWidth = webBrowser.getScreenHeight();
logger.debug("Browser screen settings {} x {}", screenWidth, heightWidth);
if (heightWidth <= 480) {
renderingMode = RenderingMode.QUESTION_BY_QUESTION;
}
// centralLayout.addStyleName("questionnaires");
// new Responsive(centralLayout);
RespondentAccount respondent = (RespondentAccount) VaadinServletService.getCurrentServletRequest()
.getUserPrincipal();
if (respondent.hasPreferredLanguage()) {
preferredLanguage = Language.fromString(respondent.getPreferredLanguage());
} else {
preferredLanguage = Language.fromLocale(webBrowser.getLocale());
}
questionnaireId = respondent.getGrantedquestionnaireIds().iterator().next();
logger.debug("Trying to fetch questionnair identified with id = {} ", questionnaireId);
QuestionnaireDefinitionDTO definition = questionnaireResource.getDefinition(questionnaireId);
sectionInfoVisible = definition.isSectionInfoVisible();
QuestionnairePageDTO page = questionnaireResource.getPage(questionnaireId, renderingMode, preferredLanguage,
NavigationAction.ENTERING);
logger.debug("Displaying page {}/{} with {} questions", page.getMetadata().getNumber(), page.getMetadata()
.getCount(), page.getQuestions().size());
questionsLayout = new VerticalLayout();
update(page);
Label questionnaireTitle = new Label(definition.getLanguageSettings().getTitle());
questionnaireTitle.addStyleName(Reindeer.LABEL_H1);
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setSizeFull();
mainLayout.setMargin(true);
mainLayout.addComponent(questionnaireTitle);
mainLayout.addComponent(questionsLayout);
// Add the responsive capabilities to the components
Panel centralLayout = new Panel();
centralLayout.setContent(mainLayout);
centralLayout.setSizeFull();
centralLayout.getContent().setSizeUndefined();
Responsive.makeResponsive(questionnaireTitle);
setCompositionRoot(centralLayout);
setSizeFull();
}