Skip to content

Commit

Permalink
Merge pull request #1051 from amvanbaren/string-literal-constants
Browse files Browse the repository at this point in the history
Use constants for string literals
  • Loading branch information
amvanbaren authored Nov 19, 2024
2 parents 2c82308 + b8b4fd9 commit 465f6bc
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 246 deletions.
88 changes: 49 additions & 39 deletions server/src/main/java/org/eclipse/openvsx/ExtensionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class ExtensionProcessor implements AutoCloseable {
private static final String[] README = { "extension/README.md", "extension/README", "extension/README.txt" };
private static final String[] CHANGELOG = { "extension/CHANGELOG.md", "extension/CHANGELOG", "extension/CHANGELOG.txt" };

private static final String MANIFEST_METADATA = "Metadata";
private static final String MANIFEST_IDENTITY = "Identity";
private static final String MANIFEST_PROPERTIES = "Properties";
private static final String MANIFEST_PROPERTY = "Property";
private static final String MANIFEST_VALUE = "Value";

protected final Logger logger = LoggerFactory.getLogger(ExtensionProcessor.class);

private final TempFile extensionFile;
Expand Down Expand Up @@ -91,7 +97,7 @@ private void loadPackageJson() {
// Read package.json
try (var entryFile = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON)) {
if (entryFile == null) {
throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
throw new ErrorResultException(entryNotFoundMessage(PACKAGE_JSON));
}

var mapper = new ObjectMapper();
Expand All @@ -114,7 +120,7 @@ private void loadVsixManifest() {
// Read extension.vsixmanifest
try (var entryFile = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST)) {
if (entryFile == null) {
throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
throw new ErrorResultException(entryNotFoundMessage(VSIX_MANIFEST));
}

var mapper = new XmlMapper();
Expand All @@ -127,6 +133,10 @@ private void loadVsixManifest() {
}
}

private String entryNotFoundMessage(String file) {
return "Entry not found: " + file;
}

private JsonNode findByIdInArray(Iterable<JsonNode> iter, String id) {
for(JsonNode node : iter){
var idNode = node.get("Id");
Expand All @@ -139,84 +149,84 @@ private JsonNode findByIdInArray(Iterable<JsonNode> iter, String id) {

public String getExtensionName() {
loadVsixManifest();
return vsixManifest.path("Metadata").path("Identity").path("Id").asText();
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Id").asText();
}

public String getNamespace() {
loadVsixManifest();
return vsixManifest.path("Metadata").path("Identity").path("Publisher").asText();
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Publisher").asText();
}

public List<String> getExtensionDependencies() {
loadVsixManifest();
var extDepenNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionDependencies");
return asStringList(extDepenNode.path("Value").asText(), ",");
var extDepenNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionDependencies");
return asStringList(extDepenNode.path(MANIFEST_VALUE).asText(), ",");
}

public List<String> getBundledExtensions() {
loadVsixManifest();
var extPackNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionPack");
return asStringList(extPackNode.path("Value").asText(), ",");
var extPackNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionPack");
return asStringList(extPackNode.path(MANIFEST_VALUE).asText(), ",");
}

public List<String> getExtensionKinds() {
loadVsixManifest();
var extKindNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionKind");
return asStringList(extKindNode.path("Value").asText(), ",");
var extKindNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionKind");
return asStringList(extKindNode.path(MANIFEST_VALUE).asText(), ",");
}

public String getHomepage() {
loadVsixManifest();
var extKindNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Learn");
return extKindNode.path("Value").asText();
var extKindNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Learn");
return extKindNode.path(MANIFEST_VALUE).asText();
}

public String getRepository() {
loadVsixManifest();
var sourceNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Source");
return sourceNode.path("Value").asText();
var sourceNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Source");
return sourceNode.path(MANIFEST_VALUE).asText();
}

public String getBugs() {
loadVsixManifest();
var supportNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Support");
return supportNode.path("Value").asText();
var supportNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Support");
return supportNode.path(MANIFEST_VALUE).asText();
}

public String getGalleryColor() {
loadVsixManifest();
var colorNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Branding.Color");
return colorNode.path("Value").asText();
var colorNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Branding.Color");
return colorNode.path(MANIFEST_VALUE).asText();
}

public String getGalleryTheme() {
loadVsixManifest();
var themeNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Branding.Theme");
return themeNode.path("Value").asText();
var themeNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Branding.Theme");
return themeNode.path(MANIFEST_VALUE).asText();
}

public List<String> getLocalizedLanguages() {
loadVsixManifest();
var languagesNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.LocalizedLanguages");
return asStringList(languagesNode.path("Value").asText(), ",");
var languagesNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.LocalizedLanguages");
return asStringList(languagesNode.path(MANIFEST_VALUE).asText(), ",");
}

public boolean isPreview() {
loadVsixManifest();
var galleryFlags = vsixManifest.path("Metadata").path("GalleryFlags");
var galleryFlags = vsixManifest.path(MANIFEST_METADATA).path("GalleryFlags");
return asStringList(galleryFlags.asText(), " ").contains("Preview");
}

public boolean isPreRelease() {
loadVsixManifest();
var preReleaseNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.PreRelease");
return preReleaseNode.path("Value").asBoolean(false);
var preReleaseNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.PreRelease");
return preReleaseNode.path(MANIFEST_VALUE).asBoolean(false);
}

public String getSponsorLink() {
loadVsixManifest();
var sponsorLinkNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.SponsorLink");
return sponsorLinkNode.path("Value").asText();
var sponsorLinkNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.SponsorLink");
return sponsorLinkNode.path(MANIFEST_VALUE).asText();
}

public ExtensionVersion getMetadata() {
Expand All @@ -227,10 +237,10 @@ public ExtensionVersion getMetadata() {
extVersion.setTargetPlatform(getTargetPlatform());
extVersion.setPreview(isPreview());
extVersion.setPreRelease(isPreRelease());
extVersion.setDisplayName(vsixManifest.path("Metadata").path("DisplayName").asText());
extVersion.setDescription(vsixManifest.path("Metadata").path("Description").path("").asText());
extVersion.setDisplayName(vsixManifest.path(MANIFEST_METADATA).path("DisplayName").asText());
extVersion.setDescription(vsixManifest.path(MANIFEST_METADATA).path("Description").path("").asText());
extVersion.setEngines(getEngines(packageJson.path("engines")));
extVersion.setCategories(asStringList(vsixManifest.path("Metadata").path("Categories").asText(), ","));
extVersion.setCategories(asStringList(vsixManifest.path(MANIFEST_METADATA).path("Categories").asText(), ","));
extVersion.setExtensionKind(getExtensionKinds());
extVersion.setTags(getTags());
extVersion.setLicense(packageJson.path("license").textValue());
Expand All @@ -248,11 +258,11 @@ public ExtensionVersion getMetadata() {
}

public String getVersion() {
return vsixManifest.path("Metadata").path("Identity").path("Version").asText();
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Version").asText();
}

private String getTargetPlatform() {
var targetPlatform = vsixManifest.path("Metadata").path("Identity").path("TargetPlatform").asText();
var targetPlatform = vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("TargetPlatform").asText();
if (targetPlatform.isEmpty()) {
targetPlatform = TargetPlatform.NAME_UNIVERSAL;
}
Expand All @@ -261,7 +271,7 @@ private String getTargetPlatform() {
}

private List<String> getTags() {
var tags = vsixManifest.path("Metadata").path("Tags").asText();
var tags = vsixManifest.path(MANIFEST_METADATA).path("Tags").asText();
return asStringList(tags, ",").stream()
.collect(Collectors.groupingBy(String::toLowerCase))
.entrySet().stream()
Expand Down Expand Up @@ -356,7 +366,7 @@ protected TempFile getManifest(ExtensionVersion extVersion) throws IOException {
readInputStream();
var entryFile = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON);
if (entryFile == null) {
throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
throw new ErrorResultException(entryNotFoundMessage(PACKAGE_JSON));
}
var manifest = new FileResource();
manifest.setExtension(extVersion);
Expand Down Expand Up @@ -403,7 +413,7 @@ public TempFile getLicense(ExtensionVersion extVersion) throws IOException {

var entryFile = ArchiveUtil.readEntry(zipFile, assetPath);
if(entryFile == null) {
throw new ErrorResultException("Entry not found: " + assetPath);
throw new ErrorResultException(entryNotFoundMessage(assetPath));
}

var lastSegmentIndex = assetPath.lastIndexOf('/');
Expand All @@ -418,7 +428,7 @@ private TempFile readFromVsixPackage(String assetType, String[] alternateNames)
if(StringUtils.isNotEmpty(assetPath)) {
var entryFile = ArchiveUtil.readEntry(zipFile, assetPath);
if(entryFile == null) {
throw new ErrorResultException("Entry not found: " + assetPath);
throw new ErrorResultException(entryNotFoundMessage(assetPath));
}

var lastSegmentIndex = assetPath.lastIndexOf('/');
Expand Down Expand Up @@ -451,7 +461,7 @@ private TempFile readFromAlternateNames(String[] names) throws IOException {

private String tryGetLicensePath() {
loadVsixManifest();
var licensePath = vsixManifest.path("Metadata").path("License").asText();
var licensePath = vsixManifest.path(MANIFEST_METADATA).path("License").asText();
return licensePath.isEmpty()
? tryGetAssetPath(ExtensionQueryResult.ExtensionFile.FILE_LICENSE)
: licensePath;
Expand Down Expand Up @@ -486,7 +496,7 @@ protected TempFile getIcon(ExtensionVersion extVersion) throws IOException {

var entryFile = ArchiveUtil.readEntry(zipFile, iconPath);
if (entryFile == null) {
throw new ErrorResultException("Entry not found: " + iconPath);
throw new ErrorResultException(entryNotFoundMessage(iconPath));
}

var icon = new FileResource();
Expand All @@ -511,7 +521,7 @@ public TempFile getVsixManifest(ExtensionVersion extVersion) throws IOException

var entryFile = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST);
if(entryFile == null) {
throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
throw new ErrorResultException(entryNotFoundMessage(VSIX_MANIFEST));
}

entryFile.setResource(vsixManifest);
Expand Down
34 changes: 23 additions & 11 deletions server/src/main/java/org/eclipse/openvsx/ExtensionValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,27 @@ public Optional<Issue> validateNamespace(String namespace) {
return Optional.of(new Issue("Invalid namespace name: " + namespace));
}
if (namespace.length() > DEFAULT_STRING_SIZE) {
return Optional.of(new Issue("The namespace name exceeds the current limit of " + DEFAULT_STRING_SIZE + " characters."));
return Optional.of(new Issue(charactersExceededMessage("namespace name", DEFAULT_STRING_SIZE)));
}
return Optional.empty();
}

private void validateDisplayName(String displayName, int limit, List<Issue> issues) {
var field = "displayName";
checkCharacters(displayName, field, issues);
checkFieldSize(displayName, limit, field, issues);
}

private void validateDescription(String description, int limit, List<Issue> issues) {
var field = "description";
checkCharacters(description, field, issues);
checkFieldSize(description, limit, field, issues);
}

public List<Issue> validateNamespaceDetails(NamespaceDetailsJson json) {
var issues = new ArrayList<Issue>();
checkCharacters(json.getDisplayName(), "displayName", issues);
checkFieldSize(json.getDisplayName(), 32, "displayName", issues);
checkCharacters(json.getDescription(), "description", issues);
checkFieldSize(json.getDescription(), DEFAULT_STRING_SIZE, "description", issues);
validateDisplayName(json.getDisplayName(), 32, issues);
validateDescription(json.getDescription(), DEFAULT_STRING_SIZE, issues);
checkURL(json.getWebsite(), "website", issues);
checkURL(json.getSupportLink(), "supportLink", issues);

Expand Down Expand Up @@ -87,7 +97,7 @@ public Optional<Issue> validateExtensionName(String name) {
return Optional.of(new Issue("Invalid extension name: " + name));
}
if (name.length() > DEFAULT_STRING_SIZE) {
return Optional.of(new Issue("The extension name exceeds the current limit of " + DEFAULT_STRING_SIZE + " characters."));
return Optional.of(new Issue(charactersExceededMessage("extension name", DEFAULT_STRING_SIZE)));
}
return Optional.empty();
}
Expand All @@ -104,10 +114,8 @@ public List<Issue> validateMetadata(ExtensionVersion extVersion) {
var issues = new ArrayList<Issue>();
checkVersion(extVersion.getVersion(), issues);
checkTargetPlatform(extVersion.getTargetPlatform(), issues);
checkCharacters(extVersion.getDisplayName(), "displayName", issues);
checkFieldSize(extVersion.getDisplayName(), DEFAULT_STRING_SIZE, "displayName", issues);
checkCharacters(extVersion.getDescription(), "description", issues);
checkFieldSize(extVersion.getDescription(), DESCRIPTION_SIZE, "description", issues);
validateDisplayName(extVersion.getDisplayName(), DEFAULT_STRING_SIZE, issues);
validateDescription(extVersion.getDescription(), DESCRIPTION_SIZE, issues);
checkCharacters(extVersion.getCategories(), "categories", issues);
checkFieldSize(extVersion.getCategories(), DEFAULT_STRING_SIZE, "categories", issues);
checkCharacters(extVersion.getTags(), "keywords", issues);
Expand Down Expand Up @@ -181,10 +189,14 @@ private void checkCharacters(List<String> values, String field, List<Issue> issu

private void checkFieldSize(String value, int limit, String field, List<Issue> issues) {
if (value != null && value.length() > limit) {
issues.add(new Issue("The field '" + field + "' exceeds the current limit of " + limit + " characters."));
issues.add(new Issue(charactersExceededMessage("field '" + field + "'", limit)));
}
}

private String charactersExceededMessage(String name, int limit) {
return "The " + name + " exceeds the current limit of " + limit + " characters.";
}

private void checkFieldSize(List<String> values, int limit, String field, List<Issue> issues) {
if (values == null) {
return;
Expand Down
Loading

0 comments on commit 465f6bc

Please sign in to comment.