Search in sources :

Example 1 with UrlBuilder

use of com.azure.android.core.http.util.UrlBuilder in project azure-sdk-for-android by Azure.

the class HttpRequestMapperTests method hostSubstitution.

@ParameterizedTest
@MethodSource("hostSubstitutionSupplier")
public void hostSubstitution(Method method, String rawHost, Object[] arguments, String expectedUrl) {
    HttpRequestMapper mapper = new HttpRequestMapper(rawHost, method, new JacksonSerder());
    UrlBuilder urlBuilder = new UrlBuilder();
    mapper.applySchemeAndHostMapping(arguments, urlBuilder);
    assertEquals(expectedUrl, urlBuilder.toString());
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) UrlBuilder(com.azure.android.core.http.util.UrlBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with UrlBuilder

use of com.azure.android.core.http.util.UrlBuilder in project azure-sdk-for-android by Azure.

the class HostPolicy method process.

@Override
public void process(HttpPipelinePolicyChain chain) {
    HttpRequest httpRequest = chain.getRequest();
    final UrlBuilder urlBuilder = UrlBuilder.parse(httpRequest.getUrl());
    try {
        httpRequest.setUrl(urlBuilder.setHost(host).toString());
    } catch (IllegalArgumentException error) {
        chain.completedError(error);
        return;
    }
    chain.processNextPolicy(httpRequest);
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) UrlBuilder(com.azure.android.core.http.util.UrlBuilder)

Example 3 with UrlBuilder

use of com.azure.android.core.http.util.UrlBuilder in project azure-sdk-for-android by Azure.

the class PortPolicy method process.

@Override
public void process(HttpPipelinePolicyChain chain) {
    HttpRequest httpRequest = chain.getRequest();
    final UrlBuilder urlBuilder = UrlBuilder.parse(httpRequest.getUrl());
    if (overwrite || urlBuilder.getPort() == null) {
        logger.info("Changing port to {}", port);
        try {
            httpRequest.setUrl(urlBuilder.setPort(port).toUrl().toString());
        } catch (MalformedURLException error) {
            chain.completedError(error);
            return;
        }
        chain.processNextPolicy(httpRequest);
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) MalformedURLException(java.net.MalformedURLException) UrlBuilder(com.azure.android.core.http.util.UrlBuilder)

Example 4 with UrlBuilder

use of com.azure.android.core.http.util.UrlBuilder in project azure-sdk-for-android by Azure.

the class ProtocolPolicy method process.

@Override
public void process(HttpPipelinePolicyChain chain) {
    HttpRequest httpRequest = chain.getRequest();
    final UrlBuilder urlBuilder = UrlBuilder.parse(httpRequest.getUrl());
    if (overwrite || urlBuilder.getScheme() == null) {
        logger.info("Setting protocol to {}", protocol);
        try {
            httpRequest.setUrl(urlBuilder.setScheme(protocol).toString());
        } catch (IllegalArgumentException error) {
            chain.completedError(error);
            return;
        }
        chain.processNextPolicy(httpRequest);
    } else {
        chain.processNextPolicy(httpRequest);
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) UrlBuilder(com.azure.android.core.http.util.UrlBuilder)

Example 5 with UrlBuilder

use of com.azure.android.core.http.util.UrlBuilder in project azure-sdk-for-android by Azure.

the class HttpRequestMapper method map.

HttpRequest map(Object[] swaggerMethodArgs) throws IOException {
    final String path = this.applyPathMappings(swaggerMethodArgs);
    UrlBuilder urlBuilder = UrlBuilder.parse(path);
    // (a simple scheme presence check to determine full URL) and ignore the Host annotation.
    if (urlBuilder.getScheme() == null) {
        urlBuilder = this.applySchemeAndHostMapping(swaggerMethodArgs, new UrlBuilder());
        // Set the path after host, concatenating the path segment in the host.
        if (path != null && !path.isEmpty() && !"/".equals(path)) {
            String hostPath = urlBuilder.getPath();
            if (hostPath == null || hostPath.isEmpty() || "/".equals(hostPath) || path.contains("://")) {
                urlBuilder.setPath(path);
            } else {
                urlBuilder.setPath(hostPath + "/" + path);
            }
        }
    }
    this.applyQueryMappings(swaggerMethodArgs, urlBuilder);
    final HttpRequest request = new HttpRequest(this.httpMethod, urlBuilder.toString());
    if (!this.formDataEntriesMapping.isEmpty()) {
        final String formData = this.applyFormDataMapping(swaggerMethodArgs);
        if (formData == null) {
            request.getHeaders().put("Content-Length", "0");
        } else {
            request.getHeaders().put("Content-Type", "application/x-www-form-urlencoded");
            request.setBody(formData);
        }
    } else {
        final Object content = this.retrieveContentArg(swaggerMethodArgs);
        if (content == null) {
            request.getHeaders().put("Content-Length", "0");
        } else {
            String contentType = this.contentType;
            if (contentType == null || contentType.isEmpty()) {
                if (content instanceof byte[] || content instanceof String) {
                    contentType = "application/octet-stream";
                } else {
                    contentType = "application/json";
                }
            }
            request.getHeaders().put("Content-Type", contentType);
            boolean isJson = false;
            final String[] contentTypeParts = contentType.split(";");
            for (final String contentTypePart : contentTypeParts) {
                if (contentTypePart.trim().equalsIgnoreCase("application/json")) {
                    isJson = true;
                    break;
                }
            }
            if (isJson) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                this.jacksonSerder.serialize(content, SerdeEncoding.JSON, stream);
                request.setHeader("Content-Length", String.valueOf(stream.size()));
                request.setBody(stream.toByteArray());
            } else if (content instanceof byte[]) {
                request.setBody((byte[]) content);
            } else if (content instanceof String) {
                final String contentString = (String) content;
                request.setBody(contentString);
            } else {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                this.jacksonSerder.serialize(content, SerdeEncoding.fromHeaders(request.getHeaders().toMap()), stream);
                request.setHeader("Content-Length", String.valueOf(stream.size()));
                request.setBody(stream.toByteArray());
            }
        }
    }
    // Headers from Swagger method arguments always take precedence over inferred headers from body types.
    HttpHeaders httpHeaders = request.getHeaders();
    this.applyHeaderMappings(swaggerMethodArgs, httpHeaders);
    return request;
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpHeaders(com.azure.android.core.http.HttpHeaders) UrlBuilder(com.azure.android.core.http.util.UrlBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

UrlBuilder (com.azure.android.core.http.util.UrlBuilder)8 HttpRequest (com.azure.android.core.http.HttpRequest)4 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 HttpHeaders (com.azure.android.core.http.HttpHeaders)1 HttpResponse (com.azure.android.core.http.HttpResponse)1 NextPolicyCallback (com.azure.android.core.http.NextPolicyCallback)1 PolicyCompleter (com.azure.android.core.http.PolicyCompleter)1 NetworkCallError (com.azure.android.core.test.models.NetworkCallError)1 NetworkCallRecord (com.azure.android.core.test.models.NetworkCallRecord)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1