.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != 200) {
throw new AssetManagerRuntimeException("Error code '"
+ responseCode + "' when requesting to '" + url + "'");
}
try (BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream()));) {
String currentLine = null;
List<Map<String, String>> csvContents = new LinkedList<Map<String, String>>();
if ((currentLine = bufferReader.readLine()) != null) {
String[] headers = currentLine.split(",");
List<String> actualHeadersInCVS = new ArrayList<String>(
Arrays.asList(headers));
if (!actualHeadersInCVS.equals(HEADERS)) {
throw new RuntimeException(
String.format(
"CVS header %s is not consistent with the expected list of headers %s",
currentLine, HEADERS.toString()));
}
}
while ((currentLine = bufferReader.readLine()) != null) {
Map<String, String> fieldToValueMap = new HashMap<String, String>();
String[] valuesInLine = currentLine.split(",");
for (int valueIndex = 0; valueIndex < valuesInLine.length; valueIndex++) {
fieldToValueMap.put(HEADERS.get(valueIndex),
valuesInLine[valueIndex]);
}
csvContents.add(fieldToValueMap);
}
return csvContents;
}
} catch (IOException ioException) {
throw new AssetManagerRuntimeException(
"Error downloading stocks for '" + symbol + "' between '"
+ startYear + "' and '" + finalYear + "'.",
ioException);
}
}