Search in sources :

Example 36 with HttpHeaders

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"));
    }
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 37 with HttpHeaders

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);
    }
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpResponse(com.azure.android.core.http.HttpResponse) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) HttpHeader(com.azure.android.core.http.HttpHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) Map(java.util.Map)

Aggregations

HttpHeaders (com.azure.android.core.http.HttpHeaders)37 Test (org.junit.jupiter.api.Test)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 HttpResponse (com.azure.android.core.http.HttpResponse)20 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)17 HttpMethod (com.azure.android.core.http.HttpMethod)16 Method (java.lang.reflect.Method)16 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 HashMap (java.util.HashMap)7 HttpRequest (com.azure.android.core.http.HttpRequest)6 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)6 Map (java.util.Map)6 OffsetDateTime (org.threeten.bp.OffsetDateTime)6 Response (com.azure.android.core.rest.Response)5 StreamResponse (com.azure.android.core.rest.StreamResponse)5 List (java.util.List)5 HttpCallback (com.azure.android.core.http.HttpCallback)4 HttpHeader (com.azure.android.core.http.HttpHeader)4 HttpPipeline (com.azure.android.core.http.HttpPipeline)4