Skip to content

Commit ba238fe

Browse files
authored
Add an option to perform download with custom headers (#373)
1 parent b29e73c commit ba238fe

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ InputStream iStream = artifactory.repository("RepoName")
224224
.doDownload();
225225
```
226226

227+
##### Downloading Artifact with custom headers
228+
229+
```groovy
230+
Map<String, String> headers = new HashMap<>();
231+
headers.put("Range", "bytes=0-10");
232+
InputStream iStream = artifactory.repository("RepoName")
233+
.download("path/to/fileToDownload.txt")
234+
.doDownloadWithHeaders(headers);
235+
```
236+
227237
#### File, Folder and Repository Info
228238

229239
##### File Info

api/src/main/java/org/jfrog/artifactory/client/Artifactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ public interface Artifactory extends ApiInterface, AutoCloseable {
4444

4545
InputStream getInputStream(String path) throws IOException;
4646

47+
InputStream getInputStreamWithHeaders(String path, Map<String, String> headers) throws IOException;
48+
4749
default public <T> T get(String path, Class<? extends T> object, Class<T> interfaceObject) throws IOException {
4850
return null;
4951
}
5052

53+
default public <T> T get(String path, Class<? extends T> object, Class<T> interfaceObject, Map<String, String> headers) throws IOException {
54+
return null;
55+
}
56+
5157
default public <T> T post(String path, org.apache.http.entity.ContentType contentType, String content,
5258
Map<String, String> headers, Class<? extends T> object, Class<T> interfaceObject) throws IOException {
5359
return null;

api/src/main/java/org/jfrog/artifactory/client/DownloadableArtifact.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.util.Map;
78

89
/**
910
* @author jbaruch
@@ -14,6 +15,7 @@ public interface DownloadableArtifact extends Artifact<DownloadableArtifact> {
1415

1516
InputStream doDownload() throws IOException;
1617

18+
InputStream doDownloadWithHeaders(Map<String, String> headers) throws IOException;
1719
DownloadableArtifact withProperty(String name, Object... values);
1820

1921
DownloadableArtifact withProperty(String name, Object value);

services/src/main/groovy/org/jfrog/artifactory/client/impl/ArtifactoryImpl.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.MalformedURLException;
2222
import java.net.URI;
2323
import java.net.URL;
24+
import java.util.HashMap;
2425
import java.util.Map;
2526

2627
/**
@@ -220,6 +221,15 @@ public InputStream getInputStream(String path) throws IOException {
220221
throw newHttpResponseException(httpResponse);
221222
}
222223

224+
public InputStream getInputStreamWithHeaders(String path, Map<String, String> headers) throws IOException {
225+
HttpResponse httpResponse = get(path, null, null, headers);
226+
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK ||
227+
httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_PARTIAL_CONTENT) {
228+
return httpResponse.getEntity().getContent();
229+
}
230+
throw newHttpResponseException(httpResponse);
231+
}
232+
223233
private HttpResponseException newHttpResponseException(HttpResponse httpResponse) throws IOException {
224234
String artifactoryResponse = Util.responseToString(httpResponse);
225235
StatusLine statusLine = httpResponse.getStatusLine();
@@ -239,12 +249,23 @@ protected Boolean head(String path) throws IOException {
239249
}
240250

241251
public <T> T get(String path, Class<? extends T> object, Class<T> interfaceObject) throws IOException {
242-
HttpGet httpGet = new HttpGet();
252+
return this.get(path, object, interfaceObject, new HashMap<>());
253+
}
243254

255+
public <T> T get(String path, Class<? extends T> object, Class<T> interfaceObject, Map<String, String> headers) throws IOException {
256+
HttpGet httpGet = new HttpGet();
244257
httpGet.setURI(URI.create(url + path));
258+
259+
if (headers != null && !headers.isEmpty()) {
260+
for (String key : headers.keySet()) {
261+
httpGet.setHeader(key, headers.get(key));
262+
}
263+
}
264+
245265
HttpResponse httpResponse = execute(httpGet);
246266
int status = httpResponse.getStatusLine().getStatusCode();
247-
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
267+
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NO_CONTENT &&
268+
status != HttpStatus.SC_ACCEPTED && status != HttpStatus.SC_PARTIAL_CONTENT) {
248269
throw newHttpResponseException(httpResponse);
249270
}
250271

services/src/main/java/org/jfrog/artifactory/client/impl/DownloadableArtifactImpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.util.HashMap;
8+
import java.util.Map;
89

910
/**
1011
* Created by eyalb on 21/06/2018.
@@ -22,12 +23,21 @@ public class DownloadableArtifactImpl extends ArtifactBase<DownloadableArtifact>
2223
}
2324

2425
public InputStream doDownload() throws IOException {
26+
String uri = generateUriWithParams();
27+
return artifactory.getInputStream(uri);
28+
}
29+
30+
public InputStream doDownloadWithHeaders(Map<String, String> headers) throws IOException {
31+
String uri = generateUriWithParams();
32+
return artifactory.getInputStreamWithHeaders(uri, headers);
33+
}
34+
35+
private String generateUriWithParams() {
2536
String params = parseParams(props, "=") + parseParams(mandatoryProps, "+=");
2637
if (params.length() > 0) {
2738
params = ";" + params;
2839
}
29-
String uri = String.format("/%s/%s%s", repo, path, params);
30-
return artifactory.getInputStream(uri);
40+
return String.format("/%s/%s%s", repo, path, params);
3141
}
3242

3343
public DownloadableArtifact withProperty(String name, Object... values) {

services/src/test/java/org/jfrog/artifactory/client/DownloadUploadTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.text.DecimalFormat;
2020
import java.text.NumberFormat;
2121
import java.util.Collections;
22+
import java.util.HashMap;
2223
import java.util.List;
24+
import java.util.Map;
2325

2426
import static org.testng.Assert.*;
2527

@@ -284,4 +286,13 @@ public void testDownloadWithMandatoryAndNonMandatoryProperties() throws IOExcept
284286
.withProperty("foo", "bar").withMandatoryProperty("colors", "red").doDownload();
285287
assertEquals(textFrom(inputStream), textFrom(this.getClass().getResourceAsStream("/sample.txt")));
286288
}
289+
290+
@Test(dependsOnMethods = "testUploadWithSingleProperty")
291+
public void testDownloadWithHeaders() throws IOException {
292+
Map<String, String> headers = new HashMap<>();
293+
headers.put("Range", "bytes=0-10");
294+
InputStream inputStream = artifactory.repository(localRepository.getKey()).download(PATH).doDownloadWithHeaders(headers);
295+
String actual = textFrom(inputStream);
296+
assertEquals(actual, textFrom(this.getClass().getResourceAsStream("/sample.txt")).substring(0, 11));
297+
}
287298
}

0 commit comments

Comments
 (0)