All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.server.ServletPortletHelper Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.server;

import java.io.Serializable;
import java.util.Locale;
import java.util.Properties;

import com.vaadin.shared.ApplicationConstants;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
import com.vaadin.util.ReflectTools;

/**
 * Contains helper methods shared by {@link VaadinServlet} and
 * {@code VaadinPortlet}.
 *
 * @deprecated As of 7.1. Will be removed or refactored in the future.
 */
@Deprecated
public class ServletPortletHelper implements Serializable {

    //
    // TODO: document these functions or use non-deprecated equivalents
    //

    public static final String UPLOAD_URL_PREFIX = "APP/UPLOAD/";
    /**
     * The default SystemMessages (read-only).
     */
    static final SystemMessages DEFAULT_SYSTEM_MESSAGES = new SystemMessages();

    public static Class getLegacyApplicationClass(
            VaadinService vaadinService) throws ServiceException {
        Properties initParameters = vaadinService.getDeploymentConfiguration()
                .getInitParameters();
        String applicationParameter = initParameters.getProperty("application");
        ClassLoader classLoader = vaadinService.getClassLoader();

        if (applicationParameter == null) {
            throw new ServiceException(
                    "No \"application\" init parameter found");
        }

        try {
            return classLoader.loadClass(applicationParameter)
                    .asSubclass(LegacyApplication.class);
        } catch (final ClassNotFoundException e) {
            throw new ServiceException(
                    "Failed to load application class: " + applicationParameter,
                    e);
        }
    }

    private static void verifyUIClass(String className, ClassLoader classLoader)
            throws ServiceException {
        if (className == null) {
            throw new ServiceException(
                    VaadinSession.UI_PARAMETER + " init parameter not defined");
        }

        // Check that the UI layout class can be found
        try {
            Class uiClass = classLoader.loadClass(className);
            if (!UI.class.isAssignableFrom(uiClass)) {
                throw new ServiceException(
                        className + " does not implement UI");
            }
            // Try finding a default constructor, else throw exception
            uiClass.getConstructor();
        } catch (ClassNotFoundException e) {
            throw new ServiceException(className + " could not be loaded", e);
        } catch (SecurityException e) {
            throw new ServiceException(
                    "Could not access " + className + " class", e);
        } catch (NoSuchMethodException e) {
            throw new ServiceException(
                    className + " doesn't have a public no-args constructor");
        }
    }

    private static boolean hasPathPrefix(VaadinRequest request, String prefix) {
        String pathInfo = request.getPathInfo();

        if (pathInfo == null) {
            return false;
        }

        if (!prefix.startsWith("/")) {
            prefix = '/' + prefix;
        }

        if (!pathInfo.endsWith("/") && prefix.endsWith("/")) {
            pathInfo += '/';
        }

        if (pathInfo.startsWith(prefix)) {
            return true;
        }

        return false;
    }

    private static boolean isPathInfo(VaadinRequest request, String string) {
        String pathInfo = request.getPathInfo();

        if (pathInfo == null) {
            return false;
        }

        if (!string.startsWith("/")) {
            string = '/' + string;
        }

        if (pathInfo.equals(string)) {
            return true;
        }

        return false;
    }

    public static boolean isFileUploadRequest(VaadinRequest request) {
        return hasPathPrefix(request, UPLOAD_URL_PREFIX);
    }

    public static boolean isPublishedFileRequest(VaadinRequest request) {
        return hasPathPrefix(request,
                ApplicationConstants.PUBLISHED_FILE_PATH + "/");
    }

    public static boolean isUIDLRequest(VaadinRequest request) {
        return hasPathPrefix(request, ApplicationConstants.UIDL_PATH + '/');
    }

    public static boolean isAppRequest(VaadinRequest request) {
        return hasPathPrefix(request, ApplicationConstants.APP_PATH + '/');
    }

    public static boolean isHeartbeatRequest(VaadinRequest request) {
        return hasPathPrefix(request,
                ApplicationConstants.HEARTBEAT_PATH + '/');
    }

    public static boolean isPushRequest(VaadinRequest request) {
        return isPathInfo(request, ApplicationConstants.PUSH_PATH);
    }

    public static void initDefaultUIProvider(VaadinSession session,
            VaadinService vaadinService) throws ServiceException {
        
        final DeploymentConfiguration config =
            vaadinService.getDeploymentConfiguration();

        String uiProperty = config.getUIClassName();

        // Add provider for UI parameter first to give it lower priority
        // (providers are FILO)
        if (uiProperty != null) {
            verifyUIClass(uiProperty, vaadinService.getClassLoader());
            int priority = config.getUIProviderPriority();
            session.addUIProvider(new DefaultUIProvider(), priority);
        }

        String uiProviderProperty = config.getUIProviderClassName();
                
        // Then add custom UI provider if defined
        if (uiProviderProperty != null) {
            UIProvider uiProvider = getUIProvider(uiProviderProperty,
                    vaadinService.getClassLoader());
            int priority = config.getUIProviderPriority();
            session.addUIProvider(uiProvider, priority);
        }
    }

    private static UIProvider getUIProvider(String uiProviderProperty,
            ClassLoader classLoader) throws ServiceException {
        try {
            Class providerClass = classLoader.loadClass(uiProviderProperty);
            Class subclass = providerClass
                    .asSubclass(UIProvider.class);
            return ReflectTools.createInstance(subclass);
        } catch (ClassNotFoundException e) {
            throw new ServiceException(
                    "Could not load UIProvider class " + uiProviderProperty, e);
        } catch (ClassCastException e) {
            throw new ServiceException("UIProvider class " + uiProviderProperty
                    + " does not extend UIProvider", e);
        }
    }

    public static void checkUiProviders(VaadinSession session,
            VaadinService vaadinService) throws ServiceException {
        if (session.getUIProviders().isEmpty()) {
            throw new ServiceException(
                    "No UIProvider has been added and there is no \""
                            + VaadinSession.UI_PARAMETER
                            + "\" init parameter.");
        }
    }

    /**
     * Helper to find the most most suitable Locale. These potential sources are
     * checked in order until a Locale is found:
     * 
    *
  1. The passed component (or UI) if not null
  2. *
  3. {@link UI#getCurrent()} if defined
  4. *
  5. The passed session if not null
  6. *
  7. {@link VaadinSession#getCurrent()} if defined
  8. *
  9. The passed request if not null
  10. *
  11. {@link VaadinService#getCurrentRequest()} if defined
  12. *
  13. {@link Locale#getDefault()}
  14. *
*/ public static Locale findLocale(Component component, VaadinSession session, VaadinRequest request) { if (component == null) { component = UI.getCurrent(); } if (component != null) { Locale locale = component.getLocale(); if (locale != null) { return locale; } } if (session == null) { session = VaadinSession.getCurrent(); } if (session != null) { Locale locale = session.getLocale(); if (locale != null) { return locale; } } if (request == null) { request = VaadinService.getCurrentRequest(); } if (request != null) { Locale locale = request.getLocale(); if (locale != null) { return locale; } } return Locale.getDefault(); } private ServletPortletHelper() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy