/** * Gets the currently processed Vaadin request. The current request is * automatically defined when the request is started. The current request * can not be used in e.g. background threads because of the way server * implementations reuse request instances. * * @return the current Vaadin request instance if available, otherwise * <code>null</code> * @see #setCurrentInstances(VaadinRequest, VaadinResponse) */ public static VaadinRequest getCurrentRequest() { return VaadinRequest.getCurrent(); }
/** * For internal use only. Updates all properties in the class according to * the given information. * * @param request * the Vaadin request to read the information from */ public void updateRequestDetails(VaadinRequest request) { locale = request.getLocale(); address = request.getRemoteAddr(); secureConnection = request.isSecure(); // Headers are case insensitive according to the specification but are // case sensitive in Weblogic portal... String agent = request.getHeader("User-Agent"); if (agent != null) { browserApplication = agent; browserDetails = new BrowserDetails(agent); } if (request.getParameter("v-sw") != null) { updateClientSideDetails(request.getParameter("v-sw"), request.getParameter("v-sh"), request.getParameter("v-tzo"), request.getParameter("v-rtzo"), request.getParameter("v-dstd"), request.getParameter("v-dston"), request.getParameter("v-tzid"), request.getParameter("v-curdate"), request.getParameter("v-td") != null); } }
@Override public InputStream getBody() throws IOException, UnsupportedOperationException { return request.getInputStream(); }
/** * Finds the class on on which page configuration annotation should be * defined. * * @param ui * the UI for which to do the lookup, not <code>null</code> * @param request * the request for which to do the lookup, not <code>null</code> * @return the class for which page configuration annotations should be * defined, or an empty optional if no such class is available */ public static Optional<Class<?>> resolvePageConfigurationHolder(UI ui, VaadinRequest request) { assert ui != null; assert request != null; if (ui.getRouter() == null) { return Optional.empty(); } return ui.getRouter().resolveNavigationTarget(request.getPathInfo(), request.getParameterMap()).map(navigationState -> { Class<? extends RouterLayout> parentLayout = getTopParentLayout( ui.getRouter(), navigationState); if (parentLayout != null) { return parentLayout; } return navigationState.getNavigationTarget(); }); }
@Override public String getRequestPath() { return request.getPathInfo(); }
@Override public Map<String, List<String>> getHeaders() { Map<String, List<String>> headers = new HashMap<>(); Enumeration<String> names = request.getHeaderNames(); if (names != null) { while (names.hasMoreElements()) { String name = names.nextElement(); String value = request.getHeader(name); if (value == null || value.trim().equals("")) { headers.put(name, Collections.emptyList()); } else { if (value.indexOf(',') > -1) { headers.put(name, Arrays.asList(value.split(","))); } else { headers.put(name, Collections.singletonList(value)); } } } } return headers; }
@Override public Map<String, List<String>> getRequestParameters() { return getQueryParametersMap(request.getParameterMap()); }
/** * Returns the UI class mapped for servlet that handles the given request. * <p> * This method is protected for testing purposes. * * @param request * the request for the UI * @return the UI class for the request */ protected static Class<? extends UI> getUIClass(VaadinRequest request) { String uiClassName = request.getService().getDeploymentConfiguration() .getUIClassName(); if (uiClassName == null) { throw new BootstrapException( "Could not determine the uiClassName for the request path " + request.getPathInfo()); } ClassLoader classLoader = request.getService().getClassLoader(); try { return Class.forName(uiClassName, true, classLoader) .asSubclass(UI.class); } catch (ClassNotFoundException e) { throw new BootstrapException( "Vaadin Servlet mapped to the request path " + request.getPathInfo() + " cannot find the mapped UI class with name " + uiClassName, e); } }
getRpcHandler(session).handleRpc(uI, request.getReader(), request); getLogger().warn( "Invalid security key received from {}", request.getRemoteHost());
@Override public String getRequestHost() { return request.getRemoteHost(); }
@Override public String getContextPath() { return request.getContextPath(); }
/** * * Read the contents of the given file from the classpath. * * @param request * the request for the ui * @param file * target file to read contents for * @return file contents as a {@link String} */ static String getDependencyContents(VaadinRequest request, String file) { Charset requestCharset = Optional .ofNullable(request.getCharacterEncoding()) .filter(string -> !string.isEmpty()).map(Charset::forName) .orElse(StandardCharsets.UTF_8); try (InputStream inlineResourceStream = getInlineResourceStream(request, file); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inlineResourceStream, requestCharset))) { return bufferedReader.lines() .collect(Collectors.joining(System.lineSeparator())); } catch (IOException e) { throw new IllegalStateException( String.format("Could not read file %s contents", file), e); } }
@Override public Optional<Cookie> getRequestCookie(String name) { ObjectUtils.argumentNotNull(name, "Cookie name must be not null"); javax.servlet.http.Cookie[] cookies = request.getCookies(); if (cookies != null) { for (javax.servlet.http.Cookie cookie : cookies) { if (name.equals(cookie.getName())) { return Optional.of(Cookie.builder().name(cookie.getName()).value(cookie.getValue()) .version(cookie.getVersion()).path(cookie.getPath()).domain(cookie.getDomain()).build()); } } } return Optional.empty(); }
/** * Called after the framework has handled a request and the response has * been written. * * @param request * The request object * @param response * The response object * @param session * The session which was used during the request or null if the * request did not use a session */ public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { if (session != null) { assert VaadinSession.getCurrent() == session; session.lock(); try { cleanupSession(session); final long duration = (System.nanoTime() - (Long) request .getAttribute(REQUEST_START_TIME_ATTRIBUTE)) / 1000000; session.setLastRequestDuration(duration); } finally { session.unlock(); } } CurrentInstance.clearAll(); }
/** * Enables navigation for a new UI instance. This initializes the UI content * based on the location used for loading the UI and sets up the UI to be * updated when the user navigates to some other location. * * @param ui * the UI that navigation should be set up for * @param initRequest * the Vaadin request that bootstraps the provided UI */ public void initializeUI(UI ui, VaadinRequest initRequest) { Location location = getLocationForRequest(initRequest.getPathInfo(), initRequest.getParameterMap()); ui.getPage().getHistory().setHistoryStateChangeHandler( e -> navigate(ui, e.getLocation(), e.getTrigger())); int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD); VaadinResponse response = VaadinService.getCurrentResponse(); if (response != null) { response.setStatus(statusCode); } }
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { String requestUri = request.getPathInfo(); if (pwaRegistry.getPwaConfiguration().isEnabled()) { if (requestHandlerMap.containsKey(requestUri)) { return requestHandlerMap.get(requestUri) .handleRequest(session,request,response); } else if (requestUri.startsWith("/"+PwaRegistry.WORKBOX_FOLDER)) { // allow only files under workbox_folder String resourceName = PwaRegistry.WORKBOX_FOLDER + requestUri // remove the extra '/' .substring(PwaRegistry.WORKBOX_FOLDER.length() + 1) .replaceAll("/", ""); return handleWorkboxResource(resourceName, response); } } return false; }
@Override public Optional<String> getRequestParameter(String name) { ObjectUtils.argumentNotNull(name, "Parameter name must be not null"); return getQueryParameterValue(request.getParameterMap(), name); }
/** * The request.getContentLength() is limited to "int" by the Servlet * specification. To support larger file uploads manually evaluate the * Content-Length header which can contain long values. */ private long getContentLength(VaadinRequest request) { try { return Long.parseLong(request.getHeader("Content-Length")); } catch (NumberFormatException e) { return -1l; } }
private static InitialPageSettings createInitialPageSettingsObject( BootstrapHandler.BootstrapContext context) { UI ui = context.getUI(); VaadinRequest request = context.getRequest(); WebBrowser browser = context.getSession().getBrowser(); String pathInfo = request.getPathInfo(); if (pathInfo == null) { pathInfo = ""; } else { assert pathInfo.startsWith("/"); pathInfo = pathInfo.substring(1); } Router router = ui.getRouter(); NavigationEvent navigationEvent = new NavigationEvent(router, new Location(pathInfo, QueryParameters.full(request.getParameterMap())), ui, NavigationTrigger.PAGE_LOAD); List<HasElement> components = ui.getChildren() .map(component -> (HasElement) component) .collect(Collectors.toList()); AfterNavigationEvent afterNavigationEvent = new AfterNavigationEvent( new LocationChangeEvent(navigationEvent.getSource(), navigationEvent.getUI(), navigationEvent.getTrigger(), navigationEvent.getLocation(), components)); return new InitialPageSettings(request, ui, afterNavigationEvent, browser); }