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

KML exhausts memory

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