Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.temporal.ChronoUnit;

import com.azure.resourcemanager.resources.fluentcore.utils.Utils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -171,7 +171,7 @@ public static void uploadFileToWebApp(PublishingProfile profile, String fileName

protected Response<String> curl(String urlString) throws IOException {
try {
return stringResponse(httpClient.getString(getHost(urlString), getPathAndQuery(urlString))).block();
return stringResponse(httpClient.getString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString))).block();
} catch (MalformedURLException e) {
Assertions.fail();
return null;
Expand All @@ -180,7 +180,7 @@ protected Response<String> curl(String urlString) throws IOException {

protected String post(String urlString, String body) {
try {
return stringResponse(httpClient.postString(getHost(urlString), getPathAndQuery(urlString), body))
return stringResponse(httpClient.postString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString), body))
.block()
.getValue();
} catch (Exception e) {
Expand All @@ -201,23 +201,6 @@ private static Mono<SimpleResponse<String>> stringResponse(Mono<SimpleResponse<F
response.getRequest(), response.getStatusCode(), response.getHeaders(), str)));
}

private static String getHost(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String protocol = url.getProtocol();
String host = url.getAuthority();
return protocol + "://" + host;
}

private static String getPathAndQuery(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String path = url.getPath();
String query = url.getQuery();
if (query != null && !query.isEmpty()) {
path = path + "?" + query;
}
return path;
}

protected WebAppTestClient httpClient =
RestProxy
.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,6 @@ public Mono<Indexable> invokeTaskAsync(TaskGroup.InvocationContext context) {
.doOnNext(createdExternalChild -> externalChild.setPendingOperation(PendingOperation.None))
.map(updatedExternalChild -> updatedExternalChild);
case ToBeRemoved:
// With 2.0 runtime, deleteResourceAsync() will be
// returning 'Completable' then use below code instead
//
// return this.externalChild.deleteResourceAsync().doOnCompleted(new Action0() {
// @Override
// public void call() {
// externalChild.setPendingOperation(PendingOperation.None);
// }
// }).andThen(voidObservable());
//
// TODO: Fix void mono result.
return this.externalChild.deleteResourceAsync()
.doOnSuccess(aVoid -> externalChild.setPendingOperation(PendingOperation.None))
.map(aVoid -> voidIndexable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
}

if (cloudError != null && MISSING_SUBSCRIPTION_REGISTRATION.equals(cloudError.getCode())) {
// TODO: add proxy in rest client
ResourceManager resourceManager = ResourceManager.authenticate(credential, profile)
.withDefaultSubscription();
Pattern providerPattern = Pattern.compile(".*'(.*)'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
package com.azure.resourcemanager.resources.fluentcore.utils;

import com.azure.core.annotation.Get;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.azure.resourcemanager.resources.fluentcore.model.Indexable;
import com.azure.resourcemanager.resources.models.Subscription;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -115,20 +123,48 @@ public static <U extends Indexable> Mono<U> rootResource(Mono<Indexable> stream)
* Download a file asynchronously.
*
* @param url the URL pointing to the file
* @param retrofit the retrofit client
* @param httpPipeline the http pipeline
* @return an Observable pointing to the content of the file
*/
public static Mono<byte[]> downloadFileAsync(String url, HttpPipeline httpPipeline) {
FileService service = RestProxy.create(FileService.class, httpPipeline);
try {
return service.download(getHost(url), getPathAndQuery(url))
.flatMap(response -> FluxUtil.collectBytesInByteBufferStream(response.getValue()));
} catch (MalformedURLException ex) {
return Mono.error(() -> ex);
}
}

/**
* Download a file asynchronously.
* Get host from url.
*
* @param url the URL pointing to the file
* @param retrofit the retrofit client
* @return an Observable pointing to the content of the file
* @param urlString the url string
* @return the host
* @throws MalformedURLException when url is invalid format
*/
public static Mono<byte[]> downloadFileAsync(String url, HttpPipeline retrofit) {
FileService service = RestProxy.create(FileService.class, retrofit);
Mono<HttpResponse> response = service.download(url);
return response.flatMap(httpResponse -> httpResponse.getBodyAsByteArray());
public static String getHost(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String protocol = url.getProtocol();
String host = url.getAuthority();
return protocol + "://" + host;
}

/**
* Get path from url.
*
* @param urlString the url string
* @return the path
* @throws MalformedURLException when the url is invalid format
*/
public static String getPathAndQuery(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String path = url.getPath();
String query = url.getQuery();
if (query != null && !query.isEmpty()) {
path = path + "?" + query;
}
return path;
}

/**
Expand Down Expand Up @@ -185,9 +221,12 @@ public static String resourceGroupId(String id) {
/**
* A Retrofit service used to download a file.
*/
@Host("{$host}")
@ServiceInterface(name = "FileService")
private interface FileService {
@Get("{url}")
Mono<HttpResponse> download(@PathParam("url") String url);
@Get("{path}")
Mono<SimpleResponse<Flux<ByteBuffer>>> download(
@HostParam("$host") String host, @PathParam(value = "path", encoded = true) String path);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

package com.azure.resourcemanager.resources;

import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.management.AzureEnvironment;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;
import com.azure.resourcemanager.resources.fluentcore.utils.Utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.temporal.ChronoUnit;

public class ResourceUtilsTests {
@Test
public void canExtractGroupFromId() throws Exception {
Expand Down Expand Up @@ -39,11 +47,16 @@ public void canExtractRelativePathFromId() throws Exception {

@Test
public void canDownloadFile() throws Exception {
// TODO(not known): Fix this
// RestProxy retrofit = new RestProxyBuilder().baseUrl("http://microsoft.com").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
// byte[] content = Utils.downloadFileAsync("http://google.com/humans.txt", retrofit).toBlocking().single();
// String contentString = new String(content);
// Assertions.assertNotNull(contentString);
HttpPipeline httpPipeline = new HttpPipelineBuilder()
.policies(
new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)),
new RetryPolicy("Retry-After", ChronoUnit.SECONDS)
)
.build();
byte[] content = Utils.downloadFileAsync("https://www.google.com/humans.txt", httpPipeline).block();
String contentString = new String(content);
Assertions.assertNotNull(contentString);
Assertions.assertTrue(contentString.startsWith("Google is built by a large team of engineers,"));
}

@Test
Expand Down