Merge "Add authentication/http request related code to LatinIme"

This commit is contained in:
Sandeep Siddhartha 2014-09-26 17:22:11 +00:00 committed by Android (Google) Code Review
commit 569f6f1c9e
5 changed files with 698 additions and 0 deletions

View file

@ -0,0 +1,67 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.accounts;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import java.io.IOException;
/**
* Utility class that handles generation/invalidation of auth tokens in the app.
*/
public class AuthUtils {
private final AccountManager mAccountManager;
public AuthUtils(Context context) {
mAccountManager = AccountManager.get(context);
}
/**
* @see AccountManager#invalidateAuthToken(String, String)
*/
public void invalidateAuthToken(final String accountType, final String authToken) {
mAccountManager.invalidateAuthToken(accountType, authToken);
}
/**
* @see AccountManager#getAuthToken(
* Account, String, Bundle, boolean, AccountManagerCallback, Handler)
*/
public AccountManagerFuture<Bundle> getAuthToken(final Account account,
final String authTokenType, final Bundle options, final boolean notifyAuthFailure,
final AccountManagerCallback<Bundle> callback, final Handler handler) {
return mAccountManager.getAuthToken(account, authTokenType, options, notifyAuthFailure,
callback, handler);
}
/**
* @see AccountManager#blockingGetAuthToken(Account, String, boolean)
*/
public String blockingGetAuthToken(final Account account, final String authTokenType,
final boolean notifyAuthFailure) throws OperationCanceledException,
AuthenticatorException, IOException {
return mAccountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure);
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.network;
import com.android.inputmethod.annotations.UsedForTesting;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A client for executing HTTP requests synchronously.
* This must never be called from the main thread.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public class BlockingHttpClient {
private final HttpURLConnection mConnection;
/**
* Interface that handles processing the response for a request.
*/
public interface ResponseProcessor {
/**
* Called when the HTTP request fails with an error.
*
* @param httpStatusCode The status code of the HTTP response.
* @param message The HTTP response message, if any, or null.
*/
void onError(int httpStatusCode, @Nullable String message);
/**
* Called when the HTTP request finishes successfully.
* The {@link InputStream} is closed by the client after the method finishes,
* so any processing must be done in this method itself.
*
* @param response An input stream that can be used to read the HTTP response.
*/
void onSuccess(InputStream response);
}
/**
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public BlockingHttpClient(HttpURLConnection connection) {
mConnection = connection;
}
/**
* Executes the request on the underlying {@link HttpURLConnection}.
*
* TODO: Remove @UsedForTesting after this is actually used.
*
* @param request The request payload, if any, or null.
* @param responeProcessor A processor for the HTTP response.
*/
@UsedForTesting
public void execute(@Nullable byte[] request, @Nonnull ResponseProcessor responseProcessor)
throws IOException {
try {
if (request != null) {
OutputStream out = new BufferedOutputStream(mConnection.getOutputStream());
out.write(request);
out.flush();
out.close();
}
final int responseCode = mConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
responseProcessor.onError(responseCode, mConnection.getResponseMessage());
} else {
responseProcessor.onSuccess(mConnection.getInputStream());
}
} finally {
mConnection.disconnect();
}
}
}

View file

