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());
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations