aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java6
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java11
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java7
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java13
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java6
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Response.java3
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java72
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java51
9 files changed, 20 insertions, 155 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java
index aeac79a61eb..a9f6b926613 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java
@@ -74,6 +74,10 @@ public class SourceService implements ServerComponent {
return deprecatedSourceDecorator.getSourceAsHtml(fileKey, from, to);
}
+ /**
+ * Raw lines of source file. Returns <code>null</code> if the file does not exist
+ */
+ @CheckForNull
public List<String> getLinesAsTxt(DbSession session, String fileKey) {
checkPermission(fileKey);
@@ -81,7 +85,7 @@ public class SourceService implements ServerComponent {
if (source != null) {
return newArrayList(Splitter.onPattern("\r?\n|\r").split(source));
}
- return Collections.emptyList();
+ return null;
}
@CheckForNull
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
index 4c4565e875b..17eb32ac7af 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
@@ -21,6 +21,8 @@
package org.sonar.server.source.ws;
import com.google.common.io.Resources;
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
@@ -30,6 +32,7 @@ import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.source.SourceService;
+import java.io.IOException;
import java.util.List;
public class RawAction implements RequestHandler {
@@ -63,10 +66,12 @@ public class RawAction implements RequestHandler {
try {
dbClient.componentDao().getByKey(session, fileKey);
List<String> lines = sourceService.getLinesAsTxt(session, fileKey);
- if (lines.isEmpty()) {
- throw new NotFoundException("File '" + fileKey + "' has no sources");
+ if (lines == null) {
+ throw new NotFoundException("File '" + fileKey + "' does not exist");
}
- response.newTxtWriter().values(lines).close();
+ IOUtils.writeLines(lines, "\n", response.stream().output(), Charsets.UTF_8);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to write raw source of file " + fileKey, e);
} finally {
session.close();
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java
index 161d753c2a5..e1907701971 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletResponse.java
@@ -21,7 +21,6 @@ package org.sonar.server.ws;
import org.sonar.api.server.ws.Response;
import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.utils.text.TxtWriter;
import org.sonar.api.utils.text.XmlWriter;
import org.sonar.server.plugins.MimeTypes;
@@ -89,12 +88,6 @@ public class ServletResponse implements Response {
}
@Override
- public TxtWriter newTxtWriter() {
- stream.setMediaType(MimeTypes.TXT);
- return TxtWriter.of(new OutputStreamWriter(stream.output()));
- }
-
- @Override
public ServletStream stream() {
return stream;
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
index ef632c3200f..b017e96d336 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
@@ -140,7 +140,7 @@ public class SourceServiceTest {
}
@Test
- public void get_txt_lines() throws Exception {
+ public void getLinesAsTxt() throws Exception {
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY);
when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn("line1\nline2");
@@ -150,13 +150,13 @@ public class SourceServiceTest {
}
@Test
- public void get_txt_lines_when_no_source() throws Exception {
+ public void getLinesAsTxt_returns_null_when_no_sources() throws Exception {
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY);
when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn(null);
List<String> result = service.getLinesAsTxt(session, COMPONENT_KEY);
- assertThat(result).isEmpty();
+ assertThat(result).isNull();
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
index fcac29ee4d4..158a4fb5791 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
@@ -81,26 +81,21 @@ public class RawActionTest {
WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey);
String result = request.execute().outputAsString();
- assertThat(result).isEqualTo("public class HelloWorld {\r\n}\r\n");
- }
-
- @Test(expected = NotFoundException.class)
- public void fail_to_get_txt_when_file_does_not_exists() throws Exception {
- WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", "unknown");
- request.execute();
+ assertThat(result).isEqualTo("public class HelloWorld {\n}\n");
}
+ @Test
public void fail_to_get_txt_when_no_source() throws Exception {
String fileKey = "src/Foo.java";
when(componentDao.getByKey(session, fileKey)).thenReturn(file);
- when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(Collections.<String>emptyList());
+ when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(null);
WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey);
try {
request.execute();
fail();
} catch (Exception e) {
- assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("");
+ assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("File 'src/Foo.java' does not exist");
}
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java b/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java
index 07d9fa3aac9..bae37674313 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java
@@ -27,7 +27,6 @@ import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.internal.ValidatingRequest;
import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.utils.text.TxtWriter;
import org.sonar.api.utils.text.XmlWriter;
import org.sonar.server.ws.WsTester.TestResponse.TestStream;
@@ -134,11 +133,6 @@ public class WsTester {
}
@Override
- public TxtWriter newTxtWriter() {
- return TxtWriter.of(new OutputStreamWriter(output, Charsets.UTF_8));
- }
-
- @Override
public Stream stream() {
if (stream == null) {
stream = new TestStream();
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 b192078a057..149ae4178c7 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,7 +20,6 @@
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;
@@ -42,8 +41,6 @@ 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
deleted file mode 100644
index e52f92a4ea2..00000000000
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/TxtWriter.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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
deleted file mode 100644
index dca31b0d855..00000000000
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/TxtWriterTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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();
- }
-
-}