-
Notifications
You must be signed in to change notification settings - Fork 94
feat: client interceptor #1812
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
Open
alperozturk96
wants to merge
4
commits into
master
Choose a base branch
from
feat/add-interceptor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: client interceptor #1812
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
library/src/main/java/com/owncloud/android/lib/common/interceptor/ClientInterceptor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * Nextcloud Android Library | ||
| * | ||
| * SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]> | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| package com.owncloud.android.lib.common.interceptor | ||
|
|
||
| import com.nextcloud.common.OkHttpMethodBase | ||
| import com.owncloud.android.lib.common.utils.Log_OC | ||
| import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormat | ||
| import com.owncloud.android.lib.common.utils.responseFormat.ResponseFormatDetector | ||
| import okhttp3.Request | ||
| import okhttp3.Response | ||
| import org.apache.commons.httpclient.HttpMethod | ||
| import org.apache.commons.httpclient.HttpMethodBase | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import org.xml.sax.InputSource | ||
| import java.io.StringReader | ||
| import java.io.StringWriter | ||
| import javax.xml.parsers.DocumentBuilderFactory | ||
| import javax.xml.transform.OutputKeys | ||
| import javax.xml.transform.Transformer | ||
| import javax.xml.transform.TransformerFactory | ||
| import javax.xml.transform.dom.DOMSource | ||
| import javax.xml.transform.stream.StreamResult | ||
| import kotlin.collections.component1 | ||
| import kotlin.collections.component2 | ||
|
|
||
| @Suppress("TooManyFunctions") | ||
| class ClientInterceptor { | ||
| companion object { | ||
| private const val TAG = "ClientInterceptor" | ||
| } | ||
|
|
||
| fun interceptHttpMethodBaseRequest(method: HttpMethodBase) { | ||
| Log_OC.d(TAG, "➡️ Method: ${method.name} 🌐 URL: ${method.uri}") | ||
| logHeaders(method.requestHeaders.map { it.name to it.value }) | ||
|
|
||
| if (method is org.apache.commons.httpclient.methods.EntityEnclosingMethod) { | ||
| val buffer = java.io.ByteArrayOutputStream() | ||
| method.requestEntity?.writeRequest(buffer) | ||
| val body = buffer.toString(method.requestCharSet ?: Charsets.UTF_8.name()) | ||
| logBody(body, method.getRequestHeader("Content-Type")?.value, "Request") | ||
| } | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptHttpMethodBaseResponse( | ||
| method: HttpMethodBase, | ||
| statusCode: Int | ||
| ) { | ||
| Log_OC.d(TAG, "⬅️ Status Code: $statusCode") | ||
| logHeaders(method.responseHeaders.map { it.name to it.value }) | ||
| logBody(method.responseBodyAsString, method.getResponseHeader("Content-Type")?.value, "Response") | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptHttpMethodRequest(method: HttpMethod) { | ||
| Log_OC.d(TAG, "➡️ Method: ${method.name} 🌐 URL: ${method.uri}") | ||
| logHeaders(method.requestHeaders.map { it.name to it.value }) | ||
|
|
||
| if (method is org.apache.commons.httpclient.methods.EntityEnclosingMethod) { | ||
| val buffer = java.io.ByteArrayOutputStream() | ||
| method.requestEntity?.writeRequest(buffer) | ||
| val body = buffer.toString(method.requestCharSet ?: Charsets.UTF_8.name()) | ||
| logBody(body, method.getRequestHeader("Content-Type")?.value, "Request") | ||
| } | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptHttpMethodResponse( | ||
| method: HttpMethod, | ||
| statusCode: Int | ||
| ) { | ||
| Log_OC.d(TAG, "⬅️ Status Code: $statusCode") | ||
| logHeaders(method.responseHeaders.map { it.name to it.value }) | ||
| logBody(method.responseBodyAsString, method.getResponseHeader("Content-Type")?.value, "Response") | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptOkHttp3Request(request: Request) { | ||
| Log_OC.d(TAG, "➡️ Method: ${request.method} 🌐 URL: ${request.url}") | ||
| request.headers?.toMultimap()?.let { headerMap -> | ||
| logHeaders(headerMap.flatMap { (k, vList) -> vList.map { k to it } }) | ||
| } | ||
| request.body?.let { | ||
| val buffer = okio.Buffer() | ||
| it.writeTo(buffer) | ||
| logBody(buffer.readUtf8(), it.contentType()?.toString(), "Request") | ||
| } | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptOkHttp3Response(response: Response) { | ||
| Log_OC.d(TAG, "⬅️ Status: ${response.code}") | ||
| response.headers?.toMultimap()?.let { headerMap -> | ||
| logHeaders(headerMap.flatMap { (k, vList) -> vList.map { k to it } }) | ||
| } | ||
|
|
||
| val body = | ||
| try { | ||
| response.peekBody(Long.MAX_VALUE) | ||
| } catch (_: Exception) { | ||
| null | ||
| } | ||
|
|
||
| body?.string()?.let { bodyString -> | ||
| logBody(bodyString, response.body?.contentType()?.toString(), "Response") | ||
| } | ||
|
|
||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptOkHttpMethodBaseRequest(method: OkHttpMethodBase) { | ||
| Log_OC.d(TAG, "➡️ Method: ${method.javaClass.simpleName} 🌐 URL: ${method.uri}") | ||
| logHeaders(method.requestHeaders.map { it.key to it.value }) | ||
| logBody(method.getRequestBodyAsString(), method.getRequestHeader("Content-Type"), "Request") | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| fun interceptOkHttpMethodBaseResponse( | ||
| method: OkHttpMethodBase, | ||
| statusCode: Int | ||
| ) { | ||
| Log_OC.d(TAG, "⬅️ Status Code: $statusCode") | ||
| logHeaders(method.getResponseHeaders().toMultimap().flatMap { (k, vList) -> vList.map { k to it } }) | ||
| logBody(method.getResponseBodyAsString(), method.getResponseHeader("Content-Type"), "Response") | ||
| Log_OC.d(TAG, "-------------------------") | ||
| } | ||
|
|
||
| // region Private Methods | ||
| private val xmlDocBuilder = | ||
| DocumentBuilderFactory | ||
| .newInstance() | ||
| .apply { | ||
| isNamespaceAware = true | ||
| isIgnoringComments = true | ||
| isIgnoringElementContentWhitespace = true | ||
| }.newDocumentBuilder() | ||
|
|
||
| private val threadLocalTransformer = ThreadLocal<Transformer>() | ||
|
|
||
| private fun getTransformer(): Transformer { | ||
| var transformer = threadLocalTransformer.get() | ||
| if (transformer == null) { | ||
| transformer = | ||
| TransformerFactory.newInstance().newTransformer().apply { | ||
| setOutputProperty(OutputKeys.INDENT, "yes") | ||
| setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") | ||
| setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no") | ||
| setOutputProperty(OutputKeys.ENCODING, "UTF-8") | ||
| } | ||
| threadLocalTransformer.set(transformer) | ||
| } | ||
| return transformer | ||
| } | ||
|
|
||
| private fun formatXml(xml: String): String = | ||
| try { | ||
| val characterStream = StringReader(xml) | ||
| val inputSource = InputSource(characterStream) | ||
| val doc = xmlDocBuilder.parse(inputSource) | ||
| val writer = StringWriter() | ||
| val domSource = DOMSource(doc) | ||
| val streamResult = StreamResult(writer) | ||
| getTransformer().transform(domSource, streamResult) | ||
| writer.toString() | ||
| } catch (_: Exception) { | ||
| xml | ||
| } | ||
|
|
||
| private fun formatJson( | ||
| json: String, | ||
| indent: Int = 2 | ||
| ): String = | ||
| try { | ||
| val trimmed = json.trim() | ||
| when { | ||
| ResponseFormatDetector.isJsonObject(trimmed) -> JSONObject(trimmed).toString(indent) | ||
| ResponseFormatDetector.isJsonArray(trimmed) -> JSONArray(trimmed).toString(indent) | ||
| else -> json | ||
| } | ||
| } catch (_: Exception) { | ||
| json | ||
| } | ||
|
|
||
| private fun formatBody( | ||
| body: String, | ||
| contentType: String | ||
| ): String { | ||
| val bodyFormat = ResponseFormatDetector.detectFormat(body) | ||
|
|
||
| return when { | ||
| contentType.contains("xml", true) || bodyFormat == ResponseFormat.XML -> formatXml(body) | ||
| contentType.contains("json", true) || bodyFormat == ResponseFormat.JSON -> formatJson(body) | ||
| else -> body | ||
| } | ||
| } | ||
|
|
||
| private fun isValidContentType(contentType: String): Boolean = | ||
| contentType.contains("application/json") || | ||
| contentType.contains("text") || | ||
| contentType.contains("xml") || | ||
| contentType.isEmpty() | ||
|
|
||
| private fun logHeaders(headers: Iterable<Pair<String, String>>) { | ||
| headers.forEach { (name, value) -> Log_OC.d(TAG, "📑 Header: $name: $value") } | ||
| } | ||
|
|
||
| @Suppress("TooGenericExceptionCaught") | ||
| private fun logBody( | ||
| body: String?, | ||
| contentType: String?, | ||
| label: String | ||
| ) { | ||
| if (!body.isNullOrBlank() && isValidContentType(contentType ?: "")) { | ||
| try { | ||
| val formatted = formatBody(body, contentType ?: "") | ||
| Log_OC.d(TAG, "📦 $label Body:\n$formatted") | ||
| } catch (e: Exception) { | ||
| Log_OC.w(TAG, "⚠️ Error reading $label body: $e") | ||
| } | ||
| } | ||
| } | ||
| // endregion | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tobiasKaminsky
How can I completely bypass the interception process? Currently,
LogOCnot going to print anything in prod, but I would also like to skip the parsing step altogether. When I checked theisEnabledvalue inLogOC, it returned false during my local tests, even though it should be enabled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like that?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases (such as beta or QA versions), even in release mode, we want to track logs to detect issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems Log_OC inside libray is not started.
I will check it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea: move files' logger to library logging