Skip to content

Commit 4ebb153

Browse files
authored
Merge pull request #52 from mtughan/JENKINS-73394
JENKINS-73394: Encode special characters
2 parents 986f23c + 6097a66 commit 4ebb153

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/main/java/io/jenkins/plugins/artifactory_artifacts/ArtifactoryClient.java

+9-17
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.Serializable;
8-
import java.net.URLEncoder;
9-
import java.nio.charset.StandardCharsets;
108
import java.nio.file.Files;
119
import java.nio.file.Path;
1210
import java.util.List;
@@ -42,7 +40,7 @@ public ArtifactoryClient(@NonNull ArtifactoryConfig config) {
4240
*/
4341
public void uploadArtifact(Path file, String targetPath) throws IOException {
4442
UploadableArtifact artifact =
45-
artifactory.repository(this.config.repository).upload(urlEncodeParts(targetPath), file.toFile());
43+
artifactory.repository(this.config.repository).upload(Utils.urlEncodeParts(targetPath), file.toFile());
4644
artifact.withSize(Files.size(file));
4745
artifact.withListener(
4846
(bytesRead, totalBytes) -> LOGGER.trace(String.format("Uploaded %d/%d", bytesRead, totalBytes)));
@@ -55,7 +53,7 @@ public void uploadArtifact(Path file, String targetPath) throws IOException {
5553
* @param targetPath the path of the artifact to delete
5654
*/
5755
public void deleteArtifact(String targetPath) {
58-
artifactory.repository(this.config.repository).delete(urlEncodeParts(targetPath));
56+
artifactory.repository(this.config.repository).delete(Utils.urlEncodeParts(targetPath));
5957
}
6058

6159
/**
@@ -64,8 +62,8 @@ public void deleteArtifact(String targetPath) {
6462
* @param targetPath the target path
6563
*/
6664
public void move(String sourcePath, String targetPath) {
67-
ItemHandle sourceItem = artifactory.repository(this.config.repository).folder(urlEncodeParts(sourcePath));
68-
sourceItem.move(this.config.repository, urlEncodeParts(targetPath));
65+
ItemHandle sourceItem = artifactory.repository(this.config.repository).folder(Utils.urlEncodeParts(sourcePath));
66+
sourceItem.move(this.config.repository, Utils.urlEncodeParts(targetPath));
6967
}
7068

7169
/**
@@ -74,7 +72,7 @@ public void move(String sourcePath, String targetPath) {
7472
* @param targetPath the target path
7573
*/
7674
public void copy(String sourcePath, String targetPath) {
77-
ItemHandle sourceItem = artifactory.repository(this.config.repository).folder(urlEncodeParts(sourcePath));
75+
ItemHandle sourceItem = artifactory.repository(this.config.repository).folder(Utils.urlEncodeParts(sourcePath));
7876
sourceItem.copy(this.config.repository, targetPath);
7977
}
8078

@@ -86,7 +84,7 @@ public void copy(String sourcePath, String targetPath) {
8684
*/
8785
public InputStream downloadArtifact(String targetPath) throws IOException {
8886
DownloadableArtifact artifact =
89-
artifactory.repository(this.config.repository).download(urlEncodeParts(targetPath));
87+
artifactory.repository(this.config.repository).download(Utils.urlEncodeParts(targetPath));
9088
return artifact.doDownload();
9189
}
9290

@@ -98,7 +96,7 @@ public InputStream downloadArtifact(String targetPath) throws IOException {
9896
*/
9997
public boolean isFolder(String targetPath) throws IOException {
10098
try {
101-
return artifactory.repository(this.config.repository).isFolder(urlEncodeParts(targetPath));
99+
return artifactory.repository(this.config.repository).isFolder(Utils.urlEncodeParts(targetPath));
102100
} catch (Exception e) {
103101
LOGGER.debug(String.format("Failed to check if %s is a folder", targetPath));
104102
return false;
@@ -140,7 +138,7 @@ public boolean isFile(String targetPath) throws IOException {
140138
try {
141139
File file = artifactory
142140
.repository(this.config.repository)
143-
.file(urlEncodeParts(targetPath))
141+
.file(Utils.urlEncodeParts(targetPath))
144142
.info();
145143
return !file.isFolder();
146144
} catch (Exception e) {
@@ -178,7 +176,7 @@ public long size(String targetPath) throws IOException {
178176
LOGGER.trace(String.format("Getting size for %s", targetPath));
179177
File file = artifactory
180178
.repository(this.config.repository)
181-
.file(urlEncodeParts(targetPath))
179+
.file(Utils.urlEncodeParts(targetPath))
182180
.info();
183181
return file.getSize();
184182
}
@@ -206,12 +204,6 @@ private Artifactory buildArtifactory() {
206204
.build();
207205
}
208206

209-
private String urlEncodeParts(String s) {
210-
return URLEncoder.encode(s, StandardCharsets.UTF_8)
211-
.replaceAll("%2F", "/")
212-
.replace("+", "%20");
213-
}
214-
215207
@Override
216208
public void close() throws Exception {
217209
artifactory.close();

src/main/java/io/jenkins/plugins/artifactory_artifacts/Utils.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
99
import hudson.security.ACL;
1010
import hudson.util.DescribableList;
11+
import java.net.URLEncoder;
12+
import java.nio.charset.StandardCharsets;
1113
import java.nio.file.Path;
1214
import java.util.Collections;
1315
import jenkins.model.ArtifactManagerConfiguration;
@@ -61,6 +63,10 @@ public static StandardUsernamePasswordCredentials getCredentials(ArtifactoryGene
6163
return getCredentials(config.getStorageCredentialId());
6264
}
6365

66+
static String urlEncodeParts(String s) {
67+
return URLEncoder.encode(s, StandardCharsets.UTF_8).replace("%2F", "/").replace("+", "%20");
68+
}
69+
6470
/**
6571
* Get the URL of the artifact
6672
* @param name the name of the artifact
@@ -69,7 +75,7 @@ public static StandardUsernamePasswordCredentials getCredentials(ArtifactoryGene
6975
public static String getUrl(String name) {
7076
return String.format(
7177
"%s/%s/%s",
72-
getArtifactConfig().getServerUrl(), getArtifactConfig().getRepository(), name);
78+
getArtifactConfig().getServerUrl(), getArtifactConfig().getRepository(), urlEncodeParts(name));
7379
}
7480

7581
/**

src/test/java/io/jenkins/plugins/artifactory_artifacts/UtilsTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public void shouldGetUrl(JenkinsRule jenkinsRule, WireMockRuntimeInfo wmRuntimeI
1919
assertThat(
2020
Utils.getUrl("artifact.txt"),
2121
is("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/my-generic-repo/artifact.txt"));
22+
23+
assertThat(
24+
Utils.getUrl("item#1.txt"),
25+
is("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/my-generic-repo/item%231.txt"));
2226
}
2327

2428
@Test

0 commit comments

Comments
 (0)