forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress reporter promoted to core and used in Http Client. (Azure#29495
) * progress reporter. * contexts * comment. * this works. * progress handlers for http clients. * vertx. * fix build. * changelogs. * poke ci * align naming with existing versions. * functional interface. * getContext. * this might come handy. * some PR feedback. * remove progress handler after request./ * some feedback. * add sendSync test. * wip. * contexts. * make it final. * changelog. * rename this. * sample update. * Update sdk/core/azure-core/src/main/java/com/azure/core/util/ProgressReporter.java Co-authored-by: Srikanta <[email protected]> * pr feedback. * samples in chlog. * hide keys for now. Co-authored-by: Srikanta <[email protected]>
- Loading branch information
1 parent
4a444eb
commit f478529
Showing
24 changed files
with
881 additions
and
69 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
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
41 changes: 41 additions & 0 deletions
41
...c/main/java/com/azure/core/http/netty/implementation/RequestProgressReportingHandler.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,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.http.netty.implementation; | ||
|
||
import com.azure.core.util.ProgressReporter; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.ByteBufHolder; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelOutboundHandlerAdapter; | ||
import io.netty.channel.ChannelPromise; | ||
import io.netty.channel.FileRegion; | ||
|
||
import java.util.Objects; | ||
|
||
public final class RequestProgressReportingHandler extends ChannelOutboundHandlerAdapter { | ||
/** | ||
* Name of the handler when it is added into a ChannelPipeline. | ||
*/ | ||
public static final String HANDLER_NAME = "azureRequestProgressHandler"; | ||
|
||
private final ProgressReporter progressReporter; | ||
|
||
public RequestProgressReportingHandler(ProgressReporter progressReporter) { | ||
this.progressReporter = Objects.requireNonNull(progressReporter, "'progressReporter' must not be null"); | ||
} | ||
|
||
@Override | ||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { | ||
|
||
if (msg instanceof ByteBuf) { | ||
progressReporter.reportProgress(((ByteBuf) msg).readableBytes()); | ||
} else if (msg instanceof ByteBufHolder) { | ||
progressReporter.reportProgress(((ByteBufHolder) msg).content().readableBytes()); | ||
} else if (msg instanceof FileRegion) { | ||
progressReporter.reportProgress(((FileRegion) msg).count()); | ||
} | ||
|
||
ctx.write(msg, promise.unvoid()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...src/test/java/com/azure/core/http/netty/NettyAsyncHttpClientHttpClientWithHttpsTests.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,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.http.netty; | ||
|
||
import com.azure.core.http.HttpClient; | ||
import com.azure.core.test.HttpClientTestsWireMockServer; | ||
import com.azure.core.test.http.HttpClientTests; | ||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslContextBuilder; | ||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
import javax.net.ssl.SSLException; | ||
|
||
/** | ||
* Reactor Netty {@link HttpClientTests} with https. | ||
* Some request logic branches out if it's https like file uploads. | ||
*/ | ||
public class NettyAsyncHttpClientHttpClientWithHttpsTests extends HttpClientTests { | ||
private static WireMockServer server; | ||
|
||
private static final HttpClient HTTP_CLIENT_INSTANCE; | ||
|
||
static { | ||
try { | ||
SslContext sslContext = SslContextBuilder.forClient() | ||
.trustManager(InsecureTrustManagerFactory.INSTANCE) | ||
.build(); | ||
|
||
reactor.netty.http.client.HttpClient nettyHttpClient = | ||
reactor.netty.http.client.HttpClient.create() | ||
.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext)); | ||
|
||
HTTP_CLIENT_INSTANCE = new NettyAsyncHttpClientBuilder(nettyHttpClient).build(); | ||
} catch (SSLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@BeforeAll | ||
public static void getWireMockServer() { | ||
server = HttpClientTestsWireMockServer.getHttpClientTestsServer(); | ||
server.start(); | ||
} | ||
|
||
@AfterAll | ||
public static void shutdownWireMockServer() { | ||
if (server != null) { | ||
server.shutdown(); | ||
} | ||
} | ||
|
||
@Override | ||
protected int getWireMockPort() { | ||
return server.httpsPort(); | ||
} | ||
|
||
@Override | ||
protected boolean isSecure() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected HttpClient createHttpClient() { | ||
return HTTP_CLIENT_INSTANCE; | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
...in/java/com/azure/core/http/okhttp/implementation/OkHttpProgressReportingRequestBody.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,77 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.core.http.okhttp.implementation; | ||
|
||
import com.azure.core.util.ProgressReporter; | ||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okio.Buffer; | ||
import okio.BufferedSink; | ||
import okio.ForwardingSink; | ||
import okio.Okio; | ||
import okio.Sink; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* An {@link okhttp3.RequestBody} subtype that adds progress | ||
* reporting functionality on top of other {@link okhttp3.RequestBody}. | ||
*/ | ||
public final class OkHttpProgressReportingRequestBody extends RequestBody { | ||
private final RequestBody delegate; | ||
private final ProgressReporter progressReporter; | ||
|
||
public OkHttpProgressReportingRequestBody(RequestBody delegate, ProgressReporter progressReporter) { | ||
this.delegate = Objects.requireNonNull(delegate, "'delegate' must not be null"); | ||
this.progressReporter = Objects.requireNonNull(progressReporter, "'progressReporter' must not be null"); | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return delegate.contentType(); | ||
} | ||
|
||
@Override | ||
public long contentLength() throws IOException { | ||
return delegate.contentLength(); | ||
} | ||
|
||
@Override | ||
public boolean isOneShot() { | ||
return delegate.isOneShot(); | ||
} | ||
|
||
@Override | ||
public boolean isDuplex() { | ||
return delegate.isDuplex(); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
BufferedSink bufferedSink; | ||
|
||
CountingSink countingSink = new CountingSink(sink, progressReporter); | ||
bufferedSink = Okio.buffer(countingSink); | ||
|
||
delegate.writeTo(bufferedSink); | ||
|
||
bufferedSink.flush(); | ||
} | ||
|
||
private static final class CountingSink extends ForwardingSink { | ||
private final ProgressReporter progressReporter; | ||
|
||
CountingSink(Sink delegate, ProgressReporter progressReporter) { | ||
super(delegate); | ||
this.progressReporter = progressReporter; | ||
} | ||
|
||
@Override | ||
public void write(Buffer source, long byteCount) throws IOException { | ||
super.write(source, byteCount); | ||
progressReporter.reportProgress(byteCount); | ||
} | ||
} | ||
} |
Oops, something went wrong.