@ -0,0 +1,213 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.network;
import android.text.TextUtils;
import com.android.inputmethod.annotations.UsedForTesting;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* Builder for {@link HttpURLConnection}s.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public class HttpUrlConnectionBuilder {
private static final int DEFAULT_TIMEOUT_MILLIS = 5 * 1000;
/**
* Request header key for cache control.
*/
public static final String KEY_CACHE_CONTROL = "Cache-Control";
/**
* Request header value for cache control indicating no caching.
* @see #KEY_CACHE_CONTROL
*/
public static final String VALUE_NO_CACHE = "no-cache";
/**
* Indicates that the request is unidirectional - upload-only.
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public static final int MODE_UPLOAD_ONLY = 1;
/**
* Indicates that the request is unidirectional - download only.
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public static final int MODE_DOWNLOAD_ONLY = 2;
/**
* Indicates that the request is bi-directional.
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public static final int MODE_BI_DIRECTIONAL = 3;
private final HashMap<String, String> mHeaderMap = new HashMap<>();
private URL mUrl;
private int mConnectTimeoutMillis = DEFAULT_TIMEOUT_MILLIS;
private int mReadTimeoutMillis = DEFAULT_TIMEOUT_MILLIS;
private int mContentLength = -1;
private boolean mUseCache;
private int mMode;
/**
* Sets the URL that'll be used for the request.
* This *must* be set before calling {@link #build()}
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setUrl(String url) throws MalformedURLException {
if (TextUtils.isEmpty(url)) {
throw new IllegalArgumentException("URL must not be empty");
}
mUrl = new URL(url);
return this;
}
/**
* Sets the connect timeout. Defaults to {@value #DEFAULT_TIMEOUT} milliseconds.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setConnectTimeout(int timeoutMillis) {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("connect-timeout must be >= 0, but was "
+ timeoutMillis);
}
mConnectTimeoutMillis = timeoutMillis;
return this;
}
/**
* Sets the read timeout. Defaults to {@value #DEFAULT_TIMEOUT} milliseconds.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setReadTimeout(int timeoutMillis) {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("read-timeout must be >= 0, but was "
+ timeoutMillis);
}
mReadTimeoutMillis = timeoutMillis;
return this;
}
/**
* Adds an entry to the request header.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder addHeader(String key, String value) {
mHeaderMap.put(key, value);
return this;
}
/**
* Sets the request to be executed such that the input is not buffered.
* This may be set when the request size is known beforehand.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setFixedLengthForStreaming(int length) {
mContentLength = length;
return this;
}
/**
* Indicates if the request can use cached responses or not.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setUseCache(boolean useCache) {
mUseCache = useCache;
return this;
}
/**
* The request mode.
* Sets the request mode to be one of: upload-only, download-only or bidirectional.
*
* @see #MODE_UPLOAD_ONLY
* @see #MODE_DOWNLOAD_ONLY
* @see #MODE_BI_DIRECTIONAL
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpUrlConnectionBuilder setMode(int mode) {
if (mode != MODE_UPLOAD_ONLY
&& mode != MODE_DOWNLOAD_ONLY
&& mode != MODE_BI_DIRECTIONAL) {
throw new IllegalArgumentException("Invalid mode specified:" + mode);
}
mMode = mode;
return this;
}
/**
* Builds the {@link HttpURLConnection} instance that can be used to execute the request.
*
* TODO: Remove @UsedForTesting after this is actually used.
*/
@UsedForTesting
public HttpURLConnection build() throws IOException {
if (mUrl == null) {
throw new IllegalArgumentException("A URL must be specified!");
}
final HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection();
connection.setConnectTimeout(mConnectTimeoutMillis);
connection.setReadTimeout(mReadTimeoutMillis);
connection.setUseCaches(mUseCache);
switch (mMode) {
case MODE_UPLOAD_ONLY:
connection.setDoInput(true);
connection.setDoOutput(false);
break;
case MODE_DOWNLOAD_ONLY:
connection.setDoInput(false);
connection.setDoOutput(true);
break;
case MODE_BI_DIRECTIONAL:
connection.setDoInput(true);
connection.setDoOutput(true);
break;
}
for (final Entry<String, String> entry : mHeaderMap.entrySet()) {
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
if (mContentLength >= 0) {
connection.setFixedLengthStreamingMode(mContentLength);
}
return connection;
}
}

View file

@ -0,0 +1,174 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.network;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.inputmethod.latin.network.BlockingHttpClient.ResponseProcessor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.Random;
/**
* Tests for {@link BlockingHttpClient}.
*/
@SmallTest
public class BlockingHttpClientTests extends AndroidTestCase {
@Mock HttpURLConnection mMockHttpConnection;
@Override
protected void setUp() throws Exception {
super.setUp();
MockitoAnnotations.initMocks(this);
}
public void testError_badGateway() throws IOException {
when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_GATEWAY);
final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection);
final FakeErrorResponseProcessor processor =
new FakeErrorResponseProcessor(HttpURLConnection.HTTP_BAD_GATEWAY);
client.execute(null /* empty request */, processor);
assertTrue("ResponseProcessor was not invoked", processor.mInvoked);
}
public void testError_clientTimeout() throws IOException {
when(mMockHttpConnection.getResponseCode()).thenReturn(
HttpURLConnection.HTTP_CLIENT_TIMEOUT);
final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection);
final FakeErrorResponseProcessor processor =
new FakeErrorResponseProcessor(HttpURLConnection.HTTP_CLIENT_TIMEOUT);
client.execute(null /* empty request */, processor);
assertTrue("ResponseProcessor was not invoked", processor.mInvoked);
}
public void testError_forbiddenWithRequest() throws IOException {
final OutputStream mockOutputStream = Mockito.mock(OutputStream.class);
when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_FORBIDDEN);
when(mMockHttpConnection.getOutputStream()).thenReturn(mockOutputStream);
final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection);
final FakeErrorResponseProcessor processor =
new FakeErrorResponseProcessor(HttpURLConnection.HTTP_FORBIDDEN);
client.execute(new byte[100], processor);
verify(mockOutputStream).write(any(byte[].class), eq(0), eq(100));
assertTrue("ResponseProcessor was not invoked", processor.mInvoked);
}
public void testSuccess_emptyRequest() throws IOException {
final Random rand = new Random();
byte[] response = new byte[100];
rand.nextBytes(response);
when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(mMockHttpConnection.getInputStream()).thenReturn(new ByteArrayInputStream(response));
final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection);
final FakeSuccessResponseProcessor processor =
new FakeSuccessResponseProcessor(response);
client.execute(null /* empty request */, processor);
assertTrue("ResponseProcessor was not invoked", processor.mInvoked);
}
public void testSuccess() throws IOException {
final OutputStream mockOutputStream = Mockito.mock(OutputStream.class);
final Random rand = new Random();
byte[] response = new byte[100];
rand.nextBytes(response);
when(mMockHttpConnection.getOutputStream()).thenReturn(mockOutputStream);
when(mMockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(mMockHttpConnection.getInputStream()).thenReturn(new ByteArrayInputStream(response));
final BlockingHttpClient client = new BlockingHttpClient(mMockHttpConnection);
final FakeSuccessResponseProcessor processor =
new FakeSuccessResponseProcessor(response);
client.execute(new byte[100], processor);
assertTrue("ResponseProcessor was not invoked", processor.mInvoked);
}
private static class FakeErrorResponseProcessor implements ResponseProcessor {
private final int mExpectedStatusCode;
boolean mInvoked;
FakeErrorResponseProcessor(int expectedStatusCode) {
mExpectedStatusCode = expectedStatusCode;
}
@Override
public void onError(int httpStatusCode, String message) {
mInvoked = true;
assertEquals("onError:", mExpectedStatusCode, httpStatusCode);
}
@Override
public void onSuccess(InputStream response) {
fail("Expected an error but received success");
}
}
private static class FakeSuccessResponseProcessor implements ResponseProcessor {
private final byte[] mExpectedResponse;
boolean mInvoked;
FakeSuccessResponseProcessor(byte[] expectedResponse) {
mExpectedResponse = expectedResponse;
}
@Override
public void onError(int httpStatusCode, String message) {
fail("Expected a response but received an error");
}
@Override
public void onSuccess(InputStream response) {
try {
mInvoked = true;
BufferedInputStream in = new BufferedInputStream(response);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = in.read()) != -1) {
buffer.write(read);
}
byte[] actualResponse = buffer.toByteArray();
in.close();
assertTrue("Response doesn't match",
Arrays.equals(mExpectedResponse, actualResponse));
} catch (IOException ex) {
fail("IOException in onSuccess");
}
}
}
}

