-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Streaming Rest Call - returning the response input stream without… #379
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,6 +1,8 @@ | ||
package org.jfrog.artifactory.client; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
@@ -42,10 +44,14 @@ public interface Artifactory extends ApiInterface, AutoCloseable { | |
|
||
ArtifactoryResponse restCall(ArtifactoryRequest artifactoryRequest) throws IOException; | ||
|
||
ArtifactoryStreamingResponse streamingRestCall(ArtifactoryRequest artifactoryRequest) throws IOException; | ||
|
||
InputStream getInputStream(String path) throws IOException; | ||
|
||
InputStream getInputStreamWithHeaders(String path, Map<String, String> headers) throws IOException; | ||
|
||
HttpResponse execute(HttpUriRequest request) throws IOException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so |
||
|
||
default public <T> T get(String path, Class<? extends T> object, Class<T> interfaceObject) throws IOException { | ||
return null; | ||
} | ||
|
11 changes: 2 additions & 9 deletions
11
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,16 @@ | ||
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 BaseArtifactoryResponse { | ||
|
||
String getRawBody(); | ||
|
||
<T> T parseBody(Class<T> toType) throws IOException; | ||
|
||
boolean isSuccessResponse(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
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 BaseArtifactoryResponse, AutoCloseable { | ||
InputStream getInputStream() throws IOException; | ||
} |
13 changes: 13 additions & 0 deletions
13
api/src/main/java/org/jfrog/artifactory/client/BaseArtifactoryResponse.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 BaseArtifactoryResponse { | ||
|
||
Header[] getAllHeaders(); | ||
|
||
StatusLine getStatusLine(); | ||
|
||
boolean isSuccessResponse(); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...es/src/main/groovy/org/jfrog/artifactory/client/impl/AbstractArtifactoryResponseImpl.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,27 @@ | ||
package org.jfrog.artifactory.client.impl; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.StatusLine; | ||
|
||
public abstract class AbstractArtifactoryResponseImpl { | ||
|
||
private final HttpResponse httpResponse; | ||
|
||
public AbstractArtifactoryResponseImpl(HttpResponse httpResponse) { | ||
this.httpResponse = httpResponse; | ||
} | ||
|
||
public HttpResponse getHttpResponse() { | ||
return httpResponse; | ||
} | ||
|
||
public Header[] getAllHeaders() { | ||
return this.httpResponse.getAllHeaders(); | ||
} | ||
|
||
public StatusLine getStatusLine() { | ||
return this.httpResponse.getStatusLine(); | ||
} | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
...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,39 @@ | ||
package org.jfrog.artifactory.client.impl; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpEntity; | ||
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 AbstractArtifactoryResponseImpl implements ArtifactoryStreamingResponse { | ||
|
||
public ArtifactoryStreamingResponseImpl(HttpResponse httpResponse) { | ||
super(httpResponse); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
InputStream is = null; | ||
HttpEntity entity = getHttpResponse().getEntity(); | ||
if (entity != null) { | ||
is = entity.getContent(); | ||
} | ||
return is; | ||
} | ||
|
||
@Override | ||
public boolean isSuccessResponse() { | ||
int status = getStatusLine().getStatusCode(); | ||
return (status == HttpStatus.SC_OK || | ||
status == HttpStatus.SC_PARTIAL_CONTENT); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
IOUtils.close(getInputStream()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
package org.jfrog.artifactory.client; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
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); | ||
assertTrue(response.isSuccessResponse()); | ||
|
||
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); | ||
ArtifactoryStreamingResponse response = artifactory.streamingRestCall(request); | ||
assertFalse(response.isSuccessResponse()); | ||
assertEquals(response.getStatusLine().getStatusCode(), 404); | ||
String raw = IOUtils.toString(response.getInputStream(), StandardCharsets.UTF_8); | ||
assertEquals(raw, "{\n" + | ||
" \"errors\" : [ {\n" + | ||
" \"status\" : 404,\n" + | ||
" \"message\" : \"File not found.\"\n" + | ||
" } ]\n" + | ||
"}"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document it to the README
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done and tested