TileController.java
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.api.v1;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.genesys.server.service.MappingService;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.util.ColorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* This is not an API controller, it's accessible to anonymous users.
*/
@Controller
public class TileController {
private static final Logger LOG = LoggerFactory.getLogger(TileController.class);
@Autowired
private MappingService mappingService;
@Autowired
private ShortFilterService shortFilterService;
@Autowired
private ThreadPoolTaskExecutor tileGeneratorPool;
@RequestMapping(value = "/acn/tile/{zoom:\\d+}/{x:\\d+}/{y:\\d+}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<?> tile(@RequestParam(value = "f", required = false, defaultValue = "") String filterCode,
@RequestParam(value = "color", required = false) String color,
@PathVariable("x") int x, @PathVariable("y") int y, @PathVariable("zoom") int zoom) throws IOException, ExecutionException, InterruptedException {
return tileGeneratorPool.submit(() -> {
try {
AccessionFilter appliedFilters = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
byte[] image = mappingService.getTile(appliedFilters, zoom, x, y);;
image = ColorUtil.changeColor(color, image);
// HTTP maxAge
int maxAge;
if (zoom < 4) {
maxAge = 60 * 60 * 24; // 24 hrs
} else if (zoom < 6) {
maxAge = 60 * 60 * 2; // 2 hrs
} else if (zoom < 8) {
maxAge = 60 * 60; // 1 hr
} else {
maxAge = 60 * 5; // 5 min
}
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.CACHE_CONTROL, "max-age=" + maxAge + ", s-maxage=" + maxAge + ", public, no-transform")
.contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*") // TODO Fixme
.contentLength(image.length)
.body(image);
} catch (Exception e) {
LOG.warn("Exception in tile request processing", e);
return ResponseEntity.badRequest().build();
}
}).get();
}
}