View file

@ -0,0 +1,145 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.network;
import static com.android.inputmethod.latin.network.HttpUrlConnectionBuilder.MODE_BI_DIRECTIONAL;
import static com.android.inputmethod.latin.network.HttpUrlConnectionBuilder.MODE_DOWNLOAD_ONLY;
import static com.android.inputmethod.latin.network.HttpUrlConnectionBuilder.MODE_UPLOAD_ONLY;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
/**
* Tests for {@link HttpUrlConnectionBuilder}.
*/
@SmallTest
public class HttpUrlConnectionBuilderTests extends AndroidTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testSetUrl_malformed() {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
try {
builder.setUrl("dadasd!@%@!:11");
fail("Expected a MalformedURLException.");
} catch (MalformedURLException e) {
// Expected
}
}
public void testSetConnectTimeout_invalid() {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
try {
builder.setConnectTimeout(-1);
fail("Expected an IllegalArgumentException.");
} catch (IllegalArgumentException e) {
// Expected
}
}
public void testSetConnectTimeout() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("https://www.example.com");
builder.setConnectTimeout(8765);
HttpURLConnection connection = builder.build();
assertEquals(8765, connection.getConnectTimeout());
}
public void testSetReadTimeout_invalid() {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
try {
builder.setReadTimeout(-1);
fail("Expected an IllegalArgumentException.");
} catch (IllegalArgumentException e) {
// Expected
}
}
public void testSetReadTimeout() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("https://www.example.com");
builder.setReadTimeout(8765);
HttpURLConnection connection = builder.build();
assertEquals(8765, connection.getReadTimeout());
}
public void testAddHeader() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("http://www.example.com");
builder.addHeader("some-random-key", "some-random-value");
HttpURLConnection connection = builder.build();
assertEquals("some-random-value", connection.getRequestProperty("some-random-key"));
}
public void testSetUseCache_notSet() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("http://www.example.com");
HttpURLConnection connection = builder.build();
assertFalse(connection.getUseCaches());
}
public void testSetUseCache_false() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("http://www.example.com");
HttpURLConnection connection = builder.build();
connection.setUseCaches(false);
assertFalse(connection.getUseCaches());
}
public void testSetUseCache_true() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("http://www.example.com");
HttpURLConnection connection = builder.build();
connection.setUseCaches(true);
assertTrue(connection.getUseCaches());
}
public void testSetMode_uploadOnly() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("http://www.example.com");
builder.setMode(MODE_UPLOAD_ONLY);
HttpURLConnection connection = builder.build();
assertTrue(connection.getDoInput());
assertFalse(connection.getDoOutput());
}
public void testSetMode_downloadOnly() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("https://www.example.com");
builder.setMode(MODE_DOWNLOAD_ONLY);
HttpURLConnection connection = builder.build();
assertFalse(connection.getDoInput());
assertTrue(connection.getDoOutput());
}
public void testSetMode_bidirectional() throws IOException {
HttpUrlConnectionBuilder builder = new HttpUrlConnectionBuilder();
builder.setUrl("https://www.example.com");
builder.setMode(MODE_BI_DIRECTIONAL);
HttpURLConnection connection = builder.build();
assertTrue(connection.getDoInput());
assertTrue(connection.getDoOutput());
}
}