diff --git a/app/build.gradle b/app/build.gradle index eeafa75fb6..6d4e53e4a4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -204,6 +204,9 @@ dependencies { //FTP implementation "commons-net:commons-net:$commonsNetVersion" + //OkHttp + implementation "com.squareup.okhttp3:okhttp:$okHttpVersion" + implementation "org.bouncycastle:bcpkix-jdk15on:$bouncyCastleVersion" implementation "org.bouncycastle:bcprov-jdk15on:$bouncyCastleVersion" diff --git a/app/src/main/java/com/amaze/filemanager/utils/smb/WsddDiscoverDeviceStrategy.kt b/app/src/main/java/com/amaze/filemanager/utils/smb/WsddDiscoverDeviceStrategy.kt index 33e1813c69..db5a1ab86f 100644 --- a/app/src/main/java/com/amaze/filemanager/utils/smb/WsddDiscoverDeviceStrategy.kt +++ b/app/src/main/java/com/amaze/filemanager/utils/smb/WsddDiscoverDeviceStrategy.kt @@ -23,12 +23,14 @@ package com.amaze.filemanager.utils.smb import androidx.annotation.VisibleForTesting import com.amaze.filemanager.R import com.amaze.filemanager.application.AppConfig +import com.amaze.filemanager.filesystem.ftp.NetCopyConnectionInfo.Companion.SLASH import com.amaze.filemanager.utils.ComputerParcelable import com.amaze.filemanager.utils.NetworkUtil -import com.android.volley.Response.ErrorListener -import com.android.volley.VolleyError -import com.android.volley.toolbox.StringRequest -import com.android.volley.toolbox.Volley +import okhttp3.Headers.Companion.toHeaders +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody import org.slf4j.Logger import org.slf4j.LoggerFactory import org.xmlpull.v1.XmlPullParser @@ -75,7 +77,6 @@ class WsddDiscoverDeviceStrategy : SmbDeviceScannerObservable.DiscoverDeviceStra private val wsdRequestHeaders = mutableMapOf( Pair("Accept-Encoding", "Identity"), - Pair("Content-Type", "application/soap+xml"), Pair("Connection", "Close"), Pair("User-Agent", "wsd") ) @@ -87,14 +88,10 @@ class WsddDiscoverDeviceStrategy : SmbDeviceScannerObservable.DiscoverDeviceStra @VisibleForTesting set - private val queue = Volley.newRequestQueue(AppConfig.getInstance()) + private val queue = OkHttpClient() private var cancelled = false - init { - queue.start() - } - override fun discoverDevices(callback: (ComputerParcelable) -> Unit) { multicastForDevice { addr -> callback.invoke(addr) @@ -181,21 +178,30 @@ class WsddDiscoverDeviceStrategy : SmbDeviceScannerObservable.DiscoverDeviceStra val endpoint = urn.substringAfter(URN_UUID) val dest = "http://${sourceAddress.hostAddress}:$TCP_PORT/$endpoint" - queue.add( - object : StringRequest( - Method.POST, - dest, - { resp -> + val requestBody = wsdRequestTemplate + .replace("##MESSAGE_ID##", "$URN_UUID$messageId") + .replace("##DEST_UUID##", urn) + .replace("##MY_UUID##", "$URN_UUID$tempDeviceId") + .toRequestBody("application/soap+xml".toMediaType()) + queue.newCall( + Request.Builder() + .url(dest) + .post(requestBody) + .headers(wsdRequestHeaders.toHeaders()) + .build() + ).execute().use { resp -> + if (resp.isSuccessful && resp.body != null) { + resp.body?.run { if (log.isTraceEnabled) log.trace("Response: $resp") val values = parseXmlForResponse( - resp, + this.string(), arrayOf(WSDP_TYPES, WSA_ADDRESS, PUB_COMPUTER) ) if (PUB_COMPUTER == values[WSDP_TYPES] && urn == values[WSA_ADDRESS]) { if (true == values[PUB_COMPUTER]?.isNotEmpty()) { val computerName: String = values[PUB_COMPUTER].let { - if (it!!.contains('/')) { - it.substringBefore("/") + if (it!!.contains(SLASH)) { + it.substringBefore(SLASH) } else { it } @@ -205,29 +211,16 @@ class WsddDiscoverDeviceStrategy : SmbDeviceScannerObservable.DiscoverDeviceStra ) } } - }, - object : ErrorListener { - override fun onErrorResponse(error: VolleyError?) { - log.error("Error querying endpoint", error) - } } - ) { - override fun getBody(): ByteArray { - return wsdRequestTemplate - .replace("##MESSAGE_ID##", "$URN_UUID$messageId") - .replace("##DEST_UUID##", urn) - .replace("##MY_UUID##", "$URN_UUID$tempDeviceId") - .toByteArray(Charsets.UTF_8) - } - override fun getHeaders(): MutableMap = wsdRequestHeaders + } else { + log.error("Error querying endpoint", resp) } - ) + } } } override fun onCancel() { cancelled = true - queue.stop() } private fun parseXmlForResponse(xml: ByteArray, tags: Array) = diff --git a/build.gradle b/build.gradle index 3fd0ef3566..3d018b2808 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,7 @@ buildscript { jsoupVersion = "1.13.1" rxAndroidVersion = "2.1.1" rxJavaVersion = "2.2.9" + okHttpVersion = "4.9.0" } repositories { google() diff --git a/testShared/src/test/java/com/amaze/filemanager/test/volley/MockHttpStack.kt b/testShared/src/test/java/com/amaze/filemanager/test/volley/MockHttpStack.kt deleted file mode 100644 index f859a35b43..0000000000 --- a/testShared/src/test/java/com/amaze/filemanager/test/volley/MockHttpStack.kt +++ /dev/null @@ -1,51 +0,0 @@ -package com.amaze.filemanager.test.volley - -import com.android.volley.AuthFailureError -import com.android.volley.Request -import com.android.volley.toolbox.BaseHttpStack -import com.android.volley.toolbox.HttpResponse -import java.io.IOException - -/** - * Mock [BaseHttpStack] for test only. - */ -class MockHttpStack : BaseHttpStack() { - - private lateinit var mResponseToReturn: HttpResponse - private lateinit var lastUrl: String - private lateinit var mLastHeaders: MutableMap - private var lastPostBody: ByteArray? = null - - /** - * get headers in last request - */ - fun getLastHeaders() = mLastHeaders - - /** - * Manually set response to return - */ - fun setResponseToReturn(response: HttpResponse) { - mResponseToReturn = response - } - - @Throws(IOException::class, AuthFailureError::class) - override fun executeRequest( - request: Request<*>, - additionalHeaders: Map? - ): HttpResponse { - lastUrl = request.url - mLastHeaders = HashMap() - if (request.headers != null) { - mLastHeaders.putAll(request.headers) - } - if (additionalHeaders != null) { - mLastHeaders.putAll(additionalHeaders) - } - try { - lastPostBody = request.body - } catch (e: AuthFailureError) { - lastPostBody = null - } - return mResponseToReturn - } -}