Let's keep API simple.
return downloadPlainText(uri, charset.name());
}
- @Override
- InputStream openStream(URI uri) {
- try {
- HttpURLConnection connection = newHttpConnection(uri);
- return connection.getInputStream();
-
- } catch (Exception e) {
- throw new SonarException("Fail to download the file: " + uri + " (" + getProxySynthesis(uri) + ")", e);
- }
- }
-
private HttpURLConnection newHttpConnection(URI uri) throws IOException {
LoggerFactory.getLogger(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri) + ")");
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
return searchForSupportedProcessor(uri).readString(uri, charset);
}
- /**
- * Opens an input stream over the given uri.
- */
- public InputStream openStream(URI uri) {
- return searchForSupportedProcessor(uri).openStream(uri);
- }
-
/**
* Returns a detailed description of the given uri. For example HTTP URIs are completed
* with the configured HTTP proxy.
abstract String readString(URI uri, Charset charset);
- abstract InputStream openStream(URI uri);
-
abstract String description(URI uri);
}
}
}
- @Override
- InputStream openStream(URI uri) {
- try {
- return Files.newInputStreamSupplier(new File(uri)).getInput();
- } catch (IOException e) {
- throw Throwables.propagate(e);
- }
- }
-
@Override
String description(URI uri) {
return new File(uri).getAbsolutePath();
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.net.*;
import java.util.Arrays;
import java.util.Properties;
assertThat(text.length()).isGreaterThan(10);
}
- @Test
- public void openStream() throws Exception {
- InputStream input = new HttpDownloader(new Settings()).openStream(new URI(baseUrl));
- assertThat(IOUtils.toByteArray(input).length).isGreaterThan(10);
- IOUtils.closeQuietly(input);
- }
-
@Test(expected = SonarException.class)
public void failIfServerDown() throws Exception {
// I hope that the port 1 is not used !
assertThat(new String(uriReader.readBytes(testFile))).isEqualTo("in foo");
}
- @Test
- public void file_openStream() throws Exception {
- UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
- InputStream input = uriReader.openStream(testFile);
- assertThat(IOUtils.toString(input)).isEqualTo("in foo");
- IOUtils.closeQuietly(input);
- }
-
@Test
public void file_readString_fails_if_file_not_found() throws Exception {
thrown.expect(RuntimeException.class);
uriReader.readBytes(new URI("file:/notfound"));
}
- @Test
- public void file_openStream_fails_if_file_not_found() throws Exception {
- thrown.expect(RuntimeException.class);
- UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
- uriReader.openStream(new URI("file:/notfound"));
- }
-
@Test
public void file_description() {
UriReader uriReader = new UriReader(new UriReader.SchemeProcessor[0]);
*/
package org.sonar.server.plugins;
+import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.Properties;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.UpdateCenterDeserializer;
-import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
}
private UpdateCenter init() {
- InputStream input = null;
+ Reader reader = null;
try {
- input = uriReader.openStream(uri);
- if (input != null) {
- java.util.Properties properties = new java.util.Properties();
- properties.load(input);
- return UpdateCenterDeserializer.fromProperties(properties);
- }
+ String content = uriReader.readString(uri, Charsets.UTF_8);
+ java.util.Properties properties = new java.util.Properties();
+ reader = new StringReader(content);
+ properties.load(reader);
+ return UpdateCenterDeserializer.fromProperties(properties);
+
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).error("Fail to connect to update center", e);
+ return null;
} finally {
- IOUtils.closeQuietly(input);
+ IOUtils.closeQuietly(reader);
}
- return null;
}
}
*/
package org.sonar.server.plugins;
-import org.apache.commons.io.IOUtils;
+import com.google.common.base.Charsets;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.config.Settings;
@Test
public void downloadUpdateCenter() throws URISyntaxException {
- when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3"));
+ when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3");
UpdateCenter center = client.getCenter();
- verify(reader, times(1)).openStream(new URI(BASE_URL));
+ verify(reader, times(1)).readString(new URI(BASE_URL), Charsets.UTF_8);
assertThat(center.getSonar().getVersions()).containsOnly(Version.create("2.2"), Version.create("2.3"));
assertThat(client.getLastRefreshDate()).isNotNull();
}
@Test
- public void not_available_before_initialization() {
- assertThat(client.getLastRefreshDate()).isNull();
- }
+ public void not_available_before_initialization() {
+ assertThat(client.getLastRefreshDate()).isNull();
+ }
@Test
public void ignore_connection_errors() {
- when(reader.openStream(any(URI.class))).thenThrow(new SonarException());
+ when(reader.readString(any(URI.class), eq(Charsets.UTF_8))).thenThrow(new SonarException());
assertThat(client.getCenter()).isNull();
}
@Test
public void cache_data() throws Exception {
- when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3"));
+ when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3");
client.getCenter();
client.getCenter();
- verify(reader, times(1)).openStream(new URI(BASE_URL));
+ verify(reader, times(1)).readString(new URI(BASE_URL), Charsets.UTF_8);
}
@Test
public void forceRefresh() throws Exception {
- when(reader.openStream(new URI(BASE_URL))).thenReturn(IOUtils.toInputStream("sonar.versions=2.2,2.3"));
+ when(reader.readString(new URI(BASE_URL), Charsets.UTF_8)).thenReturn("sonar.versions=2.2,2.3");
client.getCenter();
client.getCenter(true);
- verify(reader, times(2)).openStream(new URI(BASE_URL));
+ verify(reader, times(2)).readString(new URI(BASE_URL), Charsets.UTF_8);
}
}
\ No newline at end of file