Commit b2396d21 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

KML exhausts memory

parent c2911cef
......@@ -18,6 +18,7 @@ package org.genesys2.server.service;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -38,7 +39,7 @@ public interface MappingService {
* the filters
* @return the string
*/
String filteredKml(AppliedFilters filters);
void filteredKml(AppliedFilters filters, OutputStream outputStream) throws IOException;
/**
* Filtered geo json.
......@@ -47,7 +48,6 @@ public interface MappingService {
* the filters
* @param limit
* the limit
* @return the string
* @throws IOException
* Signals that an I/O exception has occurred.
*/
......
......@@ -19,6 +19,8 @@ package org.genesys2.server.service.impl;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
......@@ -71,36 +73,39 @@ public class MappingServiceImpl implements MappingService {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public String filteredKml(AppliedFilters filters) {
public void filteredKml(AppliedFilters filters, OutputStream outputStream) throws IOException {
filterService.transformFiltersIfNeed(filters);
LOG.debug(filters.toString());
final StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
sb.append("<Document>");
filterService.listGeo(filters, null, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
try {
sb.append("<Placemark>");
sb.append("<name>").append(rs.getString("acceNumb")).append("</name>");
sb.append("<description></description>");
sb.append("<Point><coordinates>");
sb.append(rs.getDouble("longitude")).append(",").append(rs.getDouble("latitude"));
sb.append("</coordinates></Point>");
sb.append("</Placemark>");
} catch (final SQLException e) {
LOG.warn(e.getMessage());
throw e;
}
}
});
sb.append("</Document>");
sb.append("</kml>");
return sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
writer.write("<Document>");
final OutputStreamWriter finalWriter = writer;
filterService.listGeo(filters, null, new RowCallbackHandler() {
@Override
public void processRow(final ResultSet rs) throws SQLException {
try {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<Placemark>");
stringBuilder.append("<name>").append(rs.getString("acceNumb")).append("</name>");
stringBuilder.append("<description></description>");
stringBuilder.append("<Point><coordinates>");
stringBuilder.append(rs.getDouble("longitude")).append(",").append(rs.getDouble("latitude"));
stringBuilder.append("</coordinates></Point>");
stringBuilder.append("</Placemark>");
finalWriter.write(stringBuilder.toString());
finalWriter.flush();
} catch (final SQLException e) {
LOG.warn(e.getMessage());
throw e;
} catch (final IOException e) {
LOG.warn(e.getMessage());
}
}
});
finalWriter.write("</Document></kml>");
finalWriter.flush();
}
@Override
......
......@@ -790,14 +790,28 @@ public class ExplorerController extends BaseController implements InitializingBe
@RequestMapping(value = "/explore/kml", produces = "application/vnd.google-earth.kml+xml", method = RequestMethod.POST)
@ResponseBody
public String kml(@RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
public void kml(@RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
@RequestParam(value = "filter", required = true) String jsonFilter, HttpServletResponse response) throws IOException {
final AppliedFilters appliedFilters = updateFilterWithCrop(cropName, jsonFilter);
response.setContentType("application/vnd.google-earth.kml+xml");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-kml-filtered.kml\""));
final int countFiltered = genesysService.countAccessions(appliedFilters);
LOG.info("Attempting to download KML for {} accessions", countFiltered);
if (countFiltered > DOWNLOAD_LIMIT) {
throw new RuntimeException("Refusing to export more than " + DOWNLOAD_LIMIT + " entries");
}
response.setContentType("application/vnd.google-earth.kml+xml");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-kml-filtered.kml\""));
// Write KML to the stream.
final OutputStream outputStream = response.getOutputStream();
return mappingService.filteredKml(appliedFilters);
try {
mappingService.filteredKml(appliedFilters, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
}
}
/**
......
......@@ -777,11 +777,12 @@
type: "GET",
dataType: 'json',
success: function (respObject) {
if (respObject.features == null || respObject.features.length == 0)
var parsedRespObject = jQuery.parseJSON(respObject);
if (parsedRespObject.features === null || parsedRespObject.features.length === 0)
return;
var c = "";
respObject.features.forEach(function (a, b) {
parsedRespObject.features.forEach(function (a, b) {
if (b < 10)
c += "<a href='<c:url value="/acn/id/" />" + a.id + "'>" + a.properties.acceNumb + " " + a.properties.instCode + "</a><br />";
else
......
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