Skip to content

Commit afc6e49

Browse files
authored
Make it easier to use with custom repositories (#687)
Co-authored-by: Daniel Beck <[email protected]>
1 parent b68f342 commit afc6e49

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/java/io/jenkins/update_center/ArtifactoryRepositoryImpl.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@
4646
public class ArtifactoryRepositoryImpl extends BaseMavenRepository {
4747
private static final Logger LOGGER = Logger.getLogger(ArtifactoryRepositoryImpl.class.getName());
4848

49-
private static final String ARTIFACTORY_URL = "https://repo.jenkins-ci.org/";
50-
private static final String ARTIFACTORY_API_URL = "https://repo.jenkins-ci.org/api/";
49+
private static final String ARTIFACTORY_URL = Environment.getString("ARTIFACTORY_URL", "https://repo.jenkins-ci.org/");
50+
private static final String ARTIFACTORY_API_URL = Environment.getString("ARTIFACTORY_API_URL", "https://repo.jenkins-ci.org/api/");
51+
private static final String ARTIFACTORY_REPOSITORY = Environment.getString("ARTIFACTORY_REPOSITORY", "releases");
52+
5153
private static final String ARTIFACTORY_AQL_URL = ARTIFACTORY_API_URL + "search/aql";
5254
private static final String ARTIFACTORY_MANIFEST_URL = ARTIFACTORY_URL + "%s/%s!/META-INF/MANIFEST.MF";
5355
private static final String ARTIFACTORY_ZIP_ENTRY_URL = ARTIFACTORY_URL + "%s/%s!%s";
5456
private static final String ARTIFACTORY_FILE_URL = ARTIFACTORY_URL + "%s/%s";
5557

56-
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\")";
58+
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\")";
5759

5860
private final String username;
5961
private final String password;
@@ -236,7 +238,7 @@ private String getUri(ArtifactCoordinates a) {
236238

237239
@Override
238240
public Manifest getManifest(MavenArtifact artifact) throws IOException {
239-
try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, "releases", getUri(artifact.artifact)))) {
241+
try (InputStream is = getFileContent(String.format(ARTIFACTORY_MANIFEST_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact)))) {
240242
return new Manifest(is);
241243
}
242244
}
@@ -274,7 +276,7 @@ private File getFile(final String url) throws IOException {
274276
try {
275277
OkHttpClient.Builder builder = new OkHttpClient.Builder();
276278
OkHttpClient client = builder.build();
277-
Request request = new Request.Builder().url(url).get().build();
279+
Request request = new Request.Builder().addHeader("Authorization", Credentials.basic(username, password)).url(url).get().build();
278280
final Response response = client.newCall(request).execute();
279281
if (response.isSuccessful()) {
280282
try (final ResponseBody body = HttpHelper.body(response)) {
@@ -313,7 +315,7 @@ private File getFile(final String url) throws IOException {
313315

314316
@Override
315317
public InputStream getZipFileEntry(MavenArtifact artifact, String path) throws IOException {
316-
return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, "releases", getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/")));
318+
return getFileContent(String.format(ARTIFACTORY_ZIP_ENTRY_URL, ARTIFACTORY_REPOSITORY, getUri(artifact.artifact), StringUtils.prependIfMissing(path, "/")));
317319
}
318320

319321
@Override
@@ -324,7 +326,7 @@ public File resolve(ArtifactCoordinates artifact) throws IOException {
324326
if (localFile.exists()) {
325327
return localFile;
326328
}
327-
return getFile(String.format(ARTIFACTORY_FILE_URL, "releases", uri));
329+
return getFile(String.format(ARTIFACTORY_FILE_URL, ARTIFACTORY_REPOSITORY, uri));
328330
}
329331

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

0 commit comments

Comments
 (0)