HttpStreamFetcher.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 android.accounts.Account;
  25. import com.bumptech.glide.Priority;
  26. import com.bumptech.glide.load.data.DataFetcher;
  27. import com.nextcloud.client.account.CurrentAccountProvider;
  28. import com.nextcloud.client.account.User;
  29. import com.nextcloud.client.network.ClientFactory;
  30. import com.owncloud.android.MainApp;
  31. import com.owncloud.android.lib.common.OwnCloudAccount;
  32. import com.owncloud.android.lib.common.OwnCloudClient;
  33. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  34. import com.owncloud.android.lib.common.operations.RemoteOperation;
  35. import com.owncloud.android.lib.common.utils.Log_OC;
  36. import org.apache.commons.httpclient.HttpStatus;
  37. import org.apache.commons.httpclient.methods.GetMethod;
  38. import java.io.InputStream;
  39. /**
  40. * Fetcher with OwnCloudClient
  41. */
  42. public class HttpStreamFetcher implements DataFetcher<InputStream> {
  43. private static final String TAG = HttpStreamFetcher.class.getName();
  44. private final String url;
  45. private final CurrentAccountProvider currentAccount;
  46. private final ClientFactory clientFactory;
  47. HttpStreamFetcher(final CurrentAccountProvider currentAccount, ClientFactory clientFactory, final String url) {
  48. this.currentAccount = currentAccount;
  49. this.clientFactory = clientFactory;
  50. this.url = url;
  51. }
  52. @Override
  53. public InputStream loadData(Priority priority) throws Exception {
  54. User user = currentAccount.getUser();
  55. OwnCloudClient client = clientFactory.create(user);
  56. if (client != null) {
  57. GetMethod get;
  58. try {
  59. get = new GetMethod(url);
  60. get.setRequestHeader("Cookie", "nc_sameSiteCookielax=true;nc_sameSiteCookiestrict=true");
  61. get.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);
  62. int status = client.executeMethod(get);
  63. if (status == HttpStatus.SC_OK) {
  64. return get.getResponseBodyAsStream();
  65. } else {
  66. client.exhaustResponse(get.getResponseBodyAsStream());
  67. }
  68. } catch (Exception e) {
  69. Log_OC.e(TAG, e.getMessage(), e);
  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 url;
  81. }
  82. @Override
  83. public void cancel() {
  84. Log_OC.i(TAG,"Cancel");
  85. }
  86. }