use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class TestApplicationServiceInitListener method getBaseUrl.
private static String getBaseUrl(IndexHtmlResponse indexHtmlResponse) {
VaadinServletRequest request = (VaadinServletRequest) indexHtmlResponse.getVaadinRequest();
String scheme = request.getScheme() + "://";
String serverName = request.getServerName();
String serverPort = (request.getServerPort() == 80) ? "" : ":" + request.getServerPort();
String contextPath = request.getContextPath();
return scheme + serverName + serverPort + contextPath;
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class PageView method beforeEnter.
@Override
public void beforeEnter(BeforeEnterEvent event) {
Input input = new Input();
input.setId("input");
input.clear();
Div updateButton = new Div();
updateButton.setId("button");
updateButton.setText("Update page title");
updateButton.addClickListener(e -> {
getPage().setTitle(input.getValue());
});
Div overrideButton = new Div();
overrideButton.setId("override");
overrideButton.setText("Triggers two updates");
overrideButton.addClickListener(e -> {
getPage().setTitle(input.getValue());
getPage().setTitle("OVERRIDDEN");
});
Div reloadButton = new Div();
reloadButton.setId("reload");
reloadButton.setText("Reloads the page");
reloadButton.addClickListener(e -> {
getPage().reload();
});
VaadinServletRequest request = (VaadinServletRequest) VaadinRequest.getCurrent();
HttpServletRequest httpServletRequest = request.getHttpServletRequest();
String url = httpServletRequest.getRequestURI().replace(PageView.class.getName(), BaseHrefView.class.getName());
Div setLocationButton = new Div();
setLocationButton.setId("setLocation");
setLocationButton.setText("Set page location");
setLocationButton.addClickListener(e -> getPage().setLocation(url));
Div openButton = new Div();
openButton.setId("open");
openButton.setText("Open url in a new tab");
openButton.addClickListener(e -> getPage().open(url));
IFrame frame = new IFrame();
frame.setId("newWindow");
frame.setName("newWindow");
Div openButton2 = new Div();
openButton2.setId("openInIFrame");
openButton2.setText("Open url in an IFrame");
openButton2.addClickListener(e -> getPage().open(url, "newWindow"));
add(input, updateButton, overrideButton, reloadButton, setLocationButton, openButton, openButton2, frame);
add(new NativeButton("page.fetchURL", onClickEvent -> {
getUI().ifPresent(ui -> ui.getPage().fetchCurrentURL(currentUrl -> {
LoggerFactory.getLogger(PageView.class.getName()).info(currentUrl.toString());
}));
}));
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class TrackMessageSizeView method findMethodImplementation.
private String findMethodImplementation() {
String filename = "/VAADIN/static/push/vaadinPush.js";
VaadinRequest request = VaadinRequest.getCurrent();
HttpServletRequest httpServletRequest = ((VaadinServletRequest) request).getHttpServletRequest();
String jsPath = httpServletRequest.getRequestURL().toString().replace(httpServletRequest.getRequestURI(), filename);
String content = getFileContent(jsPath);
if (content == null) {
log("Can't find " + jsPath);
return null;
}
// Find the function inside the script content
int startIndex = content.indexOf("function _trackMessageSize");
if (startIndex == -1) {
log("function not found");
return null;
}
// Assumes there's a /** comment before the next function
int endIndex = content.indexOf("/**", startIndex);
if (endIndex == -1) {
log("End of function not found");
return null;
}
content = content.substring(startIndex, endIndex);
content = content.replaceAll("jQuery", "jQueryVaadin");
return content;
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class FaviconHandler method handleRequest.
@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
VaadinServletRequest httpRequest = (VaadinServletRequest) request;
boolean isFavicon = httpRequest.getContextPath().isEmpty() && httpRequest.getServletPath().isEmpty() && "/favicon.ico".equals(httpRequest.getPathInfo());
if (isFavicon) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
return isFavicon;
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class ApplicationRunnerServlet method findDeploymentConfiguration.
private DeploymentConfiguration findDeploymentConfiguration(DeploymentConfiguration originalConfiguration) throws Exception {
// First level of cache
DeploymentConfiguration configuration = CurrentInstance.get(DeploymentConfiguration.class);
if (configuration == null) {
// Not in cache, try to find a VaadinSession to get it from
VaadinSession session = VaadinSession.getCurrent();
if (session == null) {
/*
* There's no current session, request or response when serving
* static resources, but there's still the current request
* maintained by ApplicationRunnerServlet, and there's most
* likely also a HttpSession containing a VaadinSession for that
* request.
*/
HttpServletRequest currentRequest = VaadinServletService.getCurrentServletRequest();
if (currentRequest != null) {
HttpSession httpSession = currentRequest.getSession(false);
if (httpSession != null) {
Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance.setCurrent((VaadinSession) null);
try {
VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
session = service.findVaadinSession(new VaadinServletRequest(currentRequest, service));
} finally {
/*
* Clear some state set by findVaadinSession to
* avoid accidentally depending on it when coding on
* e.g. static request handling.
*/
CurrentInstance.restoreInstances(oldCurrent);
currentRequest.removeAttribute(VaadinSession.class.getName());
}
}
}
}
if (session != null) {
String name = ApplicationRunnerServlet.class.getName() + ".deploymentConfiguration";
try {
session.getLockInstance().lock();
/*
* Read attribute using reflection to bypass
* VaadinSesison.getAttribute which would cause an infinite
* loop when checking the production mode setting for
* determining whether to check that the session is locked.
*/
Field attributesField = VaadinSession.class.getDeclaredField("attributes");
attributesField.setAccessible(true);
Attributes sessionAttributes = (Attributes) attributesField.get(session);
configuration = (DeploymentConfiguration) sessionAttributes.getAttribute(name);
if (configuration == null) {
ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet.getCurrent();
Class<?> classToRun;
try {
classToRun = servlet.getClassToRun();
} catch (ClassNotFoundException e) {
/*
* This happens e.g. if the UI class defined in the
* URL is not found or if this servlet just serves
* static resources while there's some other servlet
* that serves the UI (e.g. when using /run-push/).
*/
return originalConfiguration;
}
CustomDeploymentConfiguration customDeploymentConfiguration = classToRun.getAnnotation(CustomDeploymentConfiguration.class);
if (customDeploymentConfiguration != null) {
Properties initParameters = new Properties(originalConfiguration.getInitParameters());
for (Conf entry : customDeploymentConfiguration.value()) {
initParameters.put(entry.name(), entry.value());
}
initParameters.put(VaadinSession.UI_PARAMETER, getApplicationRunnerApplicationClassName(request.get()));
configuration = new DefaultDeploymentConfiguration(ApplicationConfiguration.get(getService().getContext()), servlet.getClass(), initParameters);
} else {
configuration = originalConfiguration;
}
sessionAttributes.setAttribute(name, configuration);
}
} finally {
session.getLockInstance().unlock();
}
CurrentInstance.set(DeploymentConfiguration.class, configuration);
} else {
configuration = originalConfiguration;
}
}
return configuration;
}
Aggregations