Skip to content
Snippets Groups Projects
Commit 0fbaf435 authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

Iss #13 - Add current modern release return for legacy release endpoint

Resolves #13
parent 8af7648b
No related branches found
No related tags found
1 merge request!23Iss #13 - Add current modern release return for legacy release endpoint
......@@ -15,6 +15,7 @@ package org.eclipsefoundation.downloads.resources;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.inject.Inject;
......@@ -25,6 +26,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
......@@ -55,6 +57,10 @@ import org.slf4j.LoggerFactory;
public class DownloadsResource {
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadsResource.class);
private static final Pattern MODERN_RELEASE_PATTERN = Pattern.compile("^\\d");
private static final String RELEASE_TYPE_ERROR_MESSAGE_FORMAT = "Invalid release type: %s";
@Inject
DefaultHibernateDao dao;
@Inject
......@@ -108,7 +114,7 @@ public class DownloadsResource {
@QueryParam(DownloadsUrlParameterNames.RELEASE_VERSION_VALUE) String releaseVersion) {
if (!isValidType(releaseType)) {
throw new BadRequestException(String.format("Invalid release type: %s", releaseType));
throw new BadRequestException(String.format(RELEASE_TYPE_ERROR_MESSAGE_FORMAT, releaseType));
}
// use existing calls and route according to what query params are provided
......@@ -116,9 +122,25 @@ public class DownloadsResource {
return releaseVersion(releaseType, releaseName, releaseVersion);
} else if (StringUtils.isNotBlank(releaseName)) {
return release(releaseType, releaseName);
} else if (StringUtils.isNotBlank(releaseVersion)) {
throw new BadRequestException("Cannot retrieve a release solely by version name");
} else {
// get the most recent modern release by reverse natural sorting on numeric release names
return Response
.ok(releaseTrackerService
.getActiveReleases()
.stream()
.filter(release -> release
.getVersions()
.stream()
.anyMatch(r -> r.getIsCurrent() && "release".equalsIgnoreCase(r.getType()))
&& MODERN_RELEASE_PATTERN.matcher(release.getName()).find())
.sorted((r1, r2) -> r2.getName().compareTo(r1.getName()))
.findFirst()
.orElseThrow(
() -> new ServerErrorException("Could not find any active modern releases", 500)))
.build();
}
throw new BadRequestException("At least the release_name query parameter is required to use this endpoint");
}
@GET
......@@ -133,7 +155,7 @@ public class DownloadsResource {
@PathParam("releaseName") String releaseName) {
if (!isValidType(releaseType)) {
throw new BadRequestException(String.format("Invalid release type: %s", releaseType));
throw new BadRequestException(String.format(RELEASE_TYPE_ERROR_MESSAGE_FORMAT, releaseType));
}
// check that the release name is valid
......@@ -172,7 +194,7 @@ public class DownloadsResource {
public Response releaseVersion(@PathParam("releaseType") String releaseType,
@PathParam("releaseName") String releaseName, @PathParam("releaseVersion") String releaseVersion) {
if (!isValidType(releaseType)) {
throw new BadRequestException(String.format("Invalid release type: %s", releaseType));
throw new BadRequestException(String.format(RELEASE_TYPE_ERROR_MESSAGE_FORMAT, releaseType));
}
// check that the release name is valid
......@@ -212,7 +234,7 @@ public class DownloadsResource {
* @return True if valid, false if not
*/
private boolean isValidType(String type) {
return (!type.equals("epp") && !type.equals("eclipse_packages")) ? false : true;
return "epp".equalsIgnoreCase(type) || "eclipse_packages".equalsIgnoreCase(type);
}
/**
......
......@@ -67,8 +67,8 @@ class DownloadsResourceTest {
LEGACY_RELEASE_NAME_VERSION_URL, new String[] { "new", "2021-12", "r" },
SchemaNamespaceHelper.ERROR_SCHEMA_PATH);
public static final EndpointTestCase LEGACY_RELEASE_NO_NAME = TestCaseHelper.buildBadRequestCase(
LEGACY_RELEASE_URL, new String[] { "eclipse_packages" }, SchemaNamespaceHelper.ERROR_SCHEMA_PATH);
public static final EndpointTestCase LEGACY_RELEASE_NO_NAME = TestCaseHelper.buildSuccessCase(
LEGACY_RELEASE_URL, new String[] { "eclipse_packages" }, SchemaNamespaceHelper.RELEASE_SCHEMA_PATH);
public static final EndpointTestCase LEGACY_RELEASE_NO_NAME_VERSION = TestCaseHelper.buildBadRequestCase(
LEGACY_RELEASE_VERSION_URL, new String[] { "eclipse_packages", "r" },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment