Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Allow to provide a different HttpClient implementation #1067

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/android_build/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ android {
version "3.10.2"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}

dependencies {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
package com.microsoft.applications.events.maesdktest;

import com.microsoft.applications.events.HttpClientRequest;

import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*;

public class HttpClientRequestTest {
@Test
public void testHeaders() {
byte[] bytes = "key1value1key33key2value2".getBytes(UTF_8);
int[] header_lengths = new int[] { 4, 6, 5, 0, 4, 6 };

HttpClientRequest.Headers headers = new HttpClientRequest.Headers(header_lengths, bytes);
ArrayList<String> actual = new ArrayList<>();
while (headers.hasNext()) {
HttpClientRequest.HeaderEntry entry = headers.next();
actual.add(entry.key);
actual.add(entry.value);
}

Assert.assertEquals(6, headers.length);
Assert.assertArrayEquals(
new String[] {"key1", "value1", "key33", "", "key2", "value2"},
actual.toArray()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -86,16 +89,15 @@ public final void onCapabilitiesChanged(
private boolean m_metered;
}

class Request implements Runnable {
class Request implements HttpClientRequest {

Request(
HttpClient parent,
String url,
String method,
byte[] body,
String request_id,
int[] header_length,
byte[] header_buffer)
@NonNull HttpClient parent,
String url,
String method,
byte[] body,
String request_id,
@NonNull Headers headers)
throws java.io.IOException {
m_parent = parent;
m_connection = (HttpURLConnection) parent.newUrl(url).openConnection();
Expand All @@ -106,13 +108,9 @@ class Request implements Runnable {
m_connection.setDoOutput(true);
}
m_request_id = request_id;
int offset = 0;
for (int i = 0; i + 1 < header_length.length; i += 2) {
String k = new String(header_buffer, offset, header_length[i], UTF_8);
offset += header_length[i];
String v = new String(header_buffer, offset, header_length[i + 1], UTF_8);
offset += header_length[i + 1];
m_connection.setRequestProperty(k, v);
while (headers.hasNext()) {
HeaderEntry header = headers.next();
m_connection.setRequestProperty(header.key, header.value);
}
}

Expand Down Expand Up @@ -190,7 +188,7 @@ public class HttpClient {

/** Shim FutureTask: we would like to @Keep the cancel method for JNI */
static class FutureShim extends FutureTask<Boolean> {
FutureShim(Request inner) {
FutureShim(HttpClientRequest inner) {
super(inner, true);
}

Expand All @@ -202,7 +200,12 @@ public boolean cancel(boolean mayInterruptIfRunning) {
}

public HttpClient(Context context) {
this(context, new HttpClientRequest.Factory.AndroidUrlConnection());
}

public HttpClient(Context context, HttpClientRequest.Factory requestFactory) {
m_context = context;
m_requestFactory = requestFactory;
String path = System.getProperty("java.io.tmpdir");
setCacheFilePath(path);
setDeviceInfo(calculateID(context), Build.MANUFACTURER, Build.MODEL);
Expand Down Expand Up @@ -376,7 +379,8 @@ public FutureTask<Boolean> createTask(
int[] header_index,
byte[] header_buffer) {
try {
Request r = new Request(this, url, method, body, request_id, header_index, header_buffer);
HttpClientRequest.Headers headers = new HttpClientRequest.Headers(header_index, header_buffer);
HttpClientRequest r = m_requestFactory.create(this, url, method, body, request_id, headers);
return new FutureShim(r);
} catch (Exception e) {
return null;
Expand All @@ -393,4 +397,5 @@ public void executeTask(FutureTask<Boolean> t) {
private android.net.ConnectivityManager m_connectivityManager;
private PowerInfoReceiver m_power_receiver;
private final Context m_context;
}
private final HttpClientRequest.Factory m_requestFactory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
package com.microsoft.applications.events;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.Iterator;

import static java.nio.charset.StandardCharsets.UTF_8;

public interface HttpClientRequest extends Runnable {

interface Factory {
HttpClientRequest create(
@NonNull HttpClient parent,
String url,
String method,
byte[] body,
String request_id,
@NonNull Headers headers
) throws IOException;

class AndroidUrlConnection implements HttpClientRequest.Factory {
@Override
public HttpClientRequest create(@NonNull HttpClient parent, String url, String method, byte[] body, String request_id, @NonNull Headers headers) throws IOException {
return new Request(parent, url, method, body, request_id, headers);
}
}
}

final class HeaderEntry {
public final String key;
public final String value;

public HeaderEntry(String key, String value) {
this.key = key;
this.value = value;
}
}

final class Headers implements Iterator<HeaderEntry> {
public final int length;
private final int[] header_lengths;
private final byte[] buffer;
private int current = 0;
private int offset = 0;

public Headers(int[] header_lengths, byte[] buffer) {
this.length = header_lengths.length;
this.header_lengths = header_lengths;
this.buffer = buffer;
}

@Override
public boolean hasNext() {
return current + 2 <= length;
}

@Override
public HeaderEntry next() {
String k = new String(buffer, offset, header_lengths[current], UTF_8);
offset += header_lengths[current];
String v = new String(buffer, offset, header_lengths[current + 1], UTF_8);
offset += header_lengths[current + 1];
current += 2;
return new HeaderEntry(k,v);
}
}
}