Commit d2d3d4ff authored by Matija Obreza's avatar Matija Obreza

Cannot change signature of MappingServiceImpl#getTile because of

@Cacheable!
Missing @ResponseBody on save tries to load JSP file
parent 4bdca206
......@@ -16,8 +16,6 @@
package org.genesys2.server.service;
import org.genesys2.server.servlet.model.MapFilter;
import java.io.IOException;
public interface MappingService {
......@@ -28,7 +26,7 @@ public interface MappingService {
String filteredGeoJson(String string, Integer limit) throws IOException;
byte[] getTile(MapFilter mapFilterm,String jsonFilter, int zoom, int xtile, int ytile);
byte[] getTile(String jsonFilter, int zoom, int xtile, int ytile);
public static class CoordUtil {
public static double tileToLon(int zoom, int xtile) {
......
......@@ -29,7 +29,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.servlet.model.MapFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
......@@ -163,7 +162,7 @@ public class MappingServiceImpl implements MappingService {
@Override
@Cacheable(value = "tileserver", key = "'tile-' + #zoom + '-' + #xtile + '-' + #ytile + '-' + #jsonFilter")
public byte[] getTile(final MapFilter mapFilter,String jsonFilter, final int zoom, final int xtile, final int ytile) {
public byte[] getTile(String jsonFilter, final int zoom, final int xtile, final int ytile) {
final BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
ObjectNode jsonTree;
......@@ -203,12 +202,7 @@ public class MappingServiceImpl implements MappingService {
for (int i = -pixelSize / 2; i <= pixelSize / 2; i++) {
for (int j = -pixelSize / 2; j <= pixelSize / 2; j++) {
if (longitude + i >= 0 && latitude + j >= 0 && longitude + i < 256 && latitude + j < 256) {
bi.setRGB(longitude + i, latitude + j, Color.yellow.getRGB());
if (mapFilter!=null){
bi.setRGB(longitude + i, latitude + j, Color.decode(mapFilter.getColor()).getRGB());
}
bi.setRGB(longitude + i, latitude + j, Color.yellow.getRGB());
}
}
}
......
package org.genesys2.server.servlet.controller;
import com.jhlabs.image.MapColorsFilter;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.service.MappingService;
......@@ -25,75 +24,77 @@ import java.util.List;
@Scope("request")
public class MapFilterController extends BaseController {
@Autowired
private MappingService mappingService;
@Autowired
private FilterSelectionBean filterSelectionBean;
@RequestMapping(value = "/filter/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<MapFilter> getMapFilters() {
return filterSelectionBean.getFilters();
}
@RequestMapping(value = "/filter/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public void saveMapFilter(@RequestBody MapFilter filter) {
filterSelectionBean.addFilter(filter);
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
public void tile(@PathVariable("zoom") int zoom, @PathVariable("x") int x, @PathVariable("y") int y,
@RequestParam(value = "filter", required = true) String jsonFilter,
@RequestParam(value = "color", required = false) String color,
@RequestParam(value = "title", required = false) String title, HttpServletResponse response) {
MapFilter mapFilter = null;
if ((StringUtils.isNotBlank(title))) {
mapFilter = filterSelectionBean.getFilter(title);
color = mapFilter.getColor();
}
try {
byte[] image = mappingService.getTile(mapFilter, jsonFilter, zoom, x, y);
image = changeColor(color, image);
response.getOutputStream().write(image, 0, image.length);
} catch (final IOException e) {
_logger.warn(e.getMessage());
throw new RuntimeException("Could not render image", e);
} catch (final Throwable e) {
_logger.error(e.getMessage(), e);
throw new ResourceNotFoundException(e.getMessage());
}
}
private byte[] changeColor(String color, byte[] imageBytes) {
if (color == null) {
return imageBytes;
}
_logger.info("Changing color to " + color);
try {
final Color newColor = Color.decode(color);
if (newColor.equals(Color.yellow)) {
return imageBytes;
}
final MapColorsFilter mcf = new MapColorsFilter(Color.yellow.getRGB(), newColor.getRGB());
final ByteArrayInputStream bios = new ByteArrayInputStream(imageBytes);
final BufferedImage image = mcf.filter(ImageIO.read(bios), null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", baos);
return baos.toByteArray();
} catch (final NumberFormatException e) {
_logger.warn("Cannot get color for " + color);
return imageBytes;
} catch (final IOException e) {
_logger.warn(e.getMessage());
return imageBytes;
}
}
@Autowired
private MappingService mappingService;
@Autowired
private FilterSelectionBean filterSelectionBean;
@RequestMapping(value = "/filter/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<MapFilter> getMapFilters() {
return filterSelectionBean.getFilters();
}
@RequestMapping(value = "/filter/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public boolean saveMapFilter(@RequestBody MapFilter filter) {
filterSelectionBean.addFilter(filter);
return true;
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
public void tile(@PathVariable("zoom") int zoom, @PathVariable("x") int x, @PathVariable("y") int y,
@RequestParam(value = "filter", required = true) String jsonFilter, @RequestParam(value = "color", required = false) String color,
@RequestParam(value = "title", required = false) String title, HttpServletResponse response) {
MapFilter mapFilter = null;
if ((StringUtils.isNotBlank(title))) {
mapFilter = filterSelectionBean.getFilter(title);
color = mapFilter.getColor();
jsonFilter = mapFilter.getFilter();
}
try {
byte[] image = mappingService.getTile(jsonFilter, zoom, x, y);
image = changeColor(color, image);
response.getOutputStream().write(image, 0, image.length);
} catch (final IOException e) {
_logger.warn(e.getMessage());
throw new RuntimeException("Could not render image", e);
} catch (final Throwable e) {
_logger.error(e.getMessage(), e);
throw new ResourceNotFoundException(e.getMessage());
}
}
private byte[] changeColor(String color, byte[] imageBytes) {
if (color == null) {
return imageBytes;
}
_logger.info("Changing color to " + color);
try {
final Color newColor = Color.decode(color);
if (newColor.equals(Color.yellow)) {
return imageBytes;
}
final MapColorsFilter mcf = new MapColorsFilter(Color.yellow.getRGB(), newColor.getRGB());
final ByteArrayInputStream bios = new ByteArrayInputStream(imageBytes);
final BufferedImage image = mcf.filter(ImageIO.read(bios), null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", baos);
return baos.toByteArray();
} catch (final NumberFormatException e) {
_logger.warn("Cannot get color for " + color);
return imageBytes;
} catch (final IOException e) {
_logger.warn(e.getMessage());
return imageBytes;
}
}
}
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