Response.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2019 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. package com.nextcloud.android.sso;
  9. import com.google.gson.Gson;
  10. import org.apache.commons.httpclient.Header;
  11. import org.apache.commons.httpclient.HttpMethodBase;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class Response {
  17. private InputStream body;
  18. private Header[] headers;
  19. private HttpMethodBase method;
  20. public Response() {
  21. headers = new Header[0];
  22. body = new InputStream() {
  23. @Override
  24. public int read() {
  25. return 0;
  26. }
  27. };
  28. }
  29. public Response(HttpMethodBase methodBase) throws IOException {
  30. this.method = methodBase;
  31. this.body = methodBase.getResponseBodyAsStream();
  32. this.headers = methodBase.getResponseHeaders();
  33. }
  34. public String getPlainHeadersString() {
  35. List<PlainHeader> arrayList = new ArrayList<>(headers.length);
  36. for (Header header : headers) {
  37. arrayList.add(new PlainHeader(header.getName(), header.getValue()));
  38. }
  39. Gson gson = new Gson();
  40. return gson.toJson(arrayList);
  41. }
  42. public InputStream getBody() {
  43. return this.body;
  44. }
  45. public HttpMethodBase getMethod() {
  46. return method;
  47. }
  48. }