HttpRequest map()

in sdk/core/azure-core-rest/src/main/java/com/azure/android/core/rest/HttpRequestMapper.java [142:224]


    HttpRequest map(Object[] swaggerMethodArgs) throws IOException {
        final String path = this.applyPathMappings(swaggerMethodArgs);
        UrlBuilder urlBuilder = UrlBuilder.parse(path);

        // Sometimes a full URL will be provided as the value of PathParam annotated argument.
        // This mainly happens in paging scenarios, in such cases, we use the full URL
        // (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;
    }