diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java index 12571a88..517dd56a 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpClient.java @@ -57,6 +57,7 @@ public class HttpClient { private static OkHttpClient sOkHttpClient; private static Context sContext; private static HashMap> sCookieStore = new HashMap<>(); + private static LogInterceptor sLogInterceptor; public static OkHttpClient getOkHttpClient() { if (sOkHttpClient == null) { @@ -111,6 +112,7 @@ public class HttpClient { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() .protocols(Arrays.asList(Protocol.HTTP_1_1)) + .addInterceptor(getLogInterceptor()) .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) @@ -137,6 +139,13 @@ public class HttpClient { sContext = context; } + public static LogInterceptor getLogInterceptor() { + if (sLogInterceptor == null) { + sLogInterceptor = new LogInterceptor(); + } + return sLogInterceptor; + } + public List getCookiesFromUrl(HttpUrl httpUrl) { return sCookieStore.get(httpUrl.host()); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt new file mode 100644 index 00000000..ef9df143 --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogBuilder.kt @@ -0,0 +1,39 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2020 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.owncloud.android.lib.common.http + +object LogBuilder { + fun toLogString( + networkPetition: NetworkPetition, + networkNode: NetworkNode, + description: String + ): String = "[Network, $networkPetition] [$networkNode] $description" +} + +enum class NetworkPetition { + REQUEST, RESPONSE +} + +enum class NetworkNode { + INFO, HEADER, BODY +} diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt new file mode 100644 index 00000000..89fa09bd --- /dev/null +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/LogInterceptor.kt @@ -0,0 +1,57 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2020 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package com.owncloud.android.lib.common.http + +import com.owncloud.android.lib.common.http.LogBuilder.toLogString +import okhttp3.Interceptor +import okhttp3.Response +import timber.log.Timber + +class LogInterceptor : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + // Log request + chain.request().let { + Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.INFO, "Type: ${it.method} URL: ${it.url}")) + it.headers.forEach { header -> + Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.HEADER, header.toString())) + } + Timber.d(toLogString(NetworkPetition.REQUEST, NetworkNode.BODY, it.body.toString())) + } + + val response = chain.proceed(chain.request()) + + // Log response + response.let { + Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.INFO, "RequestId: ${it.request.header(HttpConstants.OC_X_REQUEST_ID)}")) + Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.INFO, "Code: ${it.code} Message: ${it.message} IsSuccessful: ${it.isSuccessful}")) + it.headers.forEach { header -> + Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.HEADER, header.toString())) + } + Timber.d(toLogString(NetworkPetition.RESPONSE, NetworkNode.BODY, it.body.toString())) + } + + return response + } +}