PatchMethod.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2021 Timo Triebensky <timo@binsky.org>
  5. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. *
  8. * More information here: https://github.com/abeluck/android-streams-ipc
  9. */
  10. package com.nextcloud.android.sso;
  11. import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
  12. import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
  13. import org.apache.commons.httpclient.methods.PostMethod;
  14. import org.apache.commons.httpclient.methods.RequestEntity;
  15. import org.apache.commons.httpclient.util.EncodingUtil;
  16. import java.util.Vector;
  17. public class PatchMethod extends PostMethod {
  18. /**
  19. * The buffered request body consisting of <code>NameValuePair</code>s.
  20. */
  21. private Vector params = new Vector();
  22. /**
  23. * No-arg constructor.
  24. */
  25. public PatchMethod() {
  26. super();
  27. }
  28. /**
  29. * Constructor specifying a URI.
  30. *
  31. * @param uri either an absolute or relative URI
  32. */
  33. public PatchMethod(String uri) {
  34. super(uri);
  35. }
  36. /**
  37. * Returns <tt>"PATCH"</tt>.
  38. *
  39. * @return <tt>"PATCH"</tt>
  40. * @since 2.0
  41. */
  42. @Override
  43. public String getName() {
  44. return "PATCH";
  45. }
  46. /**
  47. * Returns <tt>true</tt> if there is a request body to be sent.
  48. *
  49. * @return boolean
  50. * @since 2.0beta1
  51. */
  52. protected boolean hasRequestContent() {
  53. if (!this.params.isEmpty()) {
  54. return true;
  55. } else {
  56. return super.hasRequestContent();
  57. }
  58. }
  59. /**
  60. * Clears request body.
  61. *
  62. * @since 2.0beta1
  63. */
  64. protected void clearRequestBody() {
  65. this.params.clear();
  66. super.clearRequestBody();
  67. }
  68. /**
  69. * Generates a request entity from the patch parameters, if present. Calls {@link
  70. * EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
  71. *
  72. * @since 3.0
  73. */
  74. protected RequestEntity generateRequestEntity() {
  75. if (!this.params.isEmpty()) {
  76. // Use a ByteArrayRequestEntity instead of a StringRequestEntity.
  77. // This is to avoid potential encoding issues. Form url encoded strings
  78. // are ASCII by definition but the content type may not be. Treating the content
  79. // as bytes allows us to keep the current charset without worrying about how
  80. // this charset will effect the encoding of the form url encoded string.
  81. String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
  82. return new ByteArrayRequestEntity(
  83. EncodingUtil.getAsciiBytes(content),
  84. FORM_URL_ENCODED_CONTENT_TYPE
  85. );
  86. } else {
  87. return super.generateRequestEntity();
  88. }
  89. }
  90. }