diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-08-23 17:18:08 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-09-13 15:50:49 +0200 |
commit | 9d5fc95ed97f0dc0e1a797065b5a3870449a9ded (patch) | |
tree | 91126ec5e0985e48887a078ecf757737bca88616 /sonar-plugin-api | |
parent | 5b9d6018431f09d090b71fa3179a9d9bed5dc0ab (diff) | |
download | sonarqube-9d5fc95ed97f0dc0e1a797065b5a3870449a9ded.tar.gz sonarqube-9d5fc95ed97f0dc0e1a797065b5a3870449a9ded.zip |
SONAR-9740 support HTTP headers in API Request and sonar-ws
Diffstat (limited to 'sonar-plugin-api')
5 files changed, 57 insertions, 1 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/LocalConnector.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/LocalConnector.java index a6cdc79c098..7e12c0fd141 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/LocalConnector.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/LocalConnector.java @@ -21,6 +21,7 @@ package org.sonar.api.server.ws; import java.util.Collection; import java.util.List; +import java.util.Optional; import javax.annotation.CheckForNull; /** @@ -92,6 +93,12 @@ public interface LocalConnector { * @see Request#multiParam(String) */ List<String> getMultiParam(String key); + + /** + * @see Request#header(String) + * @since 6.6 + */ + Optional<String> getHeader(String name); } interface LocalResponse { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java index 2428fc441f5..2a210e519e2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Supplier; @@ -302,6 +303,14 @@ public abstract class Request { } /** + * Optional value of the HTTP header with specified name. + * If present, the result can have an empty string value ({@code ""}). + * + * @since 6.6 + */ + public abstract Optional<String> header(String name); + + /** * Allows a web service to call another web service. * @see LocalConnector * @since 5.5 diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java index ce65867be76..323d7a98872 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; @@ -44,6 +45,7 @@ public class SimpleGetRequest extends Request { private final Map<String, String> params = new HashMap<>(); private final Map<String, Part> parts = new HashMap<>(); + private final Map<String, String> headers = new HashMap<>(); private String mediaType = "application/json"; private String path; @@ -130,4 +132,13 @@ public class SimpleGetRequest extends Request { return this; } + @Override + public Optional<String> header(String name) { + return Optional.ofNullable(headers.get(name)); + } + + public SimpleGetRequest setHeader(String name, String value) { + headers.put(name, value); + return this; + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java index b940b953d08..d9395d5b598 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java @@ -29,6 +29,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; @@ -469,7 +470,7 @@ public class RequestTest { @DataProvider public static Object[][] date_times() { - return new Object[][] { + return new Object[][]{ {"2014-05-27", parseDate("2014-05-27")}, {"2014-05-27T15:50:45+0100", parseDateTime("2014-05-27T15:50:45+0100")}, {null, null} @@ -584,6 +585,7 @@ public class RequestTest { private final ListMultimap<String, String> multiParams = ArrayListMultimap.create(); private final Map<String, String> params = new HashMap<>(); private final Map<String, Part> parts = new HashMap<>(); + private final Map<String, String> headers = new HashMap<>(); @Override public String method() { @@ -644,6 +646,16 @@ public class RequestTest { parts.put(key, new PartImpl(input, fileName)); return this; } + + @Override + public Optional<String> header(String name) { + return Optional.ofNullable(headers.get(name)); + } + + public FakeRequest setHeader(String name, String value) { + headers.put(name, value); + return this; + } } private static class FakeWs implements WebService { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java index d048eb6351b..15a73dbf396 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/internal/SimpleGetRequestTest.java @@ -88,4 +88,21 @@ public class SimpleGetRequestTest { assertThat(underTest.getParams()).containsOnly(entry("foo", "bar"), entry("fee", "beer")); } + + @Test + public void header_returns_empty_if_header_is_not_present() { + assertThat(underTest.header("foo")).isEmpty(); + } + + @Test + public void header_returns_value_of_header_if_present() { + underTest.setHeader("foo", "bar"); + assertThat(underTest.header("foo")).hasValue("bar"); + } + + @Test + public void header_returns_empty_string_value_if_header_is_present_without_value() { + underTest.setHeader("foo", ""); + assertThat(underTest.header("foo")).hasValue(""); + } } |