Skip to content

Commit

Permalink
Make it easier to use with custom repositories (#687)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
  • Loading branch information
daniel-beck and daniel-beck authored Mar 8, 2024
1 parent b68f342 commit afc6e49
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@
public class ArtifactoryRepositoryImpl extends BaseMavenRepository {
private static final Logger LOGGER = Logger.getLogger(ArtifactoryRepositoryImpl.class.getName());

private static final String ARTIFACTORY_URL = "https://repo.jenkins-ci.org/";
private static final String ARTIFACTORY_API_URL = "https://repo.jenkins-ci.org/api/";
private static final String ARTIFACTORY_URL = Environment.getString("ARTIFACTORY_URL", "https://repo.jenkins-ci.org/");
private static final String ARTIFACTORY_API_URL = Environment.getString("ARTIFACTORY_API_URL", "https://repo.jenkins-ci.org/api/");
private static final String ARTIFACTORY_REPOSITORY = Environment.getString("ARTIFACTORY_REPOSITORY", "releases");

private static final String ARTIFACTORY_AQL_URL = ARTIFACTORY_API_URL + "search/aql";
private static final String ARTIFACTORY_MANIFEST_URL = ARTIFACTORY_URL + "%s/%s!/META-INF/MANIFEST.MF";
private static final String ARTIFACTORY_ZIP_ENTRY_URL = ARTIFACTORY_URL + "%s/%s!%s";
private static final String ARTIFACTORY_FILE_URL = ARTIFACTORY_URL + "%s/%s";

private static final String AQL_QUERY = "items.find({\"repo\":{\"$eq\":\"releases\"},\"$or\":[{\"name\":{\"$match\":\"*.hpi\"}},{\"name\":{\"$match\":\"*.jpi\"}},{\"name\":{\"$match\":\"*.war\"}},{\"name\":{\"$match\":\"*.pom\"}}]}).include(\"repo\", \"path\", \"name\", \"modified\", \"created\", \"sha256\", \"actual_sha1\", \"size\")";
private static final String AQL_QUERY = "items.find({\"repo\":{\"$eq\":\"" + ARTIFACTORY_REPOSITORY + "\"},\"$or\":[{\"name\":{\"$match\":\"*.hpi\"}},{\"name\":{\"$match\":\"*.jpi\"}},{\"name\":{\"$match\":\"*.war\"}},{\"name\":{\"$match\":\"*.pom\"}}]}).include(\"repo\", \"path\", \"name\", \"modified\", \"created\", \"sha256\", \"actual_sha1\", \"size\")";

private final String username;
private final String password;
Expand Down Expand Up @@ -236,7 +238,7 @@ private String getUri(ArtifactCoordinates a) {

@Override
public Manifest getManifest(MavenArtifact artifact) throws IOException {
try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, "releases", getUri(artifact.artifact)))) {
try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact)))) {
return new Manifest(is);
}
}
Expand Down Expand Up @@ -274,7 +276,7 @@ private File getFile(final String url) throws IOException {
try {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClient client = builder.build();
Request request = new Request.Builder().url(url).get().build();
Request request = new Request.Builder().addHeader("Authorization", Credentials.basic(username, password)).url(url).get().build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
try (final ResponseBody body = HttpHelper.body(response)) {
Expand Down Expand Up @@ -313,7 +315,7 @@ private File getFile(final String url) throws IOException {

@Override
public InputStream getZipFileEntry(MavenArtifact artifact, String path) throws IOException {
return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, "releases", getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/")));
return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/")));
}

@Override
Expand All @@ -324,7 +326,7 @@ public File resolve(ArtifactCoordinates artifact) throws IOException {
if (localFile.exists()) {
return localFile;
}
return getFile(String.format(ARTIFACTORY_FILE_URL, "releases", uri));
return getFile(String.format(ARTIFACTORY_FILE_URL, ARTIFACTORY_REPOSITORY, uri));
}

private static final File LOCAL_REPO = new File(new File(System.getProperty("user.home")), ".m2/repository");
Expand Down

0 comments on commit afc6e49

Please sign in to comment.