Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 17 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp-version}</version>
</dependency>
<dependency>
Expand All @@ -213,11 +213,16 @@
<artifactId>gson-fire</artifactId>
<version>${gson-fire-version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>${threetenbp-version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json-version}</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>${threetenbp-version}</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand All @@ -232,11 +237,12 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
<swagger-core-version>1.5.18</swagger-core-version>
<okhttp-version>2.7.5</okhttp-version>
<okhttp-version>3.11.0</okhttp-version>
<gson-version>2.8.1</gson-version>
<threetenbp-version>1.3.5</threetenbp-version>
<threetenbp-version>1.3.5</threetenbp-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.12</junit-version>
<json-version>20180130</json-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
106 changes: 66 additions & 40 deletions src/main/java/com/telstra/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@

package com.telstra;

import com.squareup.okhttp.*;
import com.squareup.okhttp.internal.http.HttpMethod;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink;
import okio.Okio;
import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import javax.net.ssl.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -39,19 +28,54 @@
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.json.JSONArray;
import org.json.JSONObject;
import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import com.telstra.auth.ApiKeyAuth;
import com.telstra.auth.Authentication;
import com.telstra.auth.HttpBasicAuth;
import com.telstra.auth.ApiKeyAuth;
import com.telstra.auth.OAuth;

public class ApiClient {
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.http.HttpMethod;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink;
import okio.Okio;

public class ApiClient {
private String basePath = "https://tapi.telstra.com/v2";
private boolean debugging = false;
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
Expand All @@ -78,10 +102,7 @@ public class ApiClient {
*/
public ApiClient() {
httpClient = new OkHttpClient();


verifyingSsl = true;

json = new JSON();

// Set default User-Agent.
Expand Down Expand Up @@ -421,7 +442,7 @@ public ApiClient setTempFolderPath(String tempFolderPath) {
* @return Timeout in milliseconds
*/
public int getConnectTimeout() {
return httpClient.getConnectTimeout();
return httpClient.connectTimeoutMillis();
}

/**
Expand All @@ -433,7 +454,7 @@ public int getConnectTimeout() {
* @return Api client
*/
public ApiClient setConnectTimeout(int connectionTimeout) {
httpClient.setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS);
httpClient = httpClient.newBuilder().connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS).build();
return this;
}

Expand All @@ -443,7 +464,7 @@ public ApiClient setConnectTimeout(int connectionTimeout) {
* @return Timeout in milliseconds
*/
public int getReadTimeout() {
return httpClient.getReadTimeout();
return httpClient.readTimeoutMillis();
}

/**
Expand All @@ -455,7 +476,7 @@ public int getReadTimeout() {
* @return Api client
*/
public ApiClient setReadTimeout(int readTimeout) {
httpClient.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
httpClient = httpClient.newBuilder().readTimeout(readTimeout, TimeUnit.MILLISECONDS).build();
return this;
}

Expand All @@ -465,7 +486,7 @@ public ApiClient setReadTimeout(int readTimeout) {
* @return Timeout in milliseconds
*/
public int getWriteTimeout() {
return httpClient.getWriteTimeout();
return httpClient.writeTimeoutMillis();
}

/**
Expand All @@ -477,7 +498,7 @@ public int getWriteTimeout() {
* @return Api client
*/
public ApiClient setWriteTimeout(int writeTimeout) {
httpClient.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
httpClient = httpClient.newBuilder().writeTimeout(writeTimeout, TimeUnit.MILLISECONDS).build();
return this;
}

Expand Down Expand Up @@ -709,6 +730,15 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
contentType = "application/json";
}
if (isJsonMime(contentType)) {
JSONObject jsonObj = new JSONObject(respBody);
JSONArray array = jsonObj.optJSONArray("Country");
if (array != null) {
String arrayString = array.toString();
arrayString = "\"" + arrayString.replaceAll("\"", "\'") + "\"";
jsonObj.put("Country", arrayString);
respBody = jsonObj.toString();
}

return json.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
Expand Down Expand Up @@ -869,13 +899,13 @@ public <T> void executeAsync(Call call, ApiCallback<T> callback) {
@SuppressWarnings("unchecked")
public <T> void executeAsync(Call call, final Type returnType, final ApiCallback<T> callback) {
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure(new ApiException(e), 0, null);
}
}

@Override
public void onResponse(Response response) throws IOException {
@Override
public void onResponse(Call call, Response response) throws IOException {
T result;
try {
result = (T) handleResponse(response, returnType);
Expand All @@ -884,7 +914,7 @@ public void onResponse(Response response) throws IOException {
return;
}
callback.onSuccess(result, response.code(), response.headers().toMultimap());
}
}
});
}

Expand All @@ -904,11 +934,7 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
// returning null if the returnType is not defined,
// or the status code is 204 (No Content)
if (response.body() != null) {
try {
response.body().close();
} catch (IOException e) {
throw new ApiException(response.message(), e, response.code(), response.headers().toMultimap());
}
response.body().close();
}
return null;
} else {
Expand Down Expand Up @@ -1095,9 +1121,9 @@ public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<
* @return RequestBody
*/
public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) {
FormEncodingBuilder formBuilder = new FormEncodingBuilder();
FormBody.Builder formBuilder = new FormBody.Builder();
for (Entry<String, Object> param : formParams.entrySet()) {
formBuilder.add(param.getKey(), parameterToString(param.getValue()));
formBuilder.add(param.getKey(), parameterToString(param.getValue()));
}
return formBuilder.build();
}
Expand All @@ -1110,7 +1136,7 @@ public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams)
* @return RequestBody
*/
public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
MultipartBuilder mpBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
MultipartBody.Builder mpBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
Expand Down Expand Up @@ -1184,11 +1210,11 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
if (keyManagers != null || trustManagers != null) {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, new SecureRandom());
httpClient.setSslSocketFactory(sslContext.getSocketFactory());
httpClient = httpClient.newBuilder().sslSocketFactory(sslContext.getSocketFactory()).build();
} else {
httpClient.setSslSocketFactory(null);
httpClient = httpClient.newBuilder().sslSocketFactory(null).build();
}
httpClient.setHostnameVerifier(hostnameVerifier);
httpClient = httpClient.newBuilder().hostnameVerifier(hostnameVerifier).build();
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/telstra/GzipRequestInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@

package com.telstra;

import com.squareup.okhttp.*;
import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;

import java.io.IOException;

/**
* Encodes request bodies using gzip.
*
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/telstra/ProgressRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

package com.telstra;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/telstra/ProgressResponseBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

package com.telstra;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.ResponseBody;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
Expand Down Expand Up @@ -45,12 +44,12 @@ public MediaType contentType() {
}

@Override
public long contentLength() throws IOException {
public long contentLength() {
return responseBody.contentLength();
}

@Override
public BufferedSource source() throws IOException {
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/telstra/auth/HttpBasicAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@

package com.telstra.auth;

import com.telstra.Pair;

import com.squareup.okhttp.Credentials;

import java.util.Map;
import java.util.List;
import java.util.Map;

import com.telstra.Pair;

import java.io.UnsupportedEncodingException;
import okhttp3.Credentials;

public class HttpBasicAuth implements Authentication {
private String username;
Expand Down
Loading