org.metaeffekt.artifact.resolver.url.UrlArtifactReference Maven / Gradle / Ivy
/*
* 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 org.metaeffekt.artifact.resolver.url;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.metaeffekt.artifact.resolver.generic.FileLocation;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
public class UrlArtifactReference {
@Getter
String url;
public UrlArtifactReference(String url) {
this.url = url;
}
public String deriveDownloadBaseDir() {
try {
final URL boxedUrl = new URL(url);
final String host = boxedUrl.getHost();
final int port = boxedUrl.getPort();
String base = host;
if (port > 0) {
base += "@" + port;
}
return base;
} catch (MalformedURLException e) {
throw new IllegalStateException("Provided URL malformed: " + url);
}
}
public String deriveResourceName() {
try {
final URL boxedUrl = new URL(url);
String path = boxedUrl.getPath();
final String query = boxedUrl.getQuery();
// cut off trailing slash
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 2);
}
if (path.startsWith("/")) {
path = path.substring(1);
}
path = path.replace("/", "_");
return path;
} catch (MalformedURLException e) {
throw new IllegalStateException("Provided URL malformed: " + url);
}
}
public String deriveDownloadFileName() {
try {
final URL boxedUrl = new URL(url);
String path = boxedUrl.getPath();
final String query = boxedUrl.getQuery();
// cut off trailing slash
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 2);
}
if (path.startsWith("/")) {
path = path.substring(1);
}
path = path.replace("/", "_");
if (StringUtils.isNotEmpty(query)) {
path = path + "@" + Base64.getEncoder().encodeToString(query.getBytes());
}
return path;
} catch (MalformedURLException e) {
throw new IllegalStateException("Provided URL malformed: " + url);
}
}
public FileLocation deriveFileLocation() {
FileLocation fileLocation = new FileLocation();
fileLocation.setEcosystem("www");
fileLocation.setFilename(deriveDownloadFileName());
fileLocation.setDispatchName(deriveResourceName());
fileLocation.setResourceQualifier(deriveDownloadBaseDir().replace("/", "_"));
return fileLocation;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy