diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-29 12:41:51 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-29 12:59:31 +0100 |
commit | fcbc15aefa3ff4a291986c89c2ec7b10ce3c8290 (patch) | |
tree | b91c74c5a13668f5f57f34d03498ac7c9547c384 /sonar-plugin-api | |
parent | 3aad958a181266d3bbb3ff3c1a177907baa5b893 (diff) | |
download | sonarqube-fcbc15aefa3ff4a291986c89c2ec7b10ce3c8290.tar.gz sonarqube-fcbc15aefa3ff4a291986c89c2ec7b10ce3c8290.zip |
SONAR-5292 Provide a new "/api/sources/raw" WS
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 126 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java index 149ae4178c7..b192078a057 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java @@ -20,6 +20,7 @@ package org.sonar.api.server.ws; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.api.utils.text.TxtWriter; import org.sonar.api.utils.text.XmlWriter; import java.io.OutputStream; @@ -41,6 +42,8 @@ public interface Response { XmlWriter newXmlWriter(); + TxtWriter newTxtWriter(); + Response noContent(); Stream stream(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java new file mode 100644 index 00000000000..e52f92a4ea2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java @@ -0,0 +1,72 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.api.utils.text; + +import java.io.IOException; +import java.io.Writer; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +/** + * @since 5.0 + */ +public class TxtWriter { + + private final Writer writer; + private static final String LINE_SEPARATOR = "\r\n"; + + private TxtWriter(Writer writer) { + this.writer = writer; + } + + public static TxtWriter of(Writer writer) { + return new TxtWriter(writer); + } + + public TxtWriter values(List<String> values) { + for (String value : values) { + write(value); + write(LINE_SEPARATOR); + } + return this; + } + + public TxtWriter values(String... values) { + return values(newArrayList(values)); + } + + public void close() { + try { + writer.close(); + } catch (IOException e) { + throw new WriterException("Fail to close TXT output", e); + } + } + + private void write(String s) { + try { + writer.append(s); + } catch (IOException e) { + throw new WriterException("Fail to generate TXT with value: " + s, e); + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java new file mode 100644 index 00000000000..dca31b0d855 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.api.utils.text; + +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.StringReader; +import java.io.StringWriter; + +import static org.fest.assertions.Assertions.assertThat; + +public class TxtWriterTest { + + @Test + public void write_txt() throws Exception { + StringWriter output = new StringWriter(); + TxtWriter writer = TxtWriter.of(output); + + writer.values("France", "Paris"); + writer.close(); + + BufferedReader reader = new BufferedReader(new StringReader(output.toString())); + String line1 = reader.readLine(); + assertThat(line1).isEqualTo("France"); + + String line2 = reader.readLine(); + assertThat(line2).isEqualTo("Paris"); + + assertThat(reader.readLine()).isNull(); + } + +} |