HttpStreamFetcher.kt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Alejandro Bautista
  5. * @author Chris Narkiewicz
  6. *
  7. * Copyright (C) 2017 Alejandro Bautista
  8. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.utils.glide
  24. import com.bumptech.glide.Priority
  25. import com.bumptech.glide.load.data.DataFetcher
  26. import com.nextcloud.client.account.CurrentAccountProvider
  27. import com.nextcloud.client.network.ClientFactory
  28. import com.owncloud.android.lib.common.operations.RemoteOperation
  29. import com.owncloud.android.lib.common.utils.Log_OC
  30. import org.apache.commons.httpclient.HttpStatus
  31. import org.apache.commons.httpclient.methods.GetMethod
  32. import java.io.ByteArrayInputStream
  33. import java.io.ByteArrayOutputStream
  34. import java.io.InputStream
  35. /**
  36. * Fetcher with OwnCloudClient
  37. */
  38. @Suppress("TooGenericExceptionCaught")
  39. class HttpStreamFetcher internal constructor(
  40. private val currentAccount: CurrentAccountProvider,
  41. private val clientFactory: ClientFactory,
  42. private val url: String
  43. ) : DataFetcher<InputStream?> {
  44. @Throws(Exception::class)
  45. override fun loadData(priority: Priority): InputStream? {
  46. val user = currentAccount.user
  47. val client = clientFactory.create(user)
  48. if (client != null) {
  49. var get: GetMethod? = null
  50. try {
  51. get = GetMethod(url)
  52. get.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true")
  53. get.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE)
  54. val status = client.executeMethod(get)
  55. if (status == HttpStatus.SC_OK) {
  56. val byteOutputStream = ByteArrayOutputStream()
  57. get.responseBodyAsStream.use { input ->
  58. byteOutputStream.use { output ->
  59. input.copyTo(output)
  60. }
  61. }
  62. return ByteArrayInputStream(byteOutputStream.toByteArray())
  63. } else {
  64. client.exhaustResponse(get.responseBodyAsStream)
  65. }
  66. } catch (e: Exception) {
  67. Log_OC.e(TAG, e.message, e)
  68. } finally {
  69. get?.releaseConnection()
  70. }
  71. }
  72. return null
  73. }
  74. override fun cleanup() {
  75. Log_OC.i(TAG, "Cleanup")
  76. }
  77. override fun getId(): String {
  78. return url
  79. }
  80. override fun cancel() {
  81. Log_OC.i(TAG, "Cancel")
  82. }
  83. companion object {
  84. private val TAG = HttpStreamFetcher::class.java.name
  85. }
  86. }