-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from cdapio/responsestream
[CDAP-17651] Add support streaming http response
- Loading branch information
Showing
8 changed files
with
304 additions
and
47 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
common-http/src/main/java/io/cdap/common/http/HttpContentConsumer.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,53 @@ | ||
/* | ||
* Copyright © 2021 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.common.http; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Consumer for {@link HttpResponse} body. | ||
*/ | ||
public abstract class HttpContentConsumer { | ||
// Default 64K chunk size | ||
private static final int DEFAULT_CHUNK_SIZE = 65536; | ||
private int chunkSize; | ||
|
||
public HttpContentConsumer() { | ||
this.chunkSize = DEFAULT_CHUNK_SIZE; | ||
} | ||
|
||
public HttpContentConsumer(int chunkSize) { | ||
this.chunkSize = chunkSize; | ||
} | ||
|
||
/** | ||
* This method is invoked when a new chunk of the response body is available to be consumed. | ||
* | ||
* @param chunk a {@link ByteBuffer} containing a chunk of the response body | ||
* @return true to continue reading from the response stream, false to stop reading and close the connection. | ||
*/ | ||
public abstract boolean onReceived(ByteBuffer chunk); | ||
|
||
/** | ||
* This method is invoked when the end of the response body is reached. | ||
*/ | ||
public abstract void onFinished(); | ||
|
||
int getChunkSize() { | ||
return chunkSize; | ||
} | ||
} |
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
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
61 changes: 61 additions & 0 deletions
61
common-http/src/test/java/io/cdap/common/http/HttpRequestsStreamTest.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,61 @@ | ||
/* | ||
* Copyright © 2021 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.common.http; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class HttpRequestsStreamTest extends HttpRequestsTestBase { | ||
|
||
private static TestHttpService httpService; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
httpService = new TestHttpService(false); | ||
httpService.startAndWait(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
httpService.stopAndWait(); | ||
} | ||
|
||
@Override | ||
protected URI getBaseURI() throws URISyntaxException { | ||
InetSocketAddress bindAddress = httpService.getBindAddress(); | ||
return new URI("http://" + bindAddress.getHostName() + ":" + bindAddress.getPort()); | ||
} | ||
|
||
@Override | ||
protected HttpRequestConfig getHttpRequestsConfig() { | ||
return new HttpRequestConfig(0, 0, false); | ||
} | ||
|
||
@Override | ||
protected int getNumConnectionsOpened() { | ||
return httpService.getNumConnectionsOpened(); | ||
} | ||
|
||
@Override | ||
protected boolean returnResponseStream() { | ||
return true; | ||
} | ||
} |
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
Oops, something went wrong.