Skip to content

Commit

Permalink
Move ProjectRequest input validation to ProjectDescriptionCustomizer
Browse files Browse the repository at this point in the history
This commit simplifies DefaultProjectRequestToDescriptionConverter to
converting the request to a description only. Applying default or
cleaning values are now part of a ProjectDescriptionCustomizer callback.

Closes gh-1045
  • Loading branch information
snicoll committed Jan 2, 2020
1 parent d0cf3a4 commit e25fb74
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.spring.initializr.web.project;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.spring.initializr.generator.buildsystem.BuildSystem;
Expand All @@ -32,8 +31,6 @@
import io.spring.initializr.metadata.Type;
import io.spring.initializr.metadata.support.MetadataBuildItemMapper;

import org.springframework.util.StringUtils;

/**
* A default {@link ProjectRequestToDescriptionConverter} implementation that uses the
* {@link InitializrMetadata metadata} to set default values for missing attributes if
Expand All @@ -47,8 +44,6 @@ public class DefaultProjectRequestToDescriptionConverter

private static final Version VERSION_1_5_0 = Version.parse("1.5.0.RELEASE");

private static final char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' };

@Override
public ProjectDescription convert(ProjectRequest request, InitializrMetadata metadata) {
MutableProjectDescription description = new MutableProjectDescription();
Expand All @@ -70,87 +65,22 @@ public void convert(ProjectRequest request, MutableProjectDescription descriptio
List<Dependency> resolvedDependencies = getResolvedDependencies(request, springBootVersion, metadata);
validateDependencyRange(springBootVersion, resolvedDependencies);

description.setApplicationName(getApplicationName(request, metadata));
description.setArtifactId(getArtifactId(request, metadata));
description.setBaseDirectory(getBaseDirectory(request.getBaseDir(), request.getArtifactId()));
description.setApplicationName(request.getApplicationName());
description.setArtifactId(request.getArtifactId());
description.setBaseDirectory(request.getBaseDir());
description.setBuildSystem(getBuildSystem(request, metadata));
description
.setDescription(determineValue(request.getDescription(), () -> metadata.getDescription().getContent()));
description.setGroupId(getGroupId(request, metadata));
description.setDescription(request.getDescription());
description.setGroupId(request.getGroupId());
description.setLanguage(Language.forId(request.getLanguage(), request.getJavaVersion()));
description.setName(getName(request, metadata));
description.setPackageName(getPackageName(request, metadata));
description.setName(request.getName());
description.setPackageName(request.getPackageName());
description.setPackaging(Packaging.forId(request.getPackaging()));
description.setPlatformVersion(Version.parse(springBootVersion));
description.setVersion(determineValue(request.getVersion(), () -> metadata.getVersion().getContent()));
description.setVersion(request.getVersion());
resolvedDependencies.forEach((dependency) -> description.addDependency(dependency.getId(),
MetadataBuildItemMapper.toDependency(dependency)));
}

private String determineValue(String candidate, Supplier<String> fallback) {
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
}

private String getBaseDirectory(String baseDir, String artifactId) {
if (baseDir != null && baseDir.equals(artifactId)) {
return cleanMavenCoordinate(baseDir, "-");
}
return baseDir;
}

private String getName(ProjectRequest request, InitializrMetadata metadata) {
String name = request.getName();
if (!StringUtils.hasText(name)) {
return metadata.getName().getContent();
}
if (name.equals(request.getArtifactId())) {
return cleanMavenCoordinate(name, "-");
}
return name;
}

private String getGroupId(ProjectRequest request, InitializrMetadata metadata) {
if (!StringUtils.hasText(request.getGroupId())) {
return metadata.getGroupId().getContent();
}
return cleanMavenCoordinate(request.getGroupId(), ".");
}

private String getArtifactId(ProjectRequest request, InitializrMetadata metadata) {
if (!StringUtils.hasText(request.getArtifactId())) {
return metadata.getArtifactId().getContent();
}
return cleanMavenCoordinate(request.getArtifactId(), "-");
}

private String cleanMavenCoordinate(String coordinate, String delimiter) {
String[] elements = coordinate.split("[^\\w\\-.]+");
if (elements.length == 1) {
return coordinate;
}
StringBuilder builder = new StringBuilder();
for (String element : elements) {
if (shouldAppendDelimiter(element, builder)) {
builder.append(delimiter);
}
builder.append(element);
}
return builder.toString();
}

private boolean shouldAppendDelimiter(String element, StringBuilder builder) {
if (builder.length() == 0) {
return false;
}
for (char c : VALID_MAVEN_SPECIAL_CHARACTERS) {
int prevIndex = builder.length() - 1;
if (element.charAt(0) == c || builder.charAt(prevIndex) == c) {
return false;
}
}
return true;
}

private void validate(ProjectRequest request, InitializrMetadata metadata) {
validateSpringBootVersion(request);
validateType(request.getType(), metadata);
Expand Down Expand Up @@ -223,18 +153,6 @@ private BuildSystem getBuildSystem(ProjectRequest request, InitializrMetadata me
return BuildSystem.forId(typeFromMetadata.getTags().get("build"));
}

private String getPackageName(ProjectRequest request, InitializrMetadata metadata) {
return metadata.getConfiguration().cleanPackageName(request.getPackageName(),
metadata.getPackageName().getContent());
}

private String getApplicationName(ProjectRequest request, InitializrMetadata metadata) {
if (!StringUtils.hasText(request.getApplicationName())) {
return metadata.getConfiguration().generateApplicationName(request.getName());
}
return request.getApplicationName();
}

private String getSpringBootVersion(ProjectRequest request, InitializrMetadata metadata) {
return (request.getBootVersion() != null) ? request.getBootVersion()
: metadata.getBootVersions().getDefault().getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.spring.initializr.web.project;

import java.util.function.Supplier;

import io.spring.initializr.generator.project.MutableProjectDescription;
import io.spring.initializr.generator.project.ProjectDescriptionCustomizer;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.metadata.InitializrMetadata;

import org.springframework.util.StringUtils;

/**
* A {@link ProjectDescriptionCustomizer} that uses the {@link InitializrMetadata
* metadata} to set default values for missing attributes if necessary.
*
* @author Stephane Nicoll
*/
public class MetadataProjectDescriptionCustomizer implements ProjectDescriptionCustomizer {

private static final char[] VALID_MAVEN_SPECIAL_CHARACTERS = new char[] { '_', '-', '.' };

private final InitializrMetadata metadata;

public MetadataProjectDescriptionCustomizer(InitializrMetadata metadata) {
this.metadata = metadata;
}

@Override
public void customize(MutableProjectDescription description) {
if (!StringUtils.hasText(description.getApplicationName())) {
description.setApplicationName(
this.metadata.getConfiguration().generateApplicationName(description.getName()));
}
String targetArtifactId = determineValue(description.getArtifactId(),
() -> this.metadata.getArtifactId().getContent());
description.setArtifactId(cleanMavenCoordinate(targetArtifactId, "-"));
if (targetArtifactId.equals(description.getBaseDirectory())) {
description.setBaseDirectory(cleanMavenCoordinate(targetArtifactId, "-"));
}
if (!StringUtils.hasText(description.getDescription())) {
description.setDescription(this.metadata.getDescription().getContent());
}
String targetGroupId = determineValue(description.getGroupId(), () -> this.metadata.getGroupId().getContent());
description.setGroupId(cleanMavenCoordinate(targetGroupId, "."));
if (!StringUtils.hasText(description.getName())) {
description.setName(this.metadata.getName().getContent());
}
else if (targetArtifactId.equals(description.getName())) {
description.setName(cleanMavenCoordinate(targetArtifactId, "-"));
}
description.setPackageName(this.metadata.getConfiguration().cleanPackageName(description.getPackageName(),
this.metadata.getPackageName().getContent()));
if (description.getPlatformVersion() == null) {
description.setPlatformVersion(Version.parse(this.metadata.getBootVersions().getDefault().getId()));
}
if (!StringUtils.hasText(description.getVersion())) {
description.setVersion(this.metadata.getVersion().getContent());
}
}

private String cleanMavenCoordinate(String coordinate, String delimiter) {
String[] elements = coordinate.split("[^\\w\\-.]+");
if (elements.length == 1) {
return coordinate;
}
StringBuilder builder = new StringBuilder();
for (String element : elements) {
if (shouldAppendDelimiter(element, builder)) {
builder.append(delimiter);
}
builder.append(element);
}
return builder.toString();
}

private boolean shouldAppendDelimiter(String element, StringBuilder builder) {
if (builder.length() == 0) {
return false;
}
for (char c : VALID_MAVEN_SPECIAL_CHARACTERS) {
int prevIndex = builder.length() - 1;
if (element.charAt(0) == c || builder.charAt(prevIndex) == c) {
return false;
}
}
return true;
}

private String determineValue(String candidate, Supplier<String> fallback) {
return (StringUtils.hasText(candidate)) ? candidate : fallback.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private void customizeProjectGenerationContext(AnnotationConfigApplicationContex
context.registerBean(InitializrMetadata.class, () -> metadata);
context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
context.getBean(ProjectDescription.class).getPlatformVersion()));
context.registerBean(MetadataProjectDescriptionCustomizer.class,
() -> new MetadataProjectDescriptionCustomizer(metadata));
}

private void publishProjectGeneratedEvent(R request, ProjectGenerationContext context) {
Expand Down
Loading

0 comments on commit e25fb74

Please sign in to comment.