HttpStreamFetcher.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Alejandro Bautista
  5. * Copyright (C) 2017 Alejandro Bautista
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.utils.glide;
  21. import android.accounts.Account;
  22. import com.bumptech.glide.Priority;
  23. import com.bumptech.glide.load.data.DataFetcher;
  24. import com.owncloud.android.MainApp;
  25. import com.owncloud.android.authentication.AccountUtils;
  26. import com.owncloud.android.lib.common.OwnCloudAccount;
  27. import com.owncloud.android.lib.common.OwnCloudClient;
  28. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  29. import com.owncloud.android.lib.common.operations.RemoteOperation;
  30. import com.owncloud.android.lib.common.utils.Log_OC;
  31. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  32. import org.apache.commons.httpclient.HttpStatus;
  33. import org.apache.commons.httpclient.methods.GetMethod;
  34. import java.io.InputStream;
  35. /**
  36. * Fetcher with OwnCloudClient
  37. */
  38. public class HttpStreamFetcher implements DataFetcher<InputStream> {
  39. private static final String TAG = HttpStreamFetcher.class.getName();
  40. private final String mURL;
  41. public HttpStreamFetcher(String url) {
  42. this.mURL = url;
  43. }
  44. @Override
  45. public InputStream loadData(Priority priority) throws Exception {
  46. Account mAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
  47. OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount,
  48. MainApp.getAppContext());
  49. OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
  50. getClientFor(ocAccount, MainApp.getAppContext());
  51. OwnCloudVersion serverOCVersion = AccountUtils.getServerVersion(mAccount);
  52. if (mClient != null && serverOCVersion != null) {
  53. if (serverOCVersion.supportsRemoteThumbnails()) {
  54. GetMethod get = null;
  55. try {
  56. get = new GetMethod(mURL);
  57. get.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true");
  58. get.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);
  59. int status = mClient.executeMethod(get);
  60. if (status == HttpStatus.SC_OK) {
  61. return get.getResponseBodyAsStream();
  62. } else {
  63. mClient.exhaustResponse(get.getResponseBodyAsStream());
  64. }
  65. } catch (Exception e) {
  66. Log_OC.d(TAG, e.getMessage(), e);
  67. }
  68. } else {
  69. Log_OC.d(TAG, "Server too old");
  70. }
  71. }
  72. return null;
  73. }
  74. @Override
  75. public void cleanup() {
  76. Log_OC.i(TAG,"Cleanup");
  77. }
  78. @Override
  79. public String getId() {
  80. return mURL;
  81. }
  82. @Override
  83. public void cancel() {
  84. Log_OC.i(TAG,"Cancel");
  85. }
  86. }