-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Streaming Rest Call - returning the response input stream without…
… the wrappers
- Loading branch information
Showing
9 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 2 additions & 10 deletions
12
api/src/main/java/org/jfrog/artifactory/client/ArtifactoryResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,15 @@ | ||
package org.jfrog.artifactory.client; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.StatusLine; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* ArtifactoryResponse object returned from {@link Artifactory#restCall(ArtifactoryRequest)}. | ||
* acts as a wrapper for {@link org.apache.http.HttpResponse} but removes the need to handle response stream. | ||
*/ | ||
public interface ArtifactoryResponse { | ||
|
||
Header[] getAllHeaders(); | ||
|
||
StatusLine getStatusLine(); | ||
|
||
public interface ArtifactoryResponse extends JfrogResponse { | ||
String getRawBody(); | ||
|
||
<T> T parseBody(Class<T> toType) throws IOException; | ||
|
||
boolean isSuccessResponse(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
api/src/main/java/org/jfrog/artifactory/client/ArtifactoryStreamingResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.jfrog.artifactory.client; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
|
||
/** | ||
* ArtifactoryStreamingResponse object returned from {@link Artifactory#streamingRestCall(ArtifactoryRequest)}. | ||
* acts as a wrapper for {@link org.apache.http.HttpResponse}. | ||
*/ | ||
public interface ArtifactoryStreamingResponse extends JfrogResponse{ | ||
|
||
InputStream getInputStream() throws IOException; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/org/jfrog/artifactory/client/JfrogResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.jfrog.artifactory.client; | ||
import org.apache.http.Header; | ||
import org.apache.http.StatusLine; | ||
|
||
public interface JfrogResponse { | ||
|
||
Header[] getAllHeaders(); | ||
|
||
StatusLine getStatusLine(); | ||
|
||
boolean isSuccessResponse(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...s/src/main/groovy/org/jfrog/artifactory/client/impl/ArtifactoryStreamingResponseImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jfrog.artifactory.client.impl; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpStatus; | ||
import org.jfrog.artifactory.client.ArtifactoryStreamingResponse; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class ArtifactoryStreamingResponseImpl extends JfrogResponseImpl implements ArtifactoryStreamingResponse { | ||
|
||
public ArtifactoryStreamingResponseImpl(HttpResponse httpResponse) { | ||
super(httpResponse); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return getHttpResponse().getEntity().getContent(); | ||
} | ||
|
||
public boolean isSuccessResponse() { | ||
int status = getStatusLine().getStatusCode(); | ||
return (status == HttpStatus.SC_OK || | ||
status == HttpStatus.SC_PARTIAL_CONTENT); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
services/src/main/groovy/org/jfrog/artifactory/client/impl/JfrogResponseImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jfrog.artifactory.client.impl; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.StatusLine; | ||
|
||
public class JfrogResponseImpl { | ||
public JfrogResponseImpl(HttpResponse httpResponse) { | ||
this.httpResponse = httpResponse; | ||
} | ||
|
||
public HttpResponse getHttpResponse() { | ||
return httpResponse; | ||
} | ||
|
||
private final HttpResponse httpResponse; | ||
|
||
public Header[] getAllHeaders() { | ||
return this.httpResponse.getAllHeaders(); | ||
} | ||
|
||
public StatusLine getStatusLine() { | ||
return this.httpResponse.getStatusLine(); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
services/src/test/java/org/jfrog/artifactory/client/StreamingRestCallTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.jfrog.artifactory.client; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.HttpResponseException; | ||
import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
|
||
public class StreamingRestCallTest extends ArtifactoryTestsBase { | ||
|
||
@Test | ||
public void testDownloadWithHeadersByStreamingRestCall() throws IOException { | ||
InputStream inputStream = this.getClass().getResourceAsStream("/sample.txt"); | ||
assertNotNull(inputStream); | ||
artifactory.repository(localRepository.getKey()).upload(PATH, inputStream).withProperty("color", "blue") | ||
.withProperty("color", "red").doUpload(); | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
headers.put("Range", "bytes=0-10"); | ||
ArtifactoryRequest request = new ArtifactoryRequestImpl() | ||
.apiUrl(localRepository.getKey() + "/" + PATH) | ||
.method(ArtifactoryRequest.Method.GET) | ||
.setHeaders(headers) | ||
.requestType(ArtifactoryRequest.ContentType.JSON); | ||
|
||
ArtifactoryStreamingResponse response = artifactory.streamingRestCall(request); | ||
|
||
inputStream = response.getInputStream(); | ||
String actual = textFrom(inputStream); | ||
assertEquals(actual, textFrom(this.getClass().getResourceAsStream("/sample.txt")).substring(0, 11)); | ||
} | ||
|
||
@Test | ||
public void testErrorStreamingRestCall() throws IOException { | ||
ArtifactoryRequest request = new ArtifactoryRequestImpl() | ||
.apiUrl(localRepository.getKey() + "/" + PATH + "shouldNotExist") | ||
.method(ArtifactoryRequest.Method.GET) | ||
.requestType(ArtifactoryRequest.ContentType.JSON); | ||
try { | ||
artifactory.streamingRestCall(request); | ||
} catch (HttpResponseException e) { | ||
assertEquals(e.getStatusCode(), HttpStatus.SC_NOT_FOUND); | ||
assertEquals(e.getReasonPhrase(), "{\n" + | ||
" \"errors\" : [ {\n" + | ||
" \"status\" : 404,\n" + | ||
" \"message\" : \"File not found.\"\n" + | ||
" } ]\n" + | ||
"}"); | ||
} | ||
|
||
} | ||
} |