Commit f5df95af authored by j.hoerdt's avatar j.hoerdt
Browse files

make more improvements suggested by sonarlint

parent e3ed3f15
......@@ -7,6 +7,7 @@ import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
......@@ -32,16 +33,18 @@ import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Util {
private Util() {}
private static int timeout_millis = 120000;
public static URLConnection create_timed_connection(String uri) throws MalformedURLException, IOException {
public static URLConnection create_timed_connection(String uri) throws IOException {
URLConnection connection = URI.create(uri).toURL().openConnection();
connection.setConnectTimeout(timeout_millis);
connection.setReadTimeout(timeout_millis);
return connection;
}
public static BufferedReader reader_of_uri(String uri) throws MalformedURLException, IOException {
public static BufferedReader reader_of_uri(String uri) throws IOException {
return new BufferedReader(new InputStreamReader(create_timed_connection(uri).getInputStream()));
}
......@@ -50,7 +53,16 @@ public class Util {
}
public static DocumentBuilder create_quiet_document_builder() throws ParserConfigurationException {
DocumentBuilder document_builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
var document_builder_factory = DocumentBuilderFactory.newInstance();
// ensure that no external entities can be loaded, see https://rules.sonarsource.com/java/RSPEC-2755
// OSM might try to pwn us, who knows.
document_builder_factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
document_builder_factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
document_builder_factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
document_builder_factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
document_builder_factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
var document_builder = document_builder_factory.newDocumentBuilder();
document_builder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException arg0) {}
......@@ -120,14 +132,9 @@ public class Util {
case 19:
return Instant.ofEpochSecond(0, as_long);
}
throw new DateTimeException("what kind of timestamp is this supposed to be?: " + str);
}
/*
public static void main(String[] args) throws MalformedURLException, IOException {
Util.reader_of_uri("http://archive.sensor.community/2020-03-28/").lines().forEach(System.out::println);
default:
throw new DateTimeException("what kind of timestamp is this supposed to be?: " + str);
}
}
*/
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment