If you think the Android project matrix-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2014 OpenMarket Ltd//fromwww.java2s.com
*
* 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 org.matrix.androidsdk;
import android.net.Uri;
import android.util.Log;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.OkHttpClient;
import org.matrix.androidsdk.rest.model.login.Credentials;
import java.util.concurrent.TimeUnit;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;
/**
* Class for making Matrix API calls.
*/publicabstractclass RestClient {
privatestaticfinal String LOG_TAG = "RestClient";
privatestaticfinal String URI_PREFIX = "/_matrix/client/api/v1";
privatestaticfinal String PARAM_ACCESS_TOKEN = "access_token";
privatestaticfinalint CONNECTION_TIMEOUT_MS = 60000;
privatestaticfinalint READ_TIMEOUT_MS = 60000;
protected Credentials mCredentials;
protected Gson gson;
/**
* Public constructor.
* @param hsUri The http[s] URI to the home server.
*/public RestClient(Uri hsUri) {
// sanity check
if (hsUri == null || (!"http".equals(hsUri.getScheme()) && !"https".equals(hsUri.getScheme())) ) {
thrownew RuntimeException("Invalid home server URI: "+hsUri);
}
// The JSON -> object mapper
gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
// HTTP client
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
okHttpClient.setReadTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS);
// Rest adapter for turning API interfaces into actual REST-calling objects
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(hsUri.toString() + URI_PREFIX)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.setRequestInterceptor(new RequestInterceptor() {
@Override
publicvoid intercept(RequestInterceptor.RequestFacade request) {
// Add the access token to all requests if it is set
if ((mCredentials != null) && (mCredentials.accessToken != null)) {
request.addEncodedQueryParam(PARAM_ACCESS_TOKEN, mCredentials.accessToken);
}
}
})
.build();
restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
initApi(restAdapter);
}
/**
* Initialize the API object using the given RestAdapter.
* @param restAdapter the Retrofit RestAdapter used to create API objects
*/protectedabstractvoid initApi(RestAdapter restAdapter);
/**
* Constructor providing the full user credentials. To use to avoid having to log the user in.
* @param credentials the user credentials
*/public RestClient(Credentials credentials) {
this(Uri.parse(credentials.homeServer));
mCredentials = credentials;
}
/**
* Get the user's credentials. Typically for saving them somewhere persistent.
* @return the user credentials
*/public Credentials getCredentials() {
return mCredentials;
}
/**
* Provide the user's credentials. To be called after login or registration.
* @param credentials the user credentials
*/publicvoid setCredentials(Credentials credentials) {
mCredentials = credentials;
}
/**
* Default protected constructor for unit tests.
*/protected RestClient() {
}
}