use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.
the class RestProxyTests method service24Put.
@Test
public void service24Put() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
final Map<String, String> headerCollection = new HashMap<>();
headerCollection.put("DEF", "GHIJ");
headerCollection.put("123", "45");
createService(Service24.class).put(headerCollection, new Callback<Response<HttpBinJSON>>() {
@Override
public void onSuccess(Response<HttpBinJSON> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "service24Put");
if (cbResult.error != null) {
Assertions.fail(cbResult.error);
} else {
final Response<HttpBinJSON> response = cbResult.response;
assertNotNull(response);
assertEquals(200, response.getStatusCode());
HttpBinJSON result = response.getValue();
assertNotNull(result.headers());
final HttpHeaders resultHeaders = new HttpHeaders(result.headers());
assertEquals("GHIJ", resultHeaders.getValue("ABCDEF"));
assertEquals("45", resultHeaders.getValue("ABC123"));
}
}
use of com.azure.android.core.http.HttpHeaders in project azure-sdk-for-android by Azure.
the class HttpUrlConnectionAsyncHttpClient method sendIntern.
private void sendIntern(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
if (cancellationToken.isCancellationRequested()) {
httpCallback.onError(new IOException("Canceled."));
return;
}
final HttpURLConnection connection;
try {
connection = (HttpURLConnection) httpRequest.getUrl().openConnection();
} catch (IOException ioe) {
httpCallback.onError(ioe);
return;
}
connection.setDoInput(true);
Throwable error = null;
HttpResponse httpResponse = null;
boolean hasResponseContent = false;
try {
// Request: headers
for (HttpHeader header : httpRequest.getHeaders()) {
connection.addRequestProperty(header.getName(), header.getValue());
}
// Request: method and content.
switch(httpRequest.getHttpMethod()) {
case GET:
case HEAD:
case OPTIONS:
case TRACE:
connection.setRequestMethod(httpRequest.getHttpMethod().toString());
break;
case PUT:
case POST:
case PATCH:
case DELETE:
connection.setRequestMethod(httpRequest.getHttpMethod().toString());
final byte[] requestContent = httpRequest.getBody();
if (requestContent != null) {
connection.setDoOutput(true);
final OutputStream requestContentStream = connection.getOutputStream();
try {
requestContentStream.write(requestContent);
} finally {
requestContentStream.close();
}
}
break;
default:
throw logger.logExceptionAsError(new IllegalStateException("Unknown HTTP Method:" + httpRequest.getHttpMethod()));
}
// Response: StatusCode
final int statusCode = connection.getResponseCode();
if (statusCode == -1) {
final IOException ioException = new IOException("Retrieval of HTTP response code failed. " + "HttpUrlConnection::getResponseCode() returned -1");
throw logger.logExceptionAsError(new RuntimeException(ioException));
}
// Response: headers
final Map<String, List<String>> connHeaderMap = connection.getHeaderFields();
final HttpHeaders headers = new HttpHeaders();
for (Map.Entry<String, List<String>> entry : connHeaderMap.entrySet()) {
if (entry.getKey() == null) {
continue;
}
final String headerName = entry.getKey();
final String headerValue;
Iterator<String> hdrValueItr = entry.getValue().iterator();
if (hdrValueItr.hasNext()) {
headerValue = hdrValueItr.next();
} else {
headerValue = null;
}
headers.put(headerName, headerValue);
}
// Response: Content
hasResponseContent = statusCode != HttpURLConnection.HTTP_NO_CONTENT && statusCode != HttpURLConnection.HTTP_NOT_MODIFIED && statusCode >= HttpURLConnection.HTTP_OK && httpRequest.getHttpMethod() != HttpMethod.HEAD;
final InputStream responseContentStream = hasResponseContent ? new ResponseContentStream(connection) : new ByteArrayInputStream(new byte[0]);
httpResponse = new UrlConnectionResponse(logger, httpRequest, statusCode, headers, responseContentStream);
} catch (Throwable e) {
error = e;
} finally {
if (error != null || !hasResponseContent) {
connection.disconnect();
}
}
if (error != null) {
httpCallback.onError(error);
return;
} else {
httpCallback.onSuccess(httpResponse);
}
}
Aggregations