Skip to content

Commit

Permalink
refactor!: Extract app config from deployment config (#9642)
Browse files Browse the repository at this point in the history
Removes deprecated `VaadinServletConfiguration` and not needed `VaadinConfigurationException`.

Part of #9417
  • Loading branch information
Denis authored Dec 22, 2020
1 parent 8da57c1 commit e3821fd
Show file tree
Hide file tree
Showing 48 changed files with 2,189 additions and 1,471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
package com.vaadin.flow.component.dnd;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.dnd.internal.DnDUtilHelper;
import com.vaadin.flow.component.internal.DependencyList;
Expand All @@ -30,11 +36,8 @@
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.flow.shared.ui.Dependency;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public abstract class AbstractDnDUnitTest {

Expand All @@ -43,8 +46,12 @@ public abstract class AbstractDnDUnitTest {

@Before
public void setup() {
ApplicationConfiguration appConfig = Mockito
.mock(ApplicationConfiguration.class);
Mockito.when(appConfig.getPropertyNames())
.thenReturn(Collections.emptyEnumeration());
DefaultDeploymentConfiguration configuration = new DefaultDeploymentConfiguration(
VaadinServlet.class, new Properties());
appConfig, VaadinServlet.class, new Properties());

VaadinService service = Mockito.mock(VaadinService.class);
Mockito.when(service.resolveResource(Mockito.anyString()))
Expand Down
9 changes: 9 additions & 0 deletions flow-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<!-- DefaultApplicationConfigurationFactory is declared as an OSGi service -->

<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
<version>${osgi.compendium.version}</version>
<scope>provided</scope>
</dependency>


<!-- API DEPENDENCIES -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ public interface DeploymentConfiguration
*/
boolean isRequestTiming();

/**
* Returns whether cross-site request forgery protection is enabled.
*
* @return true if XSRF protection is enabled, false otherwise.
*/
boolean isXsrfProtectionEnabled();

/**
* Returns whether sync id checking is enabled. The sync id is used to
* gracefully handle situations when the client sends a message to a
Expand Down Expand Up @@ -166,26 +159,6 @@ public interface DeploymentConfiguration
<T> T getApplicationOrSystemProperty(String propertyName, T defaultValue,
Function<String, T> converter);

/**
* A shorthand of
* {@link DeploymentConfiguration#getApplicationOrSystemProperty(String, Object, Function)}
* for {@link String} type.
*
* @param propertyName
* The simple of the property, in some contexts, lookup might be
* performed using variations of the provided name.
* @param defaultValue
* the default value that should be used if no value has been
* defined
* @return the property value, or the passed default value if no property
* value is found
*/
@Override
default String getStringProperty(String propertyName, String defaultValue) {
return getApplicationOrSystemProperty(propertyName, defaultValue,
Function.identity());
}

/**
* A shorthand of
* {@link DeploymentConfiguration#getApplicationOrSystemProperty(String, Object, Function)}
Expand Down Expand Up @@ -296,18 +269,6 @@ default List<String> getPolyfills() {
.collect(Collectors.toList());
}

/**
* Get if the dev server should be reused on each reload. True by default,
* set it to false in tests so as dev server is not kept as a daemon after
* the test.
*
* @return true if dev server should be reused
*/
default boolean reuseDevServer() {
return getBooleanProperty(
InitParameters.SERVLET_PARAMETER_REUSE_DEV_SERVER, true);
}

/**
* Get if the stats.json file should be retrieved from an external service
* or through the classpath.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.Serializable;

import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION;
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP;

/**
Expand Down Expand Up @@ -45,6 +46,18 @@ default boolean enableDevServer() {
InitParameters.SERVLET_PARAMETER_ENABLE_DEV_SERVER, true);
}

/**
* Get if the dev server should be reused on each reload. True by default,
* set it to false in tests so as dev server is not kept as a daemon after
* the test.
*
* @return true if dev server should be reused
*/
default boolean reuseDevServer() {
return getBooleanProperty(
InitParameters.SERVLET_PARAMETER_REUSE_DEV_SERVER, true);
}

/**
* Returns whether Vaadin is running in useDeprecatedV14Bootstrapping.
*
Expand Down Expand Up @@ -94,4 +107,14 @@ default boolean isPnpmEnabled() {
Boolean.valueOf(Constants.ENABLE_PNPM_DEFAULT_STRING));
}

/**
* Returns whether cross-site request forgery protection is enabled.
*
* @return true if XSRF protection is enabled, false otherwise.
*/
default boolean isXsrfProtectionEnabled() {
return !getBooleanProperty(SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION,
false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.vaadin.flow.server;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -82,17 +83,7 @@ public boolean getBooleanProperty(String name, boolean defaultValue) {
* @return String value or null if not found
*/
public String getApplicationProperty(String parameterName) {

String val = properties.get(parameterName);
if (val != null) {
return val;
}

// Try lower case application properties for backward compatibility
// with 3.0.2 and earlier
val = properties.get(parameterName.toLowerCase());

return val;
return getApplicationProperty(getProperties()::get, parameterName);
}

/**
Expand Down Expand Up @@ -150,4 +141,27 @@ protected String getSystemProperty(String parameterName) {
return System.getProperty(VAADIN_PREFIX + parameterName);
}

/**
* Gets application property value using the {@code valueProvider}.
*
* @param valueProvider
* a value provider for the property
* @param propertyName
* the name or the parameter.
* @return String value or null if not found
*/
protected String getApplicationProperty(
Function<String, String> valueProvider, String propertyName) {
String val = valueProvider.apply(propertyName);
if (val != null) {
return val;
}

// Try lower case application properties for backward compatibility with
// 3.0.2 and earlier
val = valueProvider.apply(propertyName.toLowerCase(Locale.ENGLISH));

return val;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.slf4j.LoggerFactory;

import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.flow.shared.communication.PushMode;

import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR;
Expand Down Expand Up @@ -56,7 +57,8 @@ public class DefaultDeploymentConfiguration
+ "Client-side views written in TypeScript are not supported. Vaadin 15+ enables client-side and server-side views.\n"
+ "See https://vaadin.com/docs/v15/flow/typescript/starting-the-app.html for more information.";

// not a warning anymore, but keeping variable name to avoid breaking anything
// not a warning anymore, but keeping variable name to avoid breaking
// anything
public static final String WARNING_V15_BOOTSTRAP = "Using Vaadin 15+ bootstrap mode.%n %s%n %s";

private static final String DEPLOYMENT_WARNINGS = "Following issues were discovered with deployment configuration:";
Expand Down Expand Up @@ -120,17 +122,19 @@ public class DefaultDeploymentConfiguration

/**
* Create a new deployment configuration instance.
*
*
* @param parentConfig
* a parent application configuration
* @param systemPropertyBaseClass
* the class that should be used as a basis when reading system
* properties
* @param initParameters
* the init parameters that should make up the foundation for
* this configuration
*/
public DefaultDeploymentConfiguration(Class<?> systemPropertyBaseClass,
Properties initParameters) {
super(systemPropertyBaseClass, initParameters);
public DefaultDeploymentConfiguration(ApplicationConfiguration parentConfig,
Class<?> systemPropertyBaseClass, Properties initParameters) {
super(parentConfig, systemPropertyBaseClass, initParameters);

boolean log = logging.getAndSet(false);

Expand Down Expand Up @@ -287,8 +291,12 @@ public String getPushURL() {
* Log a warning if Vaadin is not running in production mode.
*/
private void checkProductionMode(boolean log) {
productionMode = getBooleanProperty(
InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE, false);
if (isOwnProperty(InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE)) {
productionMode = getBooleanProperty(
InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE, false);
} else {
productionMode = getParentConfiguration().isProductionMode();
}
if (log) {
if (productionMode) {
info.add("Vaadin is running in production mode.");
Expand All @@ -306,8 +314,13 @@ private void checkProductionMode(boolean log) {
* Log a message about the bootstrapping being used.
*/
private void checkV14Bootsrapping(boolean log) {
useDeprecatedV14Bootstrapping = getBooleanProperty(
InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP, false);
if (isOwnProperty(InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP)) {
useDeprecatedV14Bootstrapping = getBooleanProperty(
InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP, false);
} else {
useDeprecatedV14Bootstrapping = getParentConfiguration()
.useV14Bootstrap();
}
if (log) {
if (useDeprecatedV14Bootstrapping) {
warnings.add(WARNING_V14_BOOTSTRAP);
Expand Down Expand Up @@ -362,15 +375,23 @@ private String getIndexHTMLMessage(String frontendDir) {
*/
private void checkRequestTiming() {
requestTiming = getBooleanProperty(
InitParameters.SERVLET_PARAMETER_REQUEST_TIMING, !productionMode);
InitParameters.SERVLET_PARAMETER_REQUEST_TIMING,
!productionMode);
}

/**
* Log a warning if cross-site request forgery protection is disabled.
*/
private void checkXsrfProtection(boolean loggWarning) {
xsrfProtectionEnabled = !getBooleanProperty(
InitParameters.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, false);
if (isOwnProperty(
InitParameters.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION)) {
xsrfProtectionEnabled = !getBooleanProperty(
InitParameters.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION,
false);
} else {
xsrfProtectionEnabled = getParentConfiguration()
.isXsrfProtectionEnabled();
}
if (!xsrfProtectionEnabled && loggWarning) {
warnings.add(WARNING_XSRF_PROTECTION_DISABLED);
}
Expand Down Expand Up @@ -422,17 +443,18 @@ private void checkCloseIdleSessions() {
private void checkPushMode() {
try {
pushMode = getApplicationOrSystemProperty(
InitParameters.SERVLET_PARAMETER_PUSH_MODE, PushMode.DISABLED,
stringMode -> Enum.valueOf(PushMode.class,
stringMode.toUpperCase()));
InitParameters.SERVLET_PARAMETER_PUSH_MODE,
PushMode.DISABLED, stringMode -> Enum
.valueOf(PushMode.class, stringMode.toUpperCase()));
} catch (IllegalArgumentException e) {
warnings.add(WARNING_PUSH_MODE_NOT_RECOGNIZED);
pushMode = PushMode.DISABLED;
}
}

private void checkPushURL() {
pushURL = getStringProperty(InitParameters.SERVLET_PARAMETER_PUSH_URL, "");
pushURL = getStringProperty(InitParameters.SERVLET_PARAMETER_PUSH_URL,
"");
}

private void checkSyncIdCheck() {
Expand All @@ -446,4 +468,5 @@ private void checkSendUrlsAsParameters() {
InitParameters.SERVLET_PARAMETER_SEND_URLS_AS_PARAMETERS,
DEFAULT_SEND_URLS_AS_PARAMETERS);
}

}
Loading

0 comments on commit e3821fd

Please sign in to comment.