Search in sources :

Example 1 with SystemMessages

use of com.vaadin.flow.server.SystemMessages in project flow by vaadin.

the class ApplicationRunnerServlet method createServletService.

@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
    VaadinServletService service = super.createServletService(deploymentConfiguration);
    final SystemMessagesProvider provider = service.getSystemMessagesProvider();
    service.setSystemMessagesProvider((SystemMessagesProvider) systemMessagesInfo -> {
        if (systemMessagesInfo.getRequest() == null) {
            return provider.getSystemMessages(systemMessagesInfo);
        }
        Object messages = systemMessagesInfo.getRequest().getAttribute(CUSTOM_SYSTEM_MESSAGES_PROPERTY);
        if (messages instanceof SystemMessages) {
            return (SystemMessages) messages;
        }
        return provider.getSystemMessages(systemMessagesInfo);
    });
    return service;
}
Also used : Proxy(java.lang.reflect.Proxy) ServletException(javax.servlet.ServletException) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) URL(java.net.URL) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Map(java.util.Map) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) URI(java.net.URI) Method(java.lang.reflect.Method) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) SystemMessages(com.vaadin.flow.server.SystemMessages) Properties(java.util.Properties) Logger(org.slf4j.Logger) ServletConfig(javax.servlet.ServletConfig) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) IOException(java.io.IOException) File(java.io.File) Serializable(java.io.Serializable) WebServlet(javax.servlet.annotation.WebServlet) FileVisitResult(java.nio.file.FileVisitResult) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) VaadinService(com.vaadin.flow.server.VaadinService) InvocationHandler(java.lang.reflect.InvocationHandler) ServiceException(com.vaadin.flow.server.ServiceException) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) SystemMessagesProvider(com.vaadin.flow.server.SystemMessagesProvider) VaadinServletService(com.vaadin.flow.server.VaadinServletService) SystemMessages(com.vaadin.flow.server.SystemMessages)

Example 2 with SystemMessages

use of com.vaadin.flow.server.SystemMessages in project flow by vaadin.

the class PushHandler method callWithUi.

/**
 * Find the UI for the atmosphere resource, lock it and invoke the callback.
 *
 * @param resource
 *            the atmosphere resource for the current request
 * @param callback
 *            the push callback to call when a UI is found and locked
 * @param websocket
 *            true if this is a websocket message (as opposed to a HTTP
 *            request)
 */
private void callWithUi(final AtmosphereResource resource, final PushEventCallback callback, boolean websocket) {
    AtmosphereRequest req = resource.getRequest();
    VaadinServletRequest vaadinRequest = new VaadinServletRequest(req, service);
    VaadinSession session = null;
    if (websocket) {
        // For any HTTP request we have already started the request in the
        // servlet
        service.requestStart(vaadinRequest, null);
    }
    try {
        try {
            session = service.findVaadinSession(vaadinRequest);
            assert VaadinSession.getCurrent() == session;
        } catch (SessionExpiredException e) {
            sendNotificationAndDisconnect(resource, VaadinService.createSessionExpiredJSON());
            return;
        }
        UI ui = null;
        session.lock();
        try {
            ui = service.findUI(vaadinRequest);
            assert UI.getCurrent() == ui;
            if (ui == null) {
                sendNotificationAndDisconnect(resource, VaadinService.createUINotFoundJSON());
            } else {
                callback.run(resource, ui);
            }
        } catch (final IOException e) {
            callErrorHandler(session, e);
        } catch (final Exception e) {
            SystemMessages msg = service.getSystemMessages(ServletHelper.findLocale(null, vaadinRequest), vaadinRequest);
            AtmosphereResource errorResource = resource;
            if (ui != null && ui.getInternals().getPushConnection() != null) {
                // We MUST use the opened push connection if there is one.
                // Otherwise we will write the response to the wrong request
                // when using streaming (the client -> server request
                // instead of the opened push channel)
                errorResource = ((AtmospherePushConnection) ui.getInternals().getPushConnection()).getResource();
            }
            sendNotificationAndDisconnect(errorResource, VaadinService.createCriticalNotificationJSON(msg.getInternalErrorCaption(), msg.getInternalErrorMessage(), null, msg.getInternalErrorURL()));
            callErrorHandler(session, e);
        } finally {
            try {
                session.unlock();
            } catch (Exception e) {
                getLogger().warn("Error while unlocking session", e);
            // can't call ErrorHandler, we (hopefully) don't have a lock
            }
        }
    } finally {
        try {
            if (websocket) {
                service.requestEnd(vaadinRequest, null, session);
            }
        } catch (Exception e) {
            getLogger().warn("Error while ending request", e);
        // can't call ErrorHandler, we don't have a lock
        }
    }
}
Also used : AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) SystemMessages(com.vaadin.flow.server.SystemMessages) IOException(java.io.IOException) InvalidUIDLSecurityKeyException(com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException) JsonException(elemental.json.JsonException) SessionExpiredException(com.vaadin.flow.server.SessionExpiredException) IOException(java.io.IOException)

Example 3 with SystemMessages

use of com.vaadin.flow.server.SystemMessages in project flow by vaadin.

the class UidlWriter method createUidl.

/**
 * Creates a JSON object containing all pending changes to the given UI.
 *
 * @param ui
 *            The {@link UI} whose changes to write
 * @param async
 *            True if this message is sent by the server asynchronously,
 *            false if it is a response to a client message.
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async) {
    JsonObject response = Json.createObject();
    UIInternals uiInternals = ui.getInternals();
    VaadinSession session = ui.getSession();
    VaadinService service = session.getService();
    // Purge pending access calls as they might produce additional changes
    // to write out
    service.runPendingAccessTasks(session);
    // Paints components
    getLogger().debug("* Creating response to client");
    int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
    response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = ui.getSession().getService().getSystemMessages(ui.getLocale(), null);
    JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
    if (meta.keys().length > 0) {
        response.put("meta", meta);
    }
    JsonArray stateChanges = Json.createArray();
    JsonObject templates = Json.createObject();
    encodeChanges(ui, stateChanges, templates);
    populateDependencies(response, session, uiInternals.getDependencyList());
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    if (templates.keys().length > 0) {
        response.put("templates", templates);
    }
    List<JavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (ui.getSession().getService().getDeploymentConfiguration().isRequestTiming()) {
        response.put("timings", createPerformanceData(ui));
    }
    uiInternals.incrementServerId();
    return response;
}
Also used : JsonArray(elemental.json.JsonArray) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) SystemMessages(com.vaadin.flow.server.SystemMessages) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Aggregations

SystemMessages (com.vaadin.flow.server.SystemMessages)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 VaadinService (com.vaadin.flow.server.VaadinService)2 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)2 IOException (java.io.IOException)2 UI (com.vaadin.flow.component.UI)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)1 ServiceException (com.vaadin.flow.server.ServiceException)1 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)1 SystemMessagesProvider (com.vaadin.flow.server.SystemMessagesProvider)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)1 Conf (com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf)1 JsonArray (elemental.json.JsonArray)1 JsonException (elemental.json.JsonException)1