com.metaeffekt.mirror.download.other.EolDownload Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 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
*
* http://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 com.metaeffekt.mirror.download.other;
import com.metaeffekt.artifact.analysis.utils.FileUtils;
import com.metaeffekt.mirror.download.documentation.MirrorMetadata;
import com.metaeffekt.mirror.Retry;
import com.metaeffekt.mirror.download.Download;
import com.metaeffekt.mirror.download.ResourceLocation;
import org.json.JSONArray;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
/**
* References:
*
* - EOL API: https://endoflife.date/docs/api
*
* This download manages the End of Life (EOL) data for various products using their API.
* The API provides a list of product identifiers, and for each product, a detailed version history including end-of-life dates.
* The downloader fetches this list of all products and retrieves their individual version information for each product.
* Each product's version data is stored locally in JSON files.
* .
* └── eol
* ├── java.json
* ├── nodejs.json
* ├── python.json
* └── ...
*
*/
@MirrorMetadata(directoryName = "eol", mavenPropertyName = "eolDownload")
public class EolDownload extends Download {
public EolDownload(File baseMirrorDirectory) {
super(baseMirrorDirectory, EolDownload.class);
}
@Override
protected void performDownload() {
final URL allProductsUrl = getRemoteResourceLocationUrl(ResourceLocationEOL.EOL_ALL_PRODUCTS);
final JSONArray allProducts = new JSONArray(String.join("", super.downloader.fetchResponseBodyFromUrlAsList(allProductsUrl)));
for (int i = 0; i < allProducts.length(); i++) {
final String product = allProducts.getString(i);
final URL productUrl = getRemoteResourceLocationUrl(ResourceLocationEOL.EOL_PRODUCT_VERSIONS, product);
final JSONArray productVersions = new Retry<>(() -> new JSONArray(String.join("", super.downloader.fetchResponseBodyFromUrlAsList(productUrl))))
.retryCount(3)
.withDelay(1000)
.onFailure(e -> {
throw new RuntimeException("Failed to fetch EOL product versions.", e);
})
.run();
try {
FileUtils.write(new File(super.downloadIntoDirectory, product + ".json"), productVersions.toString(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Failed to write EOL product info to directory.", e);
}
}
}
@Override
protected boolean additionalIsDownloadRequired() {
return false;
}
@Override
public void setRemoteResourceLocation(String location, String url) {
super.setRemoteResourceLocation(ResourceLocationEOL.valueOf(location), url);
}
/**
* The ResourceLocationEOL enum defines the URLs for accessing the End of Life (EOL) API.
* https://endoflife.date/docs/api
*/
public enum ResourceLocationEOL implements ResourceLocation {
/**
* URL for the EOL API that is used to fetch the version information for a single product.
*
* %s
the product identifier to fetch the version information for (example: java
)
*
*/
EOL_PRODUCT_VERSIONS("https://endoflife.date/api/%s.json"),
/**
* URL for the EOL API that is used to fetch all product identifiers.
* Returns an array of strings.
*/
EOL_ALL_PRODUCTS("https://endoflife.date/api/all.json");
private final String defaultValue;
ResourceLocationEOL(String defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public String getDefault() {
return this.defaultValue;